Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dougphd

macrumors member
Original poster
Apr 28, 2016
70
0
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.
 
Last edited:
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.
 
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.
A linked list. Every struct has a pointer to the next one. Right? I'll look it up. Thanks.
 
A linked list. Every struct has a pointer to the next one. Right? I'll look it up. Thanks.
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 :))
 
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 :))

thanks
and just c no languages that compile at runtime.
 
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)
 
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.
 
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?
 
Last edited:
Not slashing C I use it where it's appropriate, but why reinvent something and make things more difficult.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.