Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
0 registered members (), 17,758 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Problems with NULL #167948
11/17/07 21:25
11/17/07 21:25
Joined: Sep 2007
Posts: 14
X
xykynan Offline OP
Newbie
xykynan  Offline OP
Newbie
X

Joined: Sep 2007
Posts: 14
Until recently, I have been using the following method to reserve memory for structs:

ITEM item[10];
ITEM* pitem[10];
pitem[0] = &item[0];
pitem[1] = &item[1];
...ect

This has been working fine, but I thought that using malloc might be preferable, so I changed it to:

ITEM* pitem[10];
pitem[0] = malloc(sizeof(ITEM));
pitem[1] = malloc(sizeof(ITEM));
...ect

However when I made this change, all the instances where I used a comparison with NULL, the program crashed. For example:

if (pitem[0]->member != NULL)
{
pitem[0]->member = member_001;
}

I've gone through the code again and again, using breakpoints and monitering variables and I'm fairly certain that the code only crashes when it comes to a NULL comparison. So, I was wondering if I should just go back to my old method or if I am using malloc wrong?
Thanks in advance!

Re: Problems with NULL [Re: xykynan] #167949
11/19/07 13:31
11/19/07 13:31
Joined: Jul 2000
Posts: 28,024
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,024
Frankfurt
malloc() delivers uninitialized memory. Thus your NULL comparision won't work. You need to explicitely set the memory to zero, using memset.

Re: Problems with NULL [Re: jcl] #167950
11/19/07 20:18
11/19/07 20:18
Joined: Sep 2007
Posts: 14
X
xykynan Offline OP
Newbie
xykynan  Offline OP
Newbie
X

Joined: Sep 2007
Posts: 14
Thanks! That did the trick. So my code now looks like this:

pitem[0] = malloc(sizeof(ITEM));
memset (pitem[0], 0, sizeof(ITEM));

Is this the best way to initialize arrays of struct pointers?


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1