c - What are the differences between *ptr and **ptr? -


i coding 3d array using triple pointers malloc. replaced *ptrdate in (a), *ptrdate[i], , *ptrdate[i] *ptrdate in code below since pointers of type date access in different dimension. got same results both ways.

question: what's difference when used operand of sizeof?

typedef struct {     int day; } date;  int main(){   int i, j, k, count=0;   int row=3, col=4, dep=5;    date ***ptrdate = malloc(row * sizeof *ptrdate); //(a)   (i=0; i<row; i++) {     ptrdate[i] = malloc(col * sizeof *ptrdate[i]); //(b)     (j=0; j<col; j++) {       ptrdate[i][j] = malloc(dep * sizeof *ptrdate[i][j]); //(c)     }   } 

i coding 3d array using triple pointers malloc.

first of all, there no need array allocated using more 1 call malloc. in fact, incorrect so, word "array" considered denote single block of contiguous memory, i.e. one allocation. i'll later, first, question:

question: what's difference when used operand of sizeof?

the answer, though obvious, misunderstood. they're different pointer types, coincidentally have same size , representation on system... might have different sizes , representations on other systems. important keep possibility in mind, can sure code portable possible.

given size_t row=3, col=4, dep=5;, can declare array so: date array[row][col][dep];. know have no use such declaration in question... bear me moment. if printf("%zu\n", sizeof array);, it'll print row * col * dep * sizeof (date). knows full size of array, including of dimensions... , exactly how many bytes required when allocating such array.

printf("%zu\n", sizeof ptrdate); ptrdate declared in code produce entirely different, though... it'll produce size of pointer (to pointer pointer date, not confused pointer date or pointer pointer date) on system. of size information, regarding number of dimensions (e.g. row * col * dep multiplication) lost, because haven't told our pointers maintain size information. can still find sizeof (date) using sizeof *ptrdate, though, because we've told our code keep size information associated pointer type.

what if tell our pointers maintain other size information (the dimensions), though? if write ptrdate = malloc(row * sizeof *ptrdate);, , have sizeof *ptrdate equal col * dep * sizeof (date)? simplify allocation, wouldn't it?

this brings introduction: there way perform of allocation using 1 single malloc. it's simple pattern remember, difficult pattern understand (and appropriate ask question about):

date (*ptrdate)[col][dep] = malloc(row * sizeof *ptrdate); 

suffice say, usage still same. can still use ptrdate[x][y][z]... there 1 thing doesn't seem quite right, though, , sizeof ptrdate still yields size of pointer (to array[col][dep] of date) , sizeof *ptrdate doesn't contain row dimension (hence multiplication in malloc above. i'll leave exercise work out whether solution necessary that...

free(ptrdate); // ooops! must remember free memory have allocated! 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -