Kevin Dong
Kevin Dong

Reputation: 5369

Is this also an undefined behavior in Ansi C?

If the code like this, it must be an undefined behavior.

char *String = "Stack Overflow"; //undefined behavior

For this reason, is the following also an undefined behavior? But most of my reference book write like this!

char *Print (char *String)
{
    return String;
}

int main (void)
{
    printf ("%s", Print("Stack Overflow"));
    return 0;
}

To avoid writing an undefined-behavior code, why not doing like this?

char *Print (char String[16])
{
    return String;
}

int main (void)
{
    printf ("%s", Print("Stack Overflow"));
    return 0;
}

Upvotes: 0

Views: 220

Answers (4)

mockinterface
mockinterface

Reputation: 14880

Defining a string like this is not an undefined behaviour. What will occur when you write,

char *String = "Stack Overflow";

if that the compiler will reserve an array of bytes in the data segment of the executable and put the "Stack Overflow" string there. Then it will point the String pointer at it. It is only when you attempt to access this pointer the behaviour will be undefined.

Note that overall it is better to avoid the additional indirection and simply define:

static const char String[] = "Stack Overflow";

The difference is subtle, but you save on a pointer and you also tell the compiler that String is immutable and it will likely be placed in a readonly memory page.

Upvotes: 2

Ned
Ned

Reputation: 957

After your recent edit, it seems you are concerned that all the characters in a string literal -- i.e. an array of characters -- are pushed onto the stack for a function call. This is incorrect. Only a pointer to the array is passed and pointers to string literals -- or any string for that matter -- are always the same size and known to the compiler at compile time.

As for your first example, it does not have any undefined behavior. The string literal -- the text between the quotes -- is written into an array of characters by the compiler. Then a pointer -- i.e. the "address" of the array -- is assigned to the varible correctly declared to be a char * -- though const char * or const char * const may be more appropriate depending on implementation details and programmer intent.

Upvotes: 1

Andrei
Andrei

Reputation: 5005

Attempting to modify the contents of the String variable is undefined.

The use of char*, passing it as a parameter and returning it from the function are not UB.

Upvotes: -1

user529758
user529758

Reputation:

If the code like this, it must be an undefined behavior.

char *String = "Stack Overflow"; //undefined behavior

Must be? Why? No, it isn't. Perhaps it's not the best idea to assign a string literal to a pointer-to-non-const, but as long as you don't modify its contents, it's OK.

The second construct isn't undefined either. String literals have static storage duration. If you return a pointer to the first character to it, it will be valid regardless of the lifetime and scope of the pointer (as long as it's copied over properly, e. g. it's passed to or returned from a function, which is exactly what happens in your code).

Upvotes: 5

Related Questions