Click here to Skip to main content
14,774,712 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
QuestionFIFO en languge C Pin
Member 1249381829-Apr-16 11:37
MemberMember 1249381829-Apr-16 11:37 
AnswerRe: FIFO en languge C Pin
Bram van Kampen29-Apr-16 12:48
MemberBram van Kampen29-Apr-16 12:48 
AnswerRe: FIFO en languge C Pin
Patrice T30-Apr-16 22:29
mvePatrice T30-Apr-16 22:29 
AnswerRe: FIFO en languge C Pin
CPallini1-May-16 21:49
mveCPallini1-May-16 21:49 
QuestionFour methods to create, pass to function and delete 2d arrays in c++ Pin
Javier Luis Lopez29-Apr-16 0:32
MemberJavier Luis Lopez29-Apr-16 0:32 
AnswerRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
Bram van Kampen29-Apr-16 13:27
MemberBram van Kampen29-Apr-16 13:27 
GeneralRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
Javier Luis Lopez30-Apr-16 0:05
MemberJavier Luis Lopez30-Apr-16 0:05 
AnswerRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
leon de boer29-Apr-16 19:01
Memberleon de boer29-Apr-16 19:01 
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 Smile | :)
In vino veritas


modified 30-Apr-16 1:42am.

GeneralRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
Javier Luis Lopez30-Apr-16 0:08
MemberJavier Luis Lopez30-Apr-16 0:08 
GeneralRe: Three methods to create, pass to function and delete 2d arrays in c++ Pin
leon de boer30-Apr-16 1:47
Memberleon de boer30-Apr-16 1:47 
QuestionHow to send huge data via sockets in continuous intervals Pin
manoharbalu28-Apr-16 23:52
Membermanoharbalu28-Apr-16 23:52 
AnswerRe: How to send huge data via sockets in continuous intervals Pin
Shyam Kodase29-Apr-16 0:39
MemberShyam Kodase29-Apr-16 0:39 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
manoharbalu29-Apr-16 1:32
Membermanoharbalu29-Apr-16 1:32 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
Shyam Kodase22-May-16 22:15
MemberShyam Kodase22-May-16 22:15 
AnswerRe: How to send huge data via sockets in continuous intervals Pin
Shyam Kodase29-Apr-16 0:42
MemberShyam Kodase29-Apr-16 0:42 
QuestionPhone Dialer Pin
Bram van Kampen28-Apr-16 13:57
MemberBram van Kampen28-Apr-16 13:57 
QuestionRe: Phone Dialer Pin
David Crow28-Apr-16 18:16
MemberDavid Crow28-Apr-16 18:16 
AnswerRe: Phone Dialer Pin
Bram van Kampen29-Apr-16 12:43
MemberBram van Kampen29-Apr-16 12:43 
QuestionRe: Phone Dialer Pin
David Crow30-Apr-16 10:30
MemberDavid Crow30-Apr-16 10:30 
AnswerRe: Phone Dialer Pin
Bram van Kampen1-May-16 15:30
MemberBram van Kampen1-May-16 15:30 
SuggestionRe: Phone Dialer Pin
David Crow1-May-16 17:32
MemberDavid Crow1-May-16 17:32 
GeneralRe: Phone Dialer Pin
Bram van Kampen2-May-16 15:12
MemberBram van Kampen2-May-16 15:12 
GeneralRe: Phone Dialer Pin
Bram van Kampen2-May-16 16:01
MemberBram van Kampen2-May-16 16:01 
AnswerRe: Phone Dialer Pin
Bram van Kampen3-May-16 13:36
MemberBram van Kampen3-May-16 13:36 
QuestionRe: Phone Dialer Pin
David Crow3-May-16 18:30
MemberDavid Crow3-May-16 18:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.