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++;
} int main(){
  ...
» Floatarray *far; » float value; » int *dummy;// - For dummy malloc() call » far=newfloatarray(); » printf("Just keep typing in numbers!\n"); » while (scanf("%g", &value)==1) { » addtoarray(far, value); » dummy=malloc(8);// - Simulate other calls to malloc } » 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 = 896200014
128.nvals = 291647075
136
137
138
139
128.buflen = 9130246

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

8376
8377
8378
8379
8380
8381
8382
8383
8376[0] = -825374577
8376[1] = 10

8392
8393
8394
8395
8396
8397
8398
8399
8392[0] = -389327533
8392[1] = 99670

8408
8409
8410
8411
8412
8413
8414
8415
8408[0] = 1948452412
8408[1] = 16201557

8424
8425
8426
8427
8428
8429
8430
8431
8424[0] = 39930422
8424[1] = 10574480

8440
8441
8442
8443
8444
8445
8446
8447
8440[0] = 1.1
8440[1] = 2.2
8448
8449
8450
8451
8452
8453
8454
8455
8440[2] = 3.3
8440[3] = 4.4
8456
8457
8458
8459
8460
8461
8462
8463
8440[4] = 0
8440[5] = 0
8464
8465
8466
8467
8468
8469
8470
8471
8440[6] = 0
8440[7] = 0

8480
8481
8482
8483
8484
8485
8486
8487
8480[0] = -508032927
8480[1] = 9863757

8496
8497
8498
8499
8500
8501
8502
8503
8496[0] = 896851745
8496[1] = 2542002

8512
8513
8514
8515
8516
8517
8518
8519
8512[0] = -1230825734
8512[1] = 14206809

8528
8529
8530
8531
8532
8533
8534
8535
8528[0] = -1653113715
8528[1] = 16294032

8544
8545
8546
8547
8548
8549
8550
8551
8544[0] = 1.1
8544[1] = 2.2
8552
8553
8554
8555
8556
8557
8558
8559
8544[2] = 3.3
8544[3] = 4.4
8560
8561
8562
8563
8564
8565
8566
8567
8544[4] = 5.5
8544[5] = 6.6
8568
8569
8570
8571
8572
8573
8574
8575
8544[6] = 7.7
8544[7] = 8.8
8576
8577
8578
8579
8580
8581
8582
8583
8544[8] = 0
8544[9] = 0
8584
8585
8586
8587
8588
8589
8590
8591
8544[10] = 0
8544[11] = 0

8600
8601
8602
8603
8604
8605
8606
8607
8600[0] = 599707720
8600[1] = 9051630

newfloatarray()

8736
8737
8738
8739
f = -4155735374

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 4.4000001

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 7.69999981

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 9.89999962

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 3.29999995

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 1.10000002

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 2.20000005

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 8.80000019

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 5.5

addtoarray()

8760
8761
8762
8763
8764
8765
8766
8767
f = -4131580208
value = 6.5999999

main()

8804
8805
8806
8807
far = -4290276160
8808
8809
8810
8811
8812
8813
8814
8815
value = -4.55239296e+33
dummy = -138591240

main(): for()

8816
8817
8818
8819
i = 0

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

The actual address of allocated memory is the
address shown plus 158696024 (0x9758258).

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