#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:
How can I get the size of a dynamically allocated array?
And my other problem (pointer returned for local array):
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*
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*