forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

dynamic memory allocation with realloc

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
lagwagon666



Joined: 20 Aug 2006
Posts: 4

PostPosted: Tue Aug 22, 2006 6:48 am    Post subject: dynamic memory allocation with realloc Reply with quote

Hello all,

I'm trying to allocate memory to dynamically resize an array of a class. As in other parts the malloc()-call works well for me, I tried using realloc(). But this doesn't seem to work, it always returns me a NULL-pointer. So, what function can I use to resize an array dynamically? Here's the code in question:
Code:

geo_object **geo_objects;

geo_object* level::get_geo_object_address()
{
   geo_object_count++;
   geo_objects=(geo_object**)realloc(geo_objects,sizeof(geo_object)*_geo_object_count);
   return geo_objects[_geo_object_count-1];
}

Any Ideas? Thx for your help,
Dave-O
Back to top
View user's profile Send private message
Aion



Joined: 24 Jul 2006
Posts: 40
Location: Montreal

PostPosted: Tue Aug 22, 2006 7:31 am    Post subject: Reply with quote

Not sure if it's realated, but the size allocated should be :
-sizeof(geo_object*) * geo_object_count
Not
-sizeof(geo_object) * geo_object_count

Since you're allocating space of for an array of pointers.
Back to top
View user's profile Send private message
PeterM



Joined: 31 Dec 2005
Posts: 125
Location: Edinburgh, UK

PostPosted: Tue Aug 22, 2006 7:39 pm    Post subject: Reply with quote

Why not use a std::vector of pointers?
http://www.sgi.com/tech/stl/Vector.html
Back to top
View user's profile Send private message Visit poster's website
lagwagon666



Joined: 20 Aug 2006
Posts: 4

PostPosted: Thu Aug 24, 2006 2:11 am    Post subject: Reply with quote

Aion wrote:
Not sure if it's realated, but the size allocated should be :
-sizeof(geo_object*) * geo_object_count

you're right, that would probably have been the next problem I'd run into :) My problem was, that I tried to access the **geo_objects-var using array-style instead using pointer-arithmetics (geo_object[0] instead geo_object+0). However, thanks for solving my "future problem".

I also have an look at vectors.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group