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...
// Demonstrate a growable array # 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++; }
...
int main(){ » Floatarray *far; » float value; » far=newfloatarray(); » printf("Just keep typing in numbers!\n"); » while (scanf("%g", &value)==1) { » addtoarray(far, value); } » printf("The array has %d elements\n", far->nvals); » for ( int i=0;i<far->nvals; ++i) printf("%d: %g\n", i, far->values[i]); » return 0; }
...
Memory

Allocated memory

128
129
130
131
132
133
134
135
128.values = 3794690357
128.nvals = -1423001586
136
137
138
139
128.buflen = 236925822

8352
8353
8354
8355
8356
8357
8358
8359
8352[0] = 1.1
8352[1] = 2.2
8360
8361
8362
8363
8364
8365
8366
8367
8352[2] = 3.3
8352[3] = 4.4
8368
8369
8370
8371
8372
8373
8374
8375
8352[4] = 5.5
8352[5] = 6.6
8376
8377
8378
8379
8380
8381
8382
8383
8352[6] = 7.7
8352[7] = 8.8
8384
8385
8386
8387
8388
8389
8390
8391
8352[8] = 0
8352[9] = 1.71588e-40
8392
8393
8394
8395
8396
8397
8398
8399
8352[10] = 0
8352[11] = 0

8352
8353
8354
8355
8356
8357
8358
8359
8352[0] = 0
8352[1] = 0
8360
8361
8362
8363
8364
8365
8366
8367
8352[2] = 0
8352[3] = 0

8352
8353
8354
8355
8356
8357
8358
8359
8352[0] = 1.1
8352[1] = 2.2
8360
8361
8362
8363
8364
8365
8366
8367
8352[2] = 3.3
8352[3] = 4.4
8368
8369
8370
8371
8372
8373
8374
8375
8352[4] = 0
8352[5] = 1.7161e-40
8376
8377
8378
8379
8380
8381
8382
8383
8352[6] = 0
8352[7] = 0

newfloatarray()

8528
8529
8530
8531
f = -4156906465

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 3.29999995

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 5.5

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 2.20000005

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 4.4000001

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 8.80000019

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 1.10000002

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 7.69999981

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 6.5999999

addtoarray()

8552
8553
8554
8555
8556
8557
8558
8559
f = -4153375496
value = 9.89999962

main()

8584
8585
8586
8587
8588
8589
8590
8591
far = -3425518884
value = -1.544652e+17

addtoarray(): for()

8592
8593
8594
8595
i = 0

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

The actual address of allocated memory is the
address shown plus 138064112 (0x83AB0F0).

Show Advanced options

.

Input was:

1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9

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 numbers!
The array has 9 elements
0: 1.1
1: 2.2
2: 3.3
3: 4.4
4: 5.5
5: 6.6
6: 7.7
7: 8.8
8: 9.9
                                                                                                                                                                                                                                                                       

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