Valhalla Legends Forums Archive | C/C++ Programming | malloc and structs

AuthorMessageTime
warz
I have a loop calling the dlopen() function and and setting the returned handle to a struct i have. the thing is, the number of calls to dlopen isn't static. it will change often. so i dont know how many times ill have to declare and instance of my structure for the dlopen() call. i need to use malloc() so i can create however many instances of my struct that i need. malloc only takes size as an argument though. how should i go about using malloc with my struct so after the loop im left with multiple structures?
March 1, 2005, 6:11 AM
Maddox
this is pretty basic. if you're going to be iterating through these a lot, you'll probably want to look into making a linked list.

anyways, first you'll need to count up the number of libraries are you are loading initially.

create your array of structs ( num is the number of initial libraries )
[code]
int i;
struct mystruct * dlarray = (struct mystruct *)malloc(num * sizeof(struct mystruct));

/* populate it now */
for(i = 0; i < num; i++)
{
        dlarray[i].member = something;
}
[/code]

when you want to add a library, you'll need to resize your array. ( newnum is the new number of libraries )
[code]
struct mystruct * oldarray = dlarray;
struct mystruct * newarray = (struct mystruct *)malloc(newnum * sizeof(struct mystruct));

/* remember num is the old size */
dlarray = (struct mystruct *)memcpy((void *)newarray, (void *)oldarray, num * sizeof(struct mystruct));
free(oldarray);

num = newsize;

/* populate new */
dlarray[num - 1].member = something;
[/code]
March 1, 2005, 6:56 AM
Zakath
I recommend using new and delete rather than malloc and free, since their usage is so much simpler. It's up to you, of course, but I find that[code]mystruct *dlarray = new mystruct[ num ];[/code]is easier to work with than[code]mystruct *dlarray = (mystruct *)malloc( num * sizeof( mystruct ));[/code]
March 1, 2005, 8:43 AM
warz
Ah, this is in C. Sorry, forgot to mention that.
March 1, 2005, 3:31 PM
warz
[quote author=Maddox link=topic=10762.msg102060#msg102060 date=1109660207]
this is pretty basic. if you're going to be iterating through these a lot, you'll probably want to look into making a linked list.

anyways, first you'll need to count up the number of libraries are you are loading initially.

create your array of structs ( num is the number of initial libraries )
[code]
int i;
struct mystruct * dlarray = (struct mystruct *)malloc(num * sizeof(struct mystruct));

/* populate it now */
for(i = 0; i < num; i++)
{
        dlarray[i].member = something;
}
[/code]

when you want to add a library, you'll need to resize your array. ( newnum is the new number of libraries )
[code]
struct mystruct * oldarray = dlarray;
struct mystruct * newarray = (struct mystruct *)malloc(newnum * sizeof(struct mystruct));

/* remember num is the old size */
dlarray = (struct mystruct *)memcpy((void *)newarray, (void *)oldarray, num * sizeof(struct mystruct));
free(oldarray);

num = newsize;

/* populate new */
dlarray[num - 1].member = something;
[/code]
[/quote]

Perfect, Maddox. Works great. Thanks, brotha. hehe.
March 1, 2005, 4:01 PM
Kp
Maddox's code reflects a C++ user trying to think in C. :)  There's no need to explicitly malloc/free when growing the array, just use realloc(void *old_memory, int new_size);  realloc will then handle the copy/free details for you, leaving you with one memory block of the new size (possibly at a new address) which is guaranteed to have the same contents for the first min(oldsize, newsize) bytes.  C++'s memory allocation operators lack an equivalent call.
March 1, 2005, 10:18 PM
Maddox
[quote author=Kp link=topic=10762.msg102109#msg102109 date=1109715519]
Maddox's code reflects a C++ user trying to think in C. :)  There's no need to explicitly malloc/free when growing the array, just use realloc(void *old_memory, int new_size);  realloc will then handle the copy/free details for you, leaving you with one memory block of the new size (possibly at a new address) which is guaranteed to have the same contents for the first min(oldsize, newsize) bytes.  C++'s memory allocation operators lack an equivalent call.
[/quote]

this is true. I'm actually writing some C code right now, so this will come in handy.
March 2, 2005, 4:17 AM
Zakath
I would say that thinking in C++ isn't such a bad thing :P

C: myfunc( stuff ) int stuff; { /* function body */ }

C++: void myfunc( int stuff ) { /* function body */ }
March 4, 2005, 12:38 AM
Kp
[quote author=Zakath link=topic=10762.msg102374#msg102374 date=1109896685]
I would say that thinking in C++ isn't such a bad thing :P

C: myfunc( stuff ) int stuff; { /* function body */ }

C++: void myfunc( int stuff ) { /* function body */ }
[/quote]

That's not at all relevant, and the latter style has been supported (and afaik encouraged) by gcc for ages.  Anyway, thinking in C++ is a bad idea when you're trying to write C code, since there's some C idioms that got lost in C++ (for instance, the idea of using realloc, which got lost because nobody thought to put a reallocation operator in C++).  You can make something that works, but it'll generally be less elegant.
March 4, 2005, 2:15 AM

Search