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 flawed attempt to dynamically * allocate an array of floats */ // Attempt to allocate an array of n floats // Contains a serious error
float * badArrayAlloc(int n){
...
float farray[n]; return farray;// Ooops!
} int main(){
...
int i, n; float *ar=NULL; printf("How many floats?\n"); scanf("%d", &n); ar=badArrayAlloc(n); for (i=0; i<n; ++i) ari=i; for (i=0; i<n; ++i) printf("%d %g\n", i, ari); return 0;
}
Memory

### badArrayAlloc()

128
129
130
131
132
133
134
135
farray[0] = 3.99837e-34
farray[1] = 3.99837e-34
136
137
138
139
140
141
142
143
farray[2] = 3.99837e-34
?
?
?
?
192
193
194
195
n = 3

### main()

228
229
230
231
ar = NULL
232
233
234
235
236
237
238
239
n = 1
i = -145132514

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

.

Input was:

3

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 floats?
0 -nan
1 -nan
2 -4.37473e+33

 Validate   Link-check © Copyright & disclaimer
Back to top