Maybe the new code can explain what I mean.
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i_size = 2, j_size = 5, k_size = 5, l_size = 6, m_size = 6;
int i = 0, j = 0, k = 0, l = 0, m = 0;
printf("ptr1\n");
int** ptr1 = (int**) malloc(sizeof(int*) * i_size);
for(i = 0; i < i_size; i++){
printf("%d\n", ptr1 + i);
}
printf("ptr2\n");
int** ptr2 = (int**) malloc(sizeof(int*) * j_size);
for(j = 0; j < j_size; j++){
printf("%d\n", ptr2 + j);
}
printf("ptr3\n");
int** ptr3 = (int**) malloc(sizeof(int*) * k_size);
for(k = 0; k < k_size; k++){
printf("%d\n", ptr3 + k);
}
printf("ptr4\n");
int** ptr4 = (int**) malloc(sizeof(int*) * l_size);
for(l = 0; l < l_size; l++){
printf("%d\n", ptr4 + l);
}
printf("ptr5\n");
int** ptr5 = (int**) malloc(sizeof(int*) * m_size);
for(m = 0; m < m_size; m++){
printf("%d\n", ptr5 + m);
}
free(ptr1);
free(ptr2);
free(ptr3);
free(ptr4);
free(ptr5);
return 0;
}
The result is as below:
It is funny that it seems for malloc
function, the minimun size of array is 32 bytes. And the OS seems to preserve 8 bytes space because when I found that for l_size
, it has 64 bytes. It means that if you want to malloc a 48-byte array, the OS will allocate more than 48-byte space.
update above
=====
My code is shown as below:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i_size = 5, j_size = 6, i = 0, j = 0;
printf("Addresses of two dimensional pointer\n");
int** ptr = (int**) malloc(sizeof(int*) * i_size);
printf("%d:\t%d\n", &ptr, ptr);
for(i = 0; i < i_size; i++){
*(ptr + i) = (int*) malloc(sizeof(int) * j_size);
}
for(i = 0; i < i_size; i++){
for(j = 0; j < j_size; j++){
*(*(ptr+i) + j) = i * 2 + j;
}
}
for(i = 0; i < i_size; i++){
printf("%d:\t%d\n", ptr + i, *(ptr + i));
}
printf("==\n");
for(i = 0; i < i_size; i++){
for(j = 0; j < j_size; j++){
printf("%d:\t%d\t%d\n",*(ptr + i) + j, *(*(ptr + i) + j), ptr[i][j]);
}
printf("==\n");
}
for(i = 0; i < i_size; i++){
free(*(ptr + i));
}
free(ptr);
return 0;
}
And the result as the folloing picture shows:
The code is running on the windows 10, 64-bit and compiled with TDM-GCC4.9.2 of Dev C++.
I am confused by the red block. Why is always 8 more bytes than the size of array. If I change the value of i_size
to 6, it seems that the OS will give 64 bytes to ptr
but not 48.
I expect that the value 12021664
should be 12021648
. If I change the value of i_size
to 7, it is ok:
But when the value of i_size
is 8, I expect the value 10710960
should be 10710944
. But it doesn't work.