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...
// INCOMPLETE // Very simple binary tree #define N 8 typedef struct tree { struct tree *before; struct tree *after; char string[N]; }Tree; // Add a node to the tree, return the base of the tree;
Tree* addstring(Tree *base,const char *string){
  ...
» if (base) { » if (strcmp(string, base->string)==0) { » fprintf(stderr, "Repeated string %s\n", string); » return base; } » if (base->before!=NULL&&strcmp(string, base->before->string)<0) { » addstring(base->before, string); » return base; } else if (base->after!=NULL&&strcmp(string, base->after->string)>0) { » addstring(base->after, string); » return base; } } // We need to put a new entry here » Tree *tmp; » tmp=xmalloc(sizeof *tmp); » tmp->before=tmp->after=NULL; » tmp->string(N-1)='\0'; » if (base==NULL) {// First node in tree » strncpy(tmp->string, string, N-1); » return tmp; } » if (strcmp(string, base->string)>0) { // new word is after base » if (base->after==NULL) { // Insert new word beneath » base->after=tmp; » strncpy(tmp->string, string, N-1); } else { // Put new text in current base node, insert tmp beneath and before it » strncpy(tmp->string, base->string, N-1); » strncpy(base->string, string, N-1); » tmp->before=base->before; » base->before=tmp; } } else { » if (base->before==NULL) { » base->before=tmp; » strncpy(tmp->string, string, N-1); } else { » strncpy(tmp->string, base->string, N-1); » strncpy(base->string, string, N-1); » tmp->after=base->after; » base->after=tmp; } } » return base;
} int main(){
  ...
» Tree *t=NULL; » t=addstring(t, "Hello"); » t=addstring(t, "world"); » t=addstring(t, "Ahh"); » t=addstring(t, "Mmmm.."); » return 0;
}
Memory

Fixed strings

130
131
132
133
134
135
'H' 'e' 'l' 'l' 'o' '\0'
136
137
138
139
140
141
142
143
?
?
?
?
'w' 'o' 'r' 'l'
144
145
146
147
148
149
150
151
'd' '\0'
?
?
?
?
'A' 'h'
152
153
154
155
156
157
158
159
'h' '\0'
?
?
?
?
'M' 'm'
160
161
162
163
164
'm' 'm' '.' '.' '\0'

Allocated memory

296
297
298
299
300
301
302
303
296.before = 3140302828
296.after = 1733530337
304
305
306
307
308
309
310
311
296.string = ' ' ' ' ' ' ' ' 'o' ' ' ' ' '\0'

320
321
322
323
324
325
326
327
320.before = 2112388157
320.after = 2634380958
328
329
330
331
332
333
334
335
320.string = 'q' 'v' '6' ' ' ' ' ' ' 'D' '\0'

344
345
346
347
348
349
350
351
344.before = 3978832860
344.after = 3184638699
352
353
354
355
356
357
358
359
344.string = 'F' ' ' ' ' 'A' ']' ' ' ' ' '\0'

368
369
370
371
372
373
374
375
368.before = 3796430119
368.after = 1052379889
376
377
378
379
380
381
382
383
368.string = 'N' 'a' ' ' ' ' ' ' 'z' ' ' '\0'

addstring()

640
641
642
643
644
645
646
647
string = -4155752306
base = -4133468272
656
657
658
659
tmp = 736

addstring()

640
641
642
643
644
645
646
647
string = -4155752334
base = NULL
656
657
658
659
tmp = 736

addstring()

640
641
642
643
644
645
646
647
string = -4155752314
base = -4133468272
656
657
658
659
tmp = 736

addstring()

640
641
642
643
644
645
646
647
string = -4155752324
base = -4133468272
656
657
658
659
tmp = 736

main()

720
721
722
723
t = NULL

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

The actual address of allocated memory is the
address shown plus 156828080 (0x95901B0).

Show Advanced options

.
                                                                                                                                                                                                                                                                       

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