Code
Header...
// Shrink or extend a symmetric array
float * * resizesymarray(float * *p,int oldm,int newm){ ...
» int i, siz;
» float *dat=NULL;
» siz= newm*( newm+1) /2;
» if ( p!=NULL)
dat= p0;
» dat= realloc(dat, siz*sizeof *dat);
» p= realloc(p, newm*sizeof *p);
» for ( i= 0; i<newm; ++ i)
pi= dat+i*( i+1) /2;
» 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] =
0
|
128[1] =
0
|
136 | 137 | 138 | 139
| 140 | 141 | 142 | 143
|
128[2] =
0
|
128[3] =
0
|
144 | 145 | 146 | 147
| 148 | 149 | 150 | 151
|
128[4] =
0
|
128[5] =
0
|
|
|
160 | 161 | 162 | 163
| 164 | 165 | 166 | 167
|
160[0] =
|
160[1] =
|
168 | 169 | 170 | 171
| | | | |
160[2] =
| | | | |
|
|
176 | 177 | 178 | 179
| 180 | 181 | 182 | 183
|
176[0] =
17
|
176[1] =
0
|
184 | 185 | 186 | 187
| 188 | 189 | 190 | 191
|
176[2] =
0
|
176[3] =
0
|
192 | 193 | 194 | 195
| 196 | 197 | 198 | 199
|
176[4] =
11.3
|
176[5] =
0
|
200 | 201 | 202 | 203
| 204 | 205 | 206 | 207
|
176[6] =
0
|
176[7] =
0
|
208 | 209 | 210 | 211
| 212 | 213 | 214 | 215
|
176[8] =
0
|
176[9] =
0
|
|
|
224 | 225 | 226 | 227
| 228 | 229 | 230 | 231
|
224[0] =
128
|
224[1] =
132
|
232 | 233 | 234 | 235
| 236 | 237 | 238 | 239
|
224[2] =
140
|
224[3] =
|
|
resizesymarray()
|
| | | | 372 | 373 | 374 | 375
|
| | | |
p =
-4130605456
|
376 | 377 | 378 | 379
| 380 | 381 | 382 | 383
|
i =
0
|
siz =
2
|
384 | 385 | 386 | 387
| 388
| 389
| 390
| 391
|
dat =
NULL
| ?
| ?
| ?
| ?
|
424
| 425
| 426
| 427
| 428 | 429 | 430 | 431
|
?
| ?
| ?
| ?
|
oldm =
4
|
432 | 433 | 434 | 435
| | | | |
newm =
0
| | | | |
|
resizesymarray()
|
| | | | 372 | 373 | 374 | 375
|
| | | |
p =
NULL
|
376 | 377 | 378 | 379
| 380 | 381 | 382 | 383
|
i =
3
|
siz =
161230856
|
384 | 385 | 386 | 387
| 388
| 389
| 390
| 391
|
dat =
NULL
| ?
| ?
| ?
| ?
|
424
| 425
| 426
| 427
| 428 | 429 | 430 | 431
|
?
| ?
| ?
| ?
|
oldm =
0
|
432 | 433 | 434 | 435
| | | | |
newm =
3
| | | | |
|
resizesymarray()
|
| | | | 420 | 421 | 422 | 423
|
| | | |
p =
-4130605520
|
424 | 425 | 426 | 427
| 428 | 429 | 430 | 431
|
i =
4
|
siz =
2
|
432 | 433 | 434 | 435
| 436
| 437
| 438
| 439
|
dat =
NULL
| ?
| ?
| ?
| ?
|
472
| 473
| 474
| 475
| 476 | 477 | 478 | 479
|
?
| ?
| ?
| ?
|
oldm =
3
|
480 | 481 | 482 | 483
| | | | |
newm =
4
| | | | |
|
freesymarray()
|
472 | 473 | 474 | 475
| 476 | 477 | 478 | 479
|
p =
-4130605456
|
m =
4
|
|
newsymarray()
|
472 | 473 | 474 | 475
| | | | |
m =
3
| | | | |
|
main()
|
504 | 505 | 506 | 507
| 508 | 509 | 510 | 511
|
p =
NULL
|
n =
-142913536
|
512 | 513 | 514 | 515
| | | | |
newn =
1
| | | | |
|
NB: the actual memory address of each variable is
the address shown plus 4291853528 (0xFFD07CD8).
The actual address of allocated memory is
the address shown plus 161247848 (0x99C7268).
|