|
memory allocation
#237552
11/20/08 16:41
11/20/08 16:41
|
Joined: Aug 2004
Posts: 1,305 New York
PrenceOfDarkness
OP
Serious User
|
OP
Serious User
Joined: Aug 2004
Posts: 1,305
New York
|
okay i've been avoiding this topic for a long time simply because it's a bit scary (hold me :().
Okay I get the basic drift of it but I'm not really seasoned in it.
lets say I have defined a struct and I want to create a new struct on the fly, I understand now that I need to set memory aside for it. I believe when using malloc it returns a pointer to that memory location. Can someone please give me a simple example of how to do that?
Also i'm noticing that sometimes I want different size arrays. Again memory allocation is the key to this as well. Can someone give me a simple example of how to allocate memory for a varying array?
"There is no problem that can't be solved with time and determination." -me prenceofdarkness for instant messages on AIM.
Looking for a model designer PLEASE, SEND ME A PRIVATE MESSAGE OR EMAIL IF YOU'RE INTERESTED.
|
|
|
Re: memory allocation
[Re: PrenceOfDarkness]
#237554
11/20/08 17:13
11/20/08 17:13
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Don't be afraid, I am from the Internet, ma'am! ;-) typedef struct mytruct // struct definition
{
int example;
};
void foo()
{
mystruct ms = NULL; // variable definition
ms = malloc(sizeof(mystruct)); // allocate memory.
memset(ms,0,sizeof(mystruct)); // initialize memory.
ms->example = 1337; // use the struct
free(ms); // free the memory allocated
ms = NULL; // signalize the pointer has become invalid.
} And for the dynamic array ... void bar()
{
int* a = NULL; // define array as a pointer to an int.
a = malloc(sizeof(int) * 23); // Allocate memory for 23 elements of int
a[12] = 1337; // Use the array for whatever you want.
free(a); // free the allocated memory.
a = NULL; // invalidate the pointer.
}
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: memory allocation
[Re: Uhrwerk]
#237610
11/20/08 23:16
11/20/08 23:16
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Don't be afraid, I am from the Internet, ma'am! ;-) Shouldn't that raise a grave fear instead? . I do believe you need to typecast the newly allocated memory block pointer to your desired structure. It's required in C, not sure if the Lite-C compiler complains about it. ms = (mystruct*)malloc(sizeof(mystruct)); // allocate memory.
...
a = (int)malloc(sizeof(int) * 23);
Last edited by Joozey; 11/20/08 23:24.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: memory allocation
[Re: Joozey]
#237612
11/20/08 23:44
11/20/08 23:44
|
Joined: May 2006
Posts: 53 Puerto Rico
monchito
Junior Member
|
Junior Member
Joined: May 2006
Posts: 53
Puerto Rico
|
Hi my compiler complains with the struct code. I have to rewrite to get it work. Not sure if typecasting is required in C-lite but I think yes. [/code]
typedef struct // struct definition
{
int example;
}mystruct;
[code]
|
|
|
Re: memory allocation
[Re: monchito]
#237628
11/21/08 02:50
11/21/08 02:50
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Type-casting IS required but not by the compiler. IE It will compile the malloc command without typecasting, but later calls to that variable will fail IF IT IS A STRUCT. If its not a struct, it seems to work OK, I think it is defaulting to a var pointer.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: memory allocation
[Re: EvilSOB]
#237764
11/22/08 04:26
11/22/08 04:26
|
Joined: Aug 2004
Posts: 1,305 New York
PrenceOfDarkness
OP
Serious User
|
OP
Serious User
Joined: Aug 2004
Posts: 1,305
New York
|
after looking into this I found out that type casting is not a requirement however it is high suggested. Of course this is for C, and i'm not sure about lite C.
THe code looks useful and friendly I'll look into it and ask any questions if i encounter and problems. Thank you for the commments guys!
"There is no problem that can't be solved with time and determination." -me prenceofdarkness for instant messages on AIM.
Looking for a model designer PLEASE, SEND ME A PRIVATE MESSAGE OR EMAIL IF YOU'RE INTERESTED.
|
|
|
Re: memory allocation
[Re: PrenceOfDarkness]
#237821
11/22/08 16:51
11/22/08 16:51
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Typecasting is not necessary in Lite-C. However, it's a good habbit to do it and it's best you get once used to it and then do it forever. I simply forgot it when writing the examples.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|