5

I am reading a C course (it is dutch so probably you won't know) and there is a small exercise to understand string behaviour. Therefor i created a small C program to start the exercise but already the first output of my program is (for me) astonishing.

Source of my C program :

#include <string.h>
#include <stdio.h>

void printString(char *string)
{
    printf("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n");
    printf("%c ",string[0]);
    printf("%c ",string[1]);
    printf("%c ",string[2]);
    printf("%c ",string[3]);
    printf("%c ",string[4]);
    printf("%c ",string[5]);
    printf("%c ",string[6]);
    printf("%c ",string[7]);
    printf("%c ",string[8]);
    printf("%c ",string[9]);
    printf("%c  ",string[10]);
    printf("%c  ",string[11]);
    printf("%c  ",string[12]);
    printf("%c  ",string[13]);
    printf("%c  ",string[14]);
    printf("%c  ",string[15]);
    printf("%c  ",string[16]);
    printf("%d  ",string[17]);
    printf("%d  ",string[18]);
    printf("%d\n",string[19]);
}

void main(){

    char str[20];

    strcpy(str,"Dag grootmoeder!");
    printString(str);
}

I compiled with gcc (no special switches) and ran the program several times : (For the English speaking people Dag grootmoeder! == Hi grandma!)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

D a g   g r o o t m o  e  d  e  r  !    94  -90  111

$./oefString 

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

D a g   g r o o t m o  e  d  e  r  !    51  -12  96

$./oefString 

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

D a g   g r o o t m o  e  d  e  r  !    -17  -117  28

$./oefString 

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

D a g   g r o o t m o  e  d  e  r  !    96  15  -28

$./oefString 

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

D a g   g r o o t m o  e  d  e  r  !    -20  -46  -18

$./oefString 

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

D a g   g r o o t m o  e  d  e  r  !    68  -75  58

Here is the question :

1) Why do I get rubbish values for the last 3 indexes of str ? At first I was also printf()'ing them with %c and noticed the chars changed, that is why I used %d thereafter to display the integer values.

2) Why do these values change? I do nothing more then copying the same string using strcpy() into str.

Thx for taking time to read and even more thanx for those who respond !

Jorn

3 Answers 3

8

You're simply acessing the memory past the end of the string. You didn't fill it so you can find anything there - so that's why on each run you find something else.

In C "strings" are actually 0-terminated arrays (or pointers to memory). So if you print the characters using "%d" you will notice the very last element, right after !, is 0.

If you want it to be predictable, you can initialize your string before using it:

memset(str, 0, sizeof(str));

or

char str[20] = {0,};

As a side note, it's int main, not void main.

Sign up to request clarification or add additional context in comments.

6 Comments

* I figured i was seeing indeed memory but i am reserving 20bytes with the initialisation of char str[20]; (or so i assume). Is it then some memory optimisation to fill unused memory of my reserved 20bytes ?
thx, i noticed when disabling stack randomisation these values stay the same.echo "0" > /proc/sys/kernel/randomize_va_space
@JornDePril If you don't initialize your array it can contain anything.
about your side note. Isn't it 'int main' when having exit codes and 'void main' when not returning anything ?
@JornDePril Read about it here. Also this is funny enough :-)
|
0
char str[20];
...
strcpy(str,"Dag grootmoeder!");

str starts with random values (whatever was there last time that RAM was used) Now you copy a 16 byte string into it, leaving the last 3 chars on there initial (random) values, so these are printed out

Comments

0

Objects defined at block scope without any storage-class specifier (e.g., your str array object) have automatic storage duration.

Automatic objects that are not explicitly initialized have indeterminate value. So after your str object declaration, all array elements have an indeterminate value. By copying the string "Dag grootmoeder!" in the array you make the first 17 (the length of the string + the trailing null character) elements have a specified value. This leaves the last 3 elements of your array with an indeterminate value.

C says that an indeterminate value is either unspecified or a trap representation. C also says that reading a indeterminate value is an undefined behavior, so as usual with undefined behavior, anything can happen when you do this, such as printing garbage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.