-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_memory.c
81 lines (67 loc) · 2.31 KB
/
array_memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# include<stdio.h>
# include<stdlib.h>
int main(void){
int row_number; /* This is just the number of rows in the array */
int col_number; /* This is just the number of cols in the array */
int i, j; /* Just an index */
double **array; /* Initialises a pointer to a pointer of doubles */
row_number = 4;
col_number = 5;
/* First allocate memory to store the addresses of rows */
array = malloc(row_number * sizeof(int *));
/* Note that the values of 'array' need to be stored somewhere */
printf("Addresses where the addresses of the array row locations are: \n");
for(i = 0; i < row_number; i++){
printf("Row %d: \t %p \n", i, &array + i);
}
printf("\n");
/* But these values of 'array' hold the addresses for the row locations */
printf("Addresses to where the rows of array are located: \n");
for(i = 0; i < row_number; i++){
printf("Row %d: \t %p \n", i, array + i);
}
printf("\n");
/* And we can now allocate some memory to each of these addresses */
/* Note that the contents of array[i] are *also* addresses */
for(i = 0; i < row_number; i++){
array[i] = malloc(col_number * sizeof(double));
}
/* And so here are the locations where the row values *actually* start */
printf("The array[i] is the address for where the row actually starts \n");
for(i = 0; i < row_number; i++){
printf("Row %d: \t %p \n", i, array[i]);
}
printf("\n");
/* Assign some values -- just i plus j for each */
for(i = 0; i < row_number; i++){
for(j = 0; j < col_number; j++){
array[i][j] = i + j;
}
}
/* Now let's print the values */
printf("The actual values of the array: \n");
for(i = 0; i < row_number; i++){
for(j = 0; j < col_number; j++){
printf("%f\t", array[i][j]);
}
printf("\n");
}
printf("\n");
/* But take a look at where those values are stored */
printf("Where each value in the array is stored: \n");
for(i = 0; i < row_number; i++){
for(j = 0; j < col_number; j++){
printf("%p\t", &array[i][j]);
}
printf("\n");
}
/* This is why we now need to free each row *and* the whole 'array' */
for(i = 0; i < row_number; i++){
free(array[i]);
}
free(array);
return 0;
}
/*
* To compile: gcc -Wall array_memory.c -o array_memory
*/