/*
* Free Particle "togo" and return the new first Particle
* "first" must be the existing first member of the list
*/
struct particle* freeparticle(Particle
*togo,Particle
*first){
» Particle
*parent;
» if (
togo==NULL) // Do nothing
return
first;
else if (
first==NULL) {
» fprintf(stderr, "Trying to remove something from an empty list!\n");
» return
NULL;
}
» if (
togo==first) // Is it the first one?
first=
first->next;
else {// Find togo's predecessor:
» for (
parent=
first;
parent->next!=togo;
parent=
parent->next) {
» if (
parent->next==NULL) {
» fprintf(stderr, "%p isn't in the list!\n", togo);
» return
first;
}
}
» parent->next=
togo->next;
}
» free(togo);
» return
first;
}
...
int main(){
» int
i,
n;
» Particle
*first=NULL;
» printf("How many structures would you like?\n");
» scanf("%d", &n);
» for (
i=
0;
i<n; ++
i)
first=
newparticle(first);
// Demonstrate some cases of freeing a particle
» Particle
*killme=first->next->next;
» first=
freeparticle(killme, first);
» killme=
first;
» first=
freeparticle(killme, first);
» killme=
NULL;
» first=
freeparticle(killme, first);
» for ( Particle
*p=first;
p!=NULL;
p=
p->next)
printf("Mass: %f position: (%g, %g)\n", p->mass, p->x[0], p->x[1]);
» return
0;
}
...
Memory
Allocated memory
|
|
128 | 129 | 130 | 131
| 132 | 133 | 134 | 135
|
128.x[0] =
0
|
128.x[1] =
0
|
136 | 137 | 138 | 139
| 140 | 141 | 142 | 143
|
128.mass =
0
|
128.next =
NULL
|
|
|
152 | 153 | 154 | 155
| 156 | 157 | 158 | 159
|
152.x[0] =
0
|
152.x[1] =
0
|
160 | 161 | 162 | 163
| 164 | 165 | 166 | 167
|
152.mass =
0
|
152.next =
NULL
|
|
|
176 | 177 | 178 | 179
| 180 | 181 | 182 | 183
|
176.x[0] =
0
|
176.x[1] =
0
|
184 | 185 | 186 | 187
| 188 | 189 | 190 | 191
|
176.mass =
0
|
176.next =
NULL
|
|
|
200 | 201 | 202 | 203
| 204 | 205 | 206 | 207
|
200.x[0] =
0
|
200.x[1] =
0
|
208 | 209 | 210 | 211
| 212 | 213 | 214 | 215
|
200.mass =
0
|
200.next =
NULL
|
|
freeparticle()
|
344 | 345 | 346 | 347
| 348 | 349 | 350 | 351
|
first =
-4136499624
|
togo =
-4136499624
|
360 | 361 | 362 | 363
| | | | |
parent =
488
| | | | |
|
freeparticle()
|
344 | 345 | 346 | 347
| 348 | 349 | 350 | 351
|
first =
-4136499624
|
togo =
-4136499672
|
360 | 361 | 362 | 363
| | | | |
parent =
488
| | | | |
|
freeparticle()
|
344 | 345 | 346 | 347
| 348 | 349 | 350 | 351
|
first =
-4136499648
|
togo =
NULL
|
360 | 361 | 362 | 363
| | | | |
parent =
488
| | | | |
|
newparticle()
|
| | | | 364 | 365 | 366 | 367
|
| | | |
first =
-4136499672
|
376 | 377 | 378 | 379
| | | | |
p =
-4291709806
| | | | |
|
newparticle()
|
| | | | 364 | 365 | 366 | 367
|
| | | |
first =
-4136499696
|
376 | 377 | 378 | 379
| | | | |
p =
-4291709806
| | | | |
|
newparticle()
|
| | | | 364 | 365 | 366 | 367
|
| | | |
first =
NULL
|
376 | 377 | 378 | 379
| | | | |
p =
-4291709806
| | | | |
|
newparticle()
|
| | | | 364 | 365 | 366 | 367
|
| | | |
first =
-4136499648
|
376 | 377 | 378 | 379
| | | | |
p =
-4291709806
| | | | |
|
main()
|
456 | 457 | 458 | 459
| 460 | 461 | 462 | 463
|
i =
-144858627
|
n =
527344087
|
464 | 465 | 466 | 467
| 468 | 469 | 470 | 471
|
first =
NULL
|
killme =
-4136499672
|
|
freeparticle(): for()
|
472 | 473 | 474 | 475
| | | | |
p =
-4136499648
| | | | |
NB: the actual memory address of each variable is
the address shown plus 4291709808 (0xFFCE4B70).
The actual address of allocated memory is
the address shown plus 155209984 (0x9405100).
|