C / C++ / MFC
|You seem to have yourself very confused on pointer dereferencing and memory allocation. You are trying to assign "right way" to something that has nothing to do with the language but what is essentially about memory ownership.
Your version 3 is mongrel of a thing and NOT CLASSICAL AT ALL. Look carefully at what it is which is an array of pointers on the stack if within a function or on the heap if globally assigned and into which you place allocated memory. YUCK writing any function via pointer reference to access that abomination would be fun and dangerous it's not necessarily a persistent structure unless defined globally.
So how about I give you 4 WHICH IS ACTUALLY THE CLASSICAL VERSION
//4. The ACTUAL CLASSICAL METHOD
//4.1. Create and Array of pointers:
float **matriz3 = new (float*[YMAX]); // Create an array of pointers
//4.2. Fill each pointer in the array with a allocated pointer to a block of memory:
for (int y = 0; y < YMAX; y++)
matriz3[y] = new float[XMAX];;
// Its derived from the standard C version which looks like this
float **matriz3 = (float**)malloc(YMAX * sizeof(float*)); // Create an array of pointers to a float
for (int y = 0; y < YMAX; y++)
*matriz3[y] = (float*)malloc(XMAX *sizeof(float)); // Fill each pointer array entry
So now you have another one and all of them are perfectly fine within the contexts of what they were designed to do although 3 is of very limited use locally. You would need to take care passing a reference to your version 3. Method 4 is usable anywhere and someone needs to take responsibility to delete/free the allocated memory blocks.
Whats different is 1 and I think 2 allocate all the memory together in one big contiguous block, 3 & 4 don't do that they have a pointer block which points to memory blocks for each row array and those blocks can be all over the place in physical memory. Structurally they are both 2D float arrays but there is differences in there internal memory layout. Versions 3 & 4 will never produce anything that looks like version 1 without a lot of luck with memory allocation.
I won't comment on 2 because I have a feeling it may be compiler dependent C++11 seems to say it will be a contiguous block but I am not sure all compilers will do it if they are not C++11 compliant.
The two structures also do no use the same amount of memory take a 2x2 matrix. Version 1 simply allocates memory for 4 floats. Version 3 & 4 allocate 2 pointers and then 2 floats allocated into each pointer. So if your floats are 4 bytes and your pointers are 4 bytes, version 1 allocates 16 bytes of memory and version 3&4 allocate 8+16=24bytes.
Technically the access to version 1 is faster but it has the considerable downside that as the structure gets larger the chances of finding that amount of contiguous memory block size becomes difficult. Version 3 & 4 can go to much larger size and are more likely to still work at larger sizes that version 1.
So you asked:
Is the first method correct for all the platforms? Yes but that is only one way to make a 2D float array don't expect that to work for other methods on any platform.
Your overloaded functions are correct for 1, 3 & 4 ... I am still wary of 2 you might need to do some checking
//This print method only works for method 1 & 2:
void print_data2d(float *matriz,int ymax,int xmax,char *method);
//This print method only works for method 3 & 4
void print_data2d(float **matriz,int ymax,int xmax,char *method);
There is some danger in that overloading that the use of an "&" will give you the wrong one. Consider a version 1 array and I went to print it.
float *matriz; /// version 1 array
print_data2d (&matriz, .... ); // made a mistake with &
See what happens there .... matriz is a float* .. &matriz is a float** .. so the call takes the second form and probably crashes and burns.
I think for safety you need to do a bit of typecasting
In vino veritas
modified 30-Apr-16 1:42am.
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.