gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > C/C++ trouble

#33826 - LOst? - Sun Jan 09, 2005 7:01 pm

I still have some troubles with C/C++ when it comes to dynamically allocated memory (C++), and a temporary allocated array which pointer is returned (C/C++).

Well, here is the first problem:
Code:

   // According to my C++ book:
   char array [18];
   sizeof (array [0]);      // <-- Returns 1
   sizeof (array);         // <-- Returns 18

   // My problem:
   char array = new char [18];
   sizeof (*array);      // <-- Returns 1
   sizeof (array);         // Returns 4 and not 18!

How can I get the size of a dynamically allocated array?

And my other problem (pointer returned for local array):
Code:

char* CreateArray ()
{
   char* tmp_array = new char [256];      // This is a local variable right?

   tmp_array [50] = 'Y';

   return tmp_array;
}

void main (void)
{
   char* array = NULL;

   array = CreateArray ();         // Since the pointer is returned, how do I know the array is still there when it tmp_array was local to the function CreateArray?

   if (array [50] == 'Y')         // Is the data really left in memory? Is it valid?
      ...
}


These things are bugging me so much. I don't have any good C++ book covering up what happens to the memory between functions.

Help you great C++ people! *bows*

#33835 - sajiimori - Sun Jan 09, 2005 8:23 pm

Quote:
How can I get the size of a dynamically allocated array?
When you reference a real array directly (i.e. not via a pointer), the compiler can see its size by looking at the declaration. Since pointers only store an address, the only thing the compiler knows is its type. The size will always be the size of a pointer.

(You might say that the compiler should be able to see that you just allocated 18 bytes, but there is no general way for the compiler to know that. For instance, if you pass a pointer to a function, what size should the compiler assume the array is? Is it even an array at all, or just a pointer to a single object? Does it even point to a valid object, or is it a null pointer or garbage?)

If you want to retain the size, you have to store it seperately. You can make a class that overloads operator[] to hide all that.
Quote:
This is a local variable right?
The pointer is local (which means it's on the stack), but the array is on the heap. Heap memory must be explicitly allocated and deallocated (with malloc/free or new/delete). You return a copy of the pointer to the array, which is fine, and then you store that pointer in another local in main, which is also fine.

The hard part is making sure you delete the array (by using the delete operator on a pointer to the array) after you're sure nobody is going to use it, but before you lose the last pointer to it. If you lose the last pointer without deleting the array, you can never delete the array and the memory can't be reclaimed.

As for books, since I usually recommend mastering plain C before using C++, that also implies that you'd have dynamic memory allocation covered, so a book like "C Primer Plus" covers all that. Since my favorite C++ books build on C, they will be assuming you know how it works.

#33837 - LOst? - Sun Jan 09, 2005 8:33 pm

Thank you sajiimori! This isn't the first time you have saved my day!

I kinda knew this a little, but I want to be sure because everytime i'm going to program something that uses this, I get a little bit nervous it will backfire some how.

And I'm good at NULLing the pointers before and after use of new and delete.

#42295 - PragmaFury - Mon May 09, 2005 3:23 am

This won't work on the GBA, but if you're doing Windows development, there is a platform method called _msize which will return the size of an array allocated on the heap.

Of course, it will only work if the memory is allocated with malloc, and I certainly wouldn't condone using it, but just FYI.

#42335 - LOst? - Tue May 10, 2005 4:17 am

PragmaFury wrote:
This won't work on the GBA, but if you're doing Windows development, there is a platform method called _msize which will return the size of an array allocated on the heap.

Of course, it will only work if the memory is allocated with malloc, and I certainly wouldn't condone using it, but just FYI.


Thanks, I guess it works for operator new too?