Skip to main content
gamedev.net
🔒 Locked

memory allocation problem

Started by CProgrammer Dec 30, 2002 at 1:09 AM 3 replies 350+ views
Original Post
CProgrammer
CProgrammer
i have a structure with two pointers:
struct something
{
    constructer *x;
    int *y;
}
 
how do i perform dynamic memory allocation on this: something *a = (SOMETHING*)malloc(size*sizeof(SOMETHING)); is not the correct way since this doesnt take the second pointer in account. Somebody please help me.
Zipster
Zipster
That's just fine. I think you're getting confused between allocating the object that contains the pointers, and allocating memory for the pointer member themselves. Pointers are variables in themselves, taking up 4 bytes on 32-bit systems.

The statement you provided will allocate an array of type something, size determining how many elements there will be. If you then want to allocate memory for the individual members, you would do something like this:

s[0].x = (constructer*)malloc(size*sizeof(constructer));
s[0].y = (int*)malloc(size*sizeof(int));

If you allocated an array of somethings, then you can change the index accordingly.

[edited by - Zipster on December 30, 2002 2:32:50 AM]
CProgrammer
CProgrammer
I did allocate them seperately sorry for not mentioning. Well it seems then, that the problem lies somewhere else.
Posibly in the calculation of size eventhough i checked that too. I''ll have to continue searching thanks for the help
-CProgrammer
Zipster
Zipster
Well, what''s the problem exactly? What kind of behavior are you experiencing?
CProgrammer
CProgrammer
I did allocate them seperately sorry for not mentioning. Well it seems then, that the problem lies somewhere else.
Posibly in the calculation of size eventhough i checked that too. I'll have to continue searching thanks for the help

The problem is in my QuadTree heres the algorithm calulating the size:

int i;
//example values
xdim = 1024
int leaflength = 16;
numleaves = (xdim/leaflength)*(xdim/leaflength);
numnodes = 1;
int tempn = 1;

for(i = 0; i < (xdim/4)/leaflength; i++)
{
numnodes+=4*tempn;
tempn = 4*tempn;
}


//This memory alocaion
nodes = (NODE*)malloc((numnodes+numleaves)*sizeof(NODE));

//the pointers in node are allocated later on

sorry didnt see youre post.
Behavior: Unless multiply the size by around 200 (of nodes memory) the application crashes after some time in the CREATENODE function


[edited by - CProgrammer on December 30, 2002 2:52:04 AM]

[edited by - CProgrammer on December 30, 2002 2:54:04 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.