Allocating memory for an array of structures

Discussion in 'Mac Programming' started by dougphd, Oct 24, 2016.

  1. dougphd, Oct 24, 2016
    Last edited: Oct 24, 2016

    dougphd macrumors member

    Joined:
    Apr 28, 2016
    #1
    Once more, more clearly I hope. Words - no code
    In c, I want to define an array of structures then every time I call a routine, I want to create another element for that array and allocate memory. How do I do it? Thanks.
     
  2. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #2
    You would probably do this using a linked list. I recommend Algorithms in C by Robert Sedgewick for an introduction in linked lists, binary trees, searching, etc.

    Since this is a Mac programming forum, you could also check out some Cocoa API like NSMutableDictionary.
     
  3. dougphd thread starter macrumors member

    Joined:
    Apr 28, 2016
    #3
    A linked list. Every struct has a pointer to the next one. Right? I'll look it up. Thanks.
     
  4. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #4
    http://www.cprogramming.com/tutorial/c/lesson15.html

    Then you have to think about do you want to traverse the list in both directions, do you want it to loop around when you hit the end, how do you add/remove/replace nodes...

    I'd suggest finding a library that already has done this for you.

    (or using a language that has lists as a built-in type :))
     
  5. dougphd thread starter macrumors member

    Joined:
    Apr 28, 2016
    #5
    thanks
    and just c no languages that compile at runtime.
     
  6. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #6
    NO.
     
  7. Toutou macrumors 6502a

    Toutou

    Joined:
    Jan 6, 2015
    Location:
    Prague, Czech Republic
    #7
    A linked list is only useful under certain conditions and this doesn't look like it needs a linked list.

    What you need is an array that can resize dynamically, just:
    Code:
    MyStruct * struct = (MyStruct *)malloc(sizeof(MyStruct) * no_of_elements);
    and then after each insertion
    Code:
    struct = (MyStruct *)realloc(sizeof(MyStruct) * new_no_of_elements);
    That would be the simple approach but it's extremely ineffective (realloc is a costly thing to run after a single insertion).

    The approach that is usually used (in a C++ vector, for example) is to alloc the array to a size of N, insert as necessary, and just realloc to 2*N or so when the starting size is filled up (and again to 2*(2*N) when 2*N is not enough)
     
  8. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #8
    Why do you want to do this in C?

    If you are programming for OSX using Objective C it has been already done for you. Use one of the "mutable" classes and create your own object with the structure you desire within it from NSObject.

    If you are using straight C for its the speed or portability you will have to take care of the memory allocation and freeing within your code. But again why spend the time and hassle redesigning what someone else has done for you.
     
  9. mif, Oct 29, 2016
    Last edited: Oct 29, 2016

    mif macrumors regular

    Joined:
    Feb 16, 2010
    Location:
    home
    #9
    No offense to anyone, but why do you slash plain c. I kind of like it and if someone likes to improve his skills... there is no harm in that?
     
  10. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #10
    Not slashing C I use it where it's appropriate, but why reinvent something and make things more difficult.
     
  11. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #11
    I would use an instance of the Cocoa class NSMutableArray. This is Described on page 46 of Cocoa Programming for Mac OS X. This is an excellent book for learning Cocoa, Objective C.
     

Share This Page