Gamestudio Links
Zorro Links
Newest Posts
Lapsa's very own thread
by Lapsa. 06/26/24 12:45
Executing Trades on Next Bar Open
by Zheka. 06/20/24 14:26
A simple game ...
by VoroneTZ. 06/18/24 10:50
Face player all the time ...
by bbn1982. 06/18/24 10:25
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Lapsa), 1,339 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Mino, squik, AemStones, LucasJoshua, Baklazhan
19061 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: 27,987
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,987
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 | chip programmers | 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