Skip to content
EMPS intranet

Back to top
Hi!   Hi!           Start main()

Try stepping through the code


  Value and type of last evaluated expression: Address of array element: Address of array elementx:  (none)

Your browser does not support the canvas element which will make some the features unavailable.

If you are using Internet Explorer within the University of Exeter try going to the Settings menu (probably the gear shape at the top right of this page), selecting "Compatibility View settings", unchecking "Display intranet sites in Compatibility View" and reloading this page.

Code

 Header...
#define NDIM 2 typedef struct particle { float x[NDIM]; float mass; struct particle *next; }Particle; // Allocate a new particle and add it to the start of the list
struct particle* newparticle(Particle *first){
  ...
» Particle *p; » p=xmalloc(sizeof *p); » p->next=first; // Now initialise exactly as before » printf("Please enter the mass and x and y values.\n"); » scanf("%f %f %f", &p->mass, &p->x[0], &p->x[1]); » return p;
} /* * 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] = 5.52501e-05
128.x[1] = -2.17241e-37
136
137
138
139
140
141
142
143
128.mass = -5.62008754e-05
128.next = -133581559

152
153
154
155
156
157
158
159
152.x[0] = 2.1745e-35
152.x[1] = -2.7945e-12
160
161
162
163
164
165
166
167
152.mass = -1.48643849e-05
152.next = -122507084

176
177
178
179
180
181
182
183
176.x[0] = 0.000912958
176.x[1] = -1.82461e-17
184
185
186
187
188
189
190
191
176.mass = 3.09285571e-23
176.next = -120285301

200
201
202
203
204
205
206
207
200.x[0] = 1.98174e-09
200.x[1] = 1.76365e+33
208
209
210
211
212
213
214
215
200.mass = 1.18234488e-23
200.next = -133504118

freeparticle()

344
345
346
347
348
349
350
351
first = -4151122320
togo = -4151122368
360
361
362
363
parent = -4286815422

freeparticle()

344
345
346
347
348
349
350
351
first = -4151122320
togo = -4151122320
360
361
362
363
parent = -4286815422

freeparticle()

344
345
346
347
348
349
350
351
first = -4151122344
togo = NULL
360
361
362
363
parent = -4286815422

newparticle()

348
349
350
351
first = -4151122392
360
361
362
363
p = -4286815422

newparticle()

348
349
350
351
first = NULL
360
361
362
363
p = -4286815422

newparticle()

348
349
350
351
first = -4151122344
360
361
362
363
p = -4286815422

newparticle()

348
349
350
351
first = -4151122368
360
361
362
363
p = -4286815422

main()

456
457
458
459
460
461
462
463
i = 1
n = 200
464
465
466
467
468
469
470
471
first = NULL
killme = -4151122368

main(): for()

472
473
474
475
p = -4151122344

NB: the actual memory address of each variable is the
address shown plus 4286815424 (0xFF839CC0).

The actual address of allocated memory is the
address shown plus 135692904 (0x8168268).

Show Advanced options

.

Input was:

4 
4.1 2.4 7.7 
5.0 6.3 8.6 
3.6 4.3 2.91 
7.2 5.7 6.1

Show output (Before looking at the output, work out what you think it put should be and see if you are right.)

Output

How many structures would you like?
Please enter the mass and x and y values.
Please enter the mass and x and y values.
Please enter the mass and x and y values.
Please enter the mass and x and y values.
Mass: 3.600000 position: (4.3, 2.91)
Mass: 4.100000 position: (2.4, 7.7)
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top