Skip to main content
gamedev.net
🔒 Locked

allocating memory

Started by tHiSiSbOb 3 replies 350+ views
Original Post
tHiSiSbOb
tHiSiSbOb
I want to be able to declare several blocks of memory that are right next to each other, one a block of ints, another a block of floats, and another a block of chars-lets just say. Is there anyway to do this using something like malloc? If so, how. If not, how do I do this? Pleas don''t tell me I should use structures because I know that in the long run this will not work out for my applications. Thank you in advance.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
T2k
T2k
something like:

void *totalmem= malloc(sizeof(int)*intamount+ sizeof(float)* floatamount);
int *urints= (int *) totalmem;
float *urfloats= (float *)(totalmem + sizeof(int)*intamount);


got it?


T2k
tHiSiSbOb
tHiSiSbOb
ok, I tried that but when I am trying to set the float pointer to by adding the amount of space taken by the integers to the pointer oto the memory, I get a compiler error that read "''void*'':unknown size".
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
tHiSiSbOb
tHiSiSbOb
Ok, nevermind, I figured it out. I should have given it more thought before posting again.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
Trienco
Trienco
i assume you want to do that in plain c?

void *mymem= malloc(lots_of_memory);
void *nextfree=mymem;

int* ints=(int*)nextfree;
nextfree= (int*)nextfree + num_ints;
float* floats=(float*)nextfree;
nextfree= (float*)nextfree + num_floats;

but thats very limited and you cant free any of the smaller blocks. with a little more work you could do your own memory management.
f@dzhttp://festini.device-zero.de

Topic Locked

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

Sign in to reply to this topic.