-1
  1. The first piece of code is:

    #include <stdio.h>
    char *getString()
    {
        char *str = "Will I be printed?";
        return str;
    }
    int main()
    {
        printf("%s", getString());
    }
    
  2. And the second piece of code is:

    #include <stdio.h>
    char *getString()
    {
        char str[] = "Will I be printed?";
        return str;
    }
    int main()
    {
        printf("%s", getString());
    }
    

In both of the above codes,char pointer is returned which points to a local variable which could get overwritten but still code-1 manages to run successfully while code-2 print Garbage values.

  • 4
    "In both of the above codes,char pointer is returned which points to a local variable" No, only the second one does that. – tkausl Sep 03 '17 at 01:19
  • It is not dangling pointer :) as your tag says. In the first case, the pointer is pointing to string in the second case its an array in read/write memory. – MCG Sep 03 '17 at 01:39
  • 1
    @MCG after execution of getString() the memory for the variables is deallocated and hence the pointer value that function returns is a dangling pointer – Rajiv Sharma Sep 03 '17 at 02:21
  • `char str[] = "Will I be printed?"; return str;` is _undefined behavior_. – chux - Reinstate Monica Sep 03 '17 at 03:51

1 Answers1

0

Let a little extend your example and see where stored our data:

#include <stdio.h>

char* some_static_var="123";

char *getString1()
{
    char *str = "Will I be printed?";
    return str;
}

char *getString2()
{
    char str[] = "Will I be printed?";
    return str;
}

int main()
{
    int some_var_at_stack=1;
    printf("%p\n", &some_var_at_stack);
    printf("%p\n", some_static_var);
    printf("%p\n", getString1());
    printf("%p\n", getString2());
}

What we got? At my 32-bit system:

0xbfc2005c

0x80497bc

0x8048554

0xbfc20035

As you can see, char* var="123" points in data segment (https://en.wikipedia.org/wiki/Data_segment), but char[] located at stack.

Every time, when you leave your function where you alloc any variables at stack, this variables get undefined, since data allocated at stack (pointers too) invalid outside function. But in getString1() actually returns pointer value to data segment which outside stack, so this function works correctly, but allocated pointer get undefined at function exit.

Community
  • 1
  • 1
bukkojot
  • 1,526
  • 1
  • 11
  • 16
  • Yes, you right. – bukkojot Sep 03 '17 at 03:37
  • And on a different day it will format your harddrive. UB is UB. Your example does not explain anything. It will not "get undefined behaviour", but it invokes UB **always**. And there is no stack in the C language. – too honest for this site Sep 03 '17 at 06:03
  • My example show real memory layout. If you don't know memory layout of your application (where data segment, heap, stack), you can't effectively code in C. – bukkojot Sep 03 '17 at 14:04
  • You don't get the point. The code invokes UB - point! There is no use in showing a specific outcome for a specic machine, a specific compiler with a specific version, on a specific date. If you don't know what UB is and what it implies, you cannot write correct C programs. It only makes sense to care about the layout of a correct, compiles C program. Siad that, you confuse a specific implementation with the language. (btw: the pointer value returned is not undefined) – too honest for this site Sep 03 '17 at 19:53