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...
// An extendable string typedef struct string { char *text; int len; int buflen; }String; // Amount to allocate at a time #define N 8 // Internal function
String* catstr(String *s,const char *addstr,int len){
  ...
» if (s==NULL) {// Create a new String » s=xmalloc(sizeof *s); » s->buflen=s->len=0; » s->text=NULL; } » if (addstr==NULL) // Just create a new empty String return s; » if (len+s->len>=s->buflen) {// Extend text buffer » s->buflen=1+len+s->len; » s->buflen+=N-s->buflen%N;// Make it a multiple of N » s->text=realloc(s->text, s->buflen); } » strcpy(s->text+s->len, addstr); » s->len+=len; » return s;
} String* Strcat(String *s,const char *addstr){
  ...
» int n=0; » if (addstr!=NULL) n=strlen(addstr); » return catstr(s, addstr, n);
} String* StrCat(String *s,const String *addstr){
  ...
» int n=0; » if (addstr!=NULL) n=addstr->len; » return catstr(s, addstr->text, n);
} int main(){
  ...
» String *s1=NULL, *s2=NULL; » s1=Strcat(s1, "Hello"); » s2=Strcat(s2, " world"); » StrCat(s1, s2); » printf("%s\n", s1->text); » return 1;
}
Memory

Fixed strings

135
'H'
136
137
138
139
140
141
142
143
'e' 'l' 'l' 'o' '\0'
?
?
?
152
153
154
155
156
157
158
159
?
?
?
?
' ' 'w' 'o' 'r'
160
161
162
163
164
165
166
167
'l' 'd' '\0'
?
?
?
?
'%'
168
169
170
's' '\n' '\0'

Allocated memory

304
305
306
307
308
309
310
311
304.text = 2528109213
304.len = 1446702668
312
313
314
315
304.buflen = 15692560

320
321
322
323
324
325
326
327
'\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'

336
337
338
339
340
341
342
343
336.text = 225871525
336.len = 1980347605
344
345
346
347
336.buflen = 3250130

352
353
354
355
356
357
358
359
'\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'

368
369
370
371
372
373
374
375
'H' 'e' 'l' 'l' 'o' '\0' '\0' '\0'
376
377
378
379
380
381
382
383
'\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'

catstr()

640
641
642
643
644
645
646
647
s = -4138359064
addstr = -4138359016
648
649
650
651
len = 6

catstr()

640
641
642
643
644
645
646
647
s = NULL
addstr = -4155067057
648
649
650
651
len = 5

catstr()

640
641
642
643
644
645
646
647
s = NULL
addstr = -4155067036
648
649
650
651
len = 6

StrCat()

664
665
666
667
668
669
670
671
addstr = -4138359032
s = -4138359064
680
681
682
683
n = 0

Strcat()

664
665
666
667
668
669
670
671
addstr = -4155067036
s = NULL
680
681
682
683
n = 0

Strcat()

664
665
666
667
668
669
670
671
addstr = -4155067057
s = NULL
680
681
682
683
n = 0

main()

724
725
726
727
s1 = NULL
728
729
730
731
s2 = NULL

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

The actual address of allocated memory is the
address shown plus 151249320 (0x903E1A8).

Show Advanced options

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

Output

Hello world
                                                                                                                                                                                                                                                                       

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