Skip to content
EMPS intranet

Back to top
Hi!   Hi!           Start program

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...
// 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] = 0

160
161
162
163
164
165
166
167
160[0] = 0
160[1] = 0

176
177
178
179
180
181
182
183
176[0] = 0
176[1] = 0
184
185
186
187
176[2] = 0

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
216[1] = 0
224
225
226
227
228
229
230
231
216[2] = 0
216[3] = 0

resizesymarray()

364
365
366
367
p = -4153590976
376
377
378
379
380
381
382
383
i = -4278200
?
?
?
?
416
417
418
419
420
421
422
423
?
?
?
?
oldm = 4
424
425
426
427
newm = 0

resizesymarray()

364
365
366
367
p = NULL
376
377
378
379
380
381
382
383
i = -4278200
?
?
?
?
416
417
418
419
420
421
422
423
?
?
?
?
oldm = 0
424
425
426
427
newm = 3

resizesymarray()

412
413
414
415
p = -4153591040
424
425
426
427
428
429
430
431
i = -4278136
?
?
?
?
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 = -4153590976
m = 4

main()

496
497
498
499
500
501
502
503
p = NULL
n = 1742143121
504
505
506
507
newn = -1767505106

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

The actual address of allocated memory is the
address shown plus 137097472 (0x82BF100).

.

Input was:

3 4

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

Output

Size of array (>0)?
New size of array (>0)?
                                                                                                                                                                                                                                                                       

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