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=len+s->len;// New string length » 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 *s=NULL; » s=Strcat(s, "Hello"); » Strcat(s, " world"); » printf("%s\n", s->text); » return 1;
}
Memory

Fixed strings

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

Allocated memory

296
297
298
299
300
301
302
303
296.text = 2711028638
296.len = -1532828464
304
305
306
307
296.buflen = 12450955

312
313
314
315
316
317
318
319
'\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'

312
313
314
315
316
317
318
319
'H' 'e' 'l' 'l' 'o' '\0' '\0' '\0'
320
321
322
323
324
325
326
327
'\0' '\0' '\0' '\0' ' ' ' ' ' ' '\0'

catstr()

584
585
586
587
588
589
590
591
s = NULL
addstr = -4157659138
592
593
594
595
len = 5

catstr()

584
585
586
587
588
589
590
591
s = -4151166192
addstr = -4157659118
592
593
594
595
len = 6

Strcat()

608
609
610
611
612
613
614
615
addstr = -4157659118
s = -4151166192
624
625
626
627
n = 0

Strcat()

608
609
610
611
612
613
614
615
addstr = -4157659138
s = NULL
624
625
626
627
n = 0

main()

672
673
674
675
s = NULL

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

The actual address of allocated memory is the
address shown plus 141033904 (0x86801B0).

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