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...
// Demonstrate a growable array // - NB This code contains an unnecessary call to malloc() // - to simulate the behavior of realloc() in a larger, // - more realistic program whether there is other // - dynamic memory allocation # define GROWBY 4 // Growable array of floats typedef struct floatarray { float *values; int nvals; int buflen; }Floatarray; // Allocate a new growable array
Floatarray* newfloatarray(void){
  ...
» Floatarray *f; » f=xmalloc(sizeof *f);// NB xmalloc checks for NULL » f->buflen=f->nvals=0; » f->values=NULL; » return f;
} // Add a value to an array, extending it if necessary void addtoarray(Floatarray *f,float value){
  ...
// Check to see if we need to extend the array » if (f->nvals==f->buflen) { » f->buflen+=GROWBY; » f->values=realloc(f->values, f->buflen*sizeof f->values); } » f->values(f->nvals)=value; » f->nvals++;
} void print2 (Floatarray **ar2); int main(){
  ...
» Floatarray **fars=NULL; » float value; » int kmax=-1, k; » printf("Just keep typing in pairs of numbers!\n"); » while (scanf("%d %g", &k, &value)==2) { » if (k>kmax) { » fars=realloc(fars, (k+2)*sizeof *fars); » while (kmax<k) { » ++kmax; » farskmax=newfloatarray(); } » fars(kmax+1)=NULL; } » addtoarray(farsk, value); } » print2(fars); » return 0;
} // Print one "array" (accepts Floatarray) void print1(Floatarray *far){
  ...
» printf("The array (%p) has %d elements\n", far, far->nvals); » for ( int i=0;i<far->nvals; ++i) printf("%d: %g\n", i, far->values[i]);
} // Print out a 2-D "array" void print2(Floatarray **ar2){
  ...
» while (*ar2) { » print1(*ar2); » ar2++; » printf("\n"); }
}
Memory

Allocated memory

128
129
130
131
132
133
134
135
128.values = 703740115
128.nvals = 411428931
136
137
138
139
128.buflen = 14322486

128
129
130
131
132
133
134
135
128[0] =
128[1] =
136
137
138
139
128[2] =

144
145
146
147
148
149
150
151
144.values = 1157731824
144.nvals = 1003484976
152
153
154
155
144.buflen = 1094611

160
161
162
163
164
165
166
167
160.values = 7335120
160.nvals = -954957280
168
169
170
171
160.buflen = 2465599

176
177
178
179
180
181
182
183
176[0] = 0
176[1] = 0
184
185
186
187
188
189
190
191
176[2] = 0
176[3] = 0

200
201
202
203
204
205
206
207
200[0] = 144
200[1] = 160
208
209
210
211
212
213
214
215
200[2] =
200[3] =
216
217
218
219
200[4] =

224
225
226
227
228
229
230
231
224.values = 2386535480
224.nvals = -1164521099
232
233
234
235
224.buflen = 12389894

240
241
242
243
244
245
246
247
240[0] = 0
240[1] = 0
248
249
250
251
252
253
254
255
240[2] = 0
240[3] = 0

264
265
266
267
268
269
270
271
264[0] = 0
264[1] = 0
272
273
274
275
276
277
278
279
264[2] = 0
264[3] = 0

print1()

412
413
414
415
far = -4148849016

print1()

412
413
414
415
far = -4148848936

print1()

412
413
414
415
far = -4148849000

print1()

412
413
414
415
far = -4148849032

print1(): for()

424
425
426
427
i = 0

print1(): for()

424
425
426
427
i = 0

print1(): for()

424
425
426
427
i = 0

print1(): for()

424
425
426
427
i = 0

newfloatarray()

472
473
474
475
f = -4155851653

newfloatarray()

472
473
474
475
f = -4155851653

newfloatarray()

472
473
474
475
f = -4155851653

newfloatarray()

472
473
474
475
f = -4155851653

print2()

496
497
498
499
ar2 = -4148848960

addtoarray()

496
497
498
499
500
501
502
503
f = -4148849032
value = 7.0999999

addtoarray()

496
497
498
499
500
501
502
503
f = -4148849000
value = 0.100000001

addtoarray()

496
497
498
499
500
501
502
503
f = -4148848936
value = 4.19999981

main()

540
541
542
543
fars = NULL
544
545
546
547
548
549
550
551
value = -4.56633712e+33
kmax = -1
552
553
554
555
k = 1

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

The actual address of allocated memory is the
address shown plus 141546088 (0x86FD268).

Show Advanced options

.

Input was:

1 .1 3 4.2 2 7.1

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

Output

Just keep typing in pairs of numbers!
The array (0x86fd2f8) has 0 elements

The array (0x86fd308) has 1 elements
0: 0.1

The array (0x86fd2e8) has 1 elements
0: 7.1

The array (0x86fd348) has 1 elements
0: 4.2

                                                                                                                                                                                                                                                                       

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