| View previous topic :: View next topic |
| Author |
Message |
lagwagon666
Joined: 20 Aug 2006 Posts: 4
|
Posted: Tue Aug 22, 2006 6:48 am Post subject: dynamic memory allocation with realloc |
|
|
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 |
|
 |
Aion
Joined: 24 Jul 2006 Posts: 40 Location: Montreal
|
Posted: Tue Aug 22, 2006 7:31 am Post subject: |
|
|
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 |
|
 |
PeterM
Joined: 31 Dec 2005 Posts: 125 Location: Edinburgh, UK
|
|
| Back to top |
|
 |
lagwagon666
Joined: 20 Aug 2006 Posts: 4
|
Posted: Thu Aug 24, 2006 2:11 am Post subject: |
|
|
| 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 |
|
 |
|