Click here to Skip to main content
15,897,518 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
Richard MacCutchan25-Apr-19 2:46
mveRichard MacCutchan25-Apr-19 2:46 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
zak10025-Apr-19 6:52
zak10025-Apr-19 6:52 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
Joe Woodbury29-Apr-19 6:38
professionalJoe Woodbury29-Apr-19 6:38 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
leon de boer3-May-19 4:20
leon de boer3-May-19 4:20 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
Joe Woodbury3-May-19 5:51
professionalJoe Woodbury3-May-19 5:51 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
zak10025-Apr-19 7:05
zak10025-Apr-19 7:05 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
zak10025-Apr-19 7:10
zak10025-Apr-19 7:10 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
leon de boer25-Apr-19 7:49
leon de boer25-Apr-19 7:49 
That isn't correct but you are getting away with it because the pointer size is standard double* and double** are the same size.
If you are handing this in for a uni assignment they will ping you for it.

Lets put the comments over it so you get it
/* First you malloc a set of pointers to a double ... not a pointer to a pointer to a double */ 
/* data is a pointer to a pointer of double .. so you need to allocate pointers to doubles */
data = (double **) malloc(num_cols * sizeof(double *));

/* Then for each pointer of double you malloc a 1D array of doubles to that pointer */
for(int i = 0; i < num_cols; i++)
  data[i] = (double *) malloc(num_rows * sizeof(double));

/* see how the thing inside the sizeof is always one asterix less than what malloc returns */

So you end up using more space this way because you have the actual array data (column x row) plus row number of pointers.

If you flat 1D the array you can just allocate
double* data1D = (double*) malloc(num_rows * num_cols * sizeof(double));

It is smaller but you can't just reference it via
data[i][j]
you have to reference if via a formula
data1D[j*num_cols + i]

So there is a trade off being made between memory size and ease of indexing.
The flat 1D array is usually faster if you know you pointer arithmetic but more prone to beginner error.

It's clearly a M x N matrix and most commercial programmers would flatten it to a 1D array because you are likely going to do matrix arithmetic and there are tricks you can use with a flat 1D array and memmove.
In vino veritas


modified 25-Apr-19 14:22pm.

GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
zak10025-Apr-19 8:56
zak10025-Apr-19 8:56 
AnswerRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
ChrisFromWales30-Apr-19 23:37
ChrisFromWales30-Apr-19 23:37 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
Stefan_Lang4-Jun-19 22:01
Stefan_Lang4-Jun-19 22:01 
Questionprocessing Multithread in MFC Pin
Member 1422104124-Apr-19 18:41
Member 1422104124-Apr-19 18:41 
AnswerRe: processing Multithread in MFC Pin
Victor Nijegorodov24-Apr-19 20:36
Victor Nijegorodov24-Apr-19 20:36 
AnswerRe: processing Multithread in MFC Pin
Richard MacCutchan24-Apr-19 21:31
mveRichard MacCutchan24-Apr-19 21:31 
QuestionConversion from- C++ to C-language Pin
zak10024-Apr-19 16:28
zak10024-Apr-19 16:28 
QuestionRe: Conversion from- C++ to C-language Pin
David Crow24-Apr-19 16:59
David Crow24-Apr-19 16:59 
AnswerRe: Conversion from- C++ to C-language Pin
zak10024-Apr-19 18:04
zak10024-Apr-19 18:04 
GeneralRe: Conversion from- C++ to C-language Pin
David Crow25-Apr-19 2:11
David Crow25-Apr-19 2:11 
GeneralRe: Conversion from- C++ to C-language Pin
leon de boer25-Apr-19 2:20
leon de boer25-Apr-19 2:20 
QuestionPreprocessor Directives in C Program Pin
Aakashdata22-Apr-19 22:57
Aakashdata22-Apr-19 22:57 
AnswerRe: Preprocessor Directives in C Program Pin
Renuka peshwani22-Apr-19 23:16
Renuka peshwani22-Apr-19 23:16 
AnswerRe: Preprocessor Directives in C Program Pin
Richard MacCutchan22-Apr-19 23:43
mveRichard MacCutchan22-Apr-19 23:43 
AnswerRe: Preprocessor Directives in C Program Pin
leon de boer23-Apr-19 6:55
leon de boer23-Apr-19 6:55 
GeneralRe: Preprocessor Directives in C Program Pin
k505423-Apr-19 7:01
mvek505423-Apr-19 7:01 
GeneralRe: Preprocessor Directives in C Program Pin
leon de boer23-Apr-19 7:15
leon de boer23-Apr-19 7:15 

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.