Code
Header...
// Shrink or extend a symmetric array
float * * resizesymarray(float * *p,int oldm,int newm){ ...
» int i;
// Is the new one smaller?
» for ( i= newm; i<oldm; ++ i)
free(pi);
» p= realloc(p, newm*sizeof *p);
// Is the new one bigger?
» for ( i= oldm; i<newm; ++ i)
pi= malloc((i+1)*sizeof *p[i]);
» return p;
}
// Create a new symmetric array
float * * newsymarray(int m){ ...
» return resizesymarray(NULL, 0, m);
}
// Free a symmetric array
void freesymarray(float * *p,int m){ ...
» resizesymarray(p, m, 0);
}
int main(){ ...
» float * *p=NULL;
» int n, newn;
» printf("Size of array (>0)?\n");
» scanf("%i", &n);
» p= newsymarray(n);
» p[0]0= 17;
» p[n-1]1= 11.3;
» printf("New size of array (>0)?\n");
» scanf("%i", &newn);
» p= resizesymarray(p, n, newn);
// Do stuff here
» freesymarray(p, newn);
// Do more stuff not requiring p
» return 0;
}
|
Memory
Allocated memory
|
|
128 | 129 | 130 | 131
| 132 | 133 | 134 | 135
|
128[0] =
|
128[1] =
|
136 | 137 | 138 | 139
| | | | |
128[2] =
| | | | |
|
|
144 | 145 | 146 | 147
| | | | |
144[0] =
2.08086e-39
| | | | |
|
|
160 | 161 | 162 | 163
| 164 | 165 | 166 | 167
|
160[0] =
-1.19109e-08
|
160[1] =
2.11344e-38
|
|
|
176 | 177 | 178 | 179
| 180 | 181 | 182 | 183
|
176[0] =
-0.00148442
|
176[1] =
1.88526e-37
|
184 | 185 | 186 | 187
| | | | |
176[2] =
1.41962e-38
| | | | |
|
|
192 | 193 | 194 | 195
| 196 | 197 | 198 | 199
|
192[0] =
144
|
192[1] =
160
|
200 | 201 | 202 | 203
| 204 | 205 | 206 | 207
|
192[2] =
176
|
192[3] =
|
|
|
216 | 217 | 218 | 219
| 220 | 221 | 222 | 223
|
216[0] =
0.0057955
|
216[1] =
3.09474e+06
|
224 | 225 | 226 | 227
| 228 | 229 | 230 | 231
|
216[2] =
-2.06062e-07
|
216[3] =
9.32071e-39
|
|
resizesymarray()
|
| | | | 364 | 365 | 366 | 367
|
| | | |
p =
NULL
|
376 | 377 | 378 | 379
| 380
| 381
| 382
| 383
|
i =
-199736
| ?
| ?
| ?
| ?
|
416
| 417
| 418
| 419
| 420 | 421 | 422 | 423
|
?
| ?
| ?
| ?
|
oldm =
0
|
424 | 425 | 426 | 427
| | | | |
newm =
3
| | | | |
|
resizesymarray()
|
| | | | 364 | 365 | 366 | 367
|
| | | |
p =
-4153839320
|
376 | 377 | 378 | 379
| 380
| 381
| 382
| 383
|
i =
-199736
| ?
| ?
| ?
| ?
|
416
| 417
| 418
| 419
| 420 | 421 | 422 | 423
|
?
| ?
| ?
| ?
|
oldm =
4
|
424 | 425 | 426 | 427
| | | | |
newm =
0
| | | | |
|
resizesymarray()
|
| | | | 412 | 413 | 414 | 415
|
| | | |
p =
-4153839384
|
424 | 425 | 426 | 427
| 428
| 429
| 430
| 431
|
i =
-199656
| ?
| ?
| ?
| ?
|
464
| 465
| 466
| 467
| 468 | 469 | 470 | 471
|
?
| ?
| ?
| ?
|
oldm =
3
|
472 | 473 | 474 | 475
| | | | |
newm =
4
| | | | |
|
newsymarray()
|
464 | 465 | 466 | 467
| | | | |
m =
3
| | | | |
|
freesymarray()
|
464 | 465 | 466 | 467
| 468 | 469 | 470 | 471
|
p =
-4153839320
|
m =
4
|
|
main()
|
496 | 497 | 498 | 499
| 500 | 501 | 502 | 503
|
p =
NULL
|
n =
-143233024
|
504 | 505 | 506 | 507
| | | | |
newn =
1
| | | | |
|
NB: the actual memory address of each variable is
the address shown plus 4294767104 (0xFFFCF200).
The actual address of allocated memory is
the address shown plus 140927592 (0x8666268).
|