0

Because in function f, is it likely that the buffer reference is lost? I know the function is destroyed after it is called, but if the memory address remains the same, why is that content lost? Another detail, because this is an indefinite behavior. In function g, when using strcpy, the content is not lost. I want to understand what the compiler does, wanted to sample it in assembly. Thankful.

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

char *f(void) {
      char buffer[7] = "blabla";
      char *ptr = buffer;
      return ptr;
}

char *g(void) {
     char *pt = malloc(20);
     char str[7] = "blabla";
     strcpy(pt,str);
     return pt;
}

void main(void) {
    char *s1 = f();
    puts(s1);
    char *s2 = g();
    puts(s2);

}
Yuri
  • 53
  • 4
  • 1
    What do you mean by "function is destroyed"? "indefinite behavior" == undefined behavior. – Fiddling Bits Nov 09 '18 at 15:54
  • 1
    Doesn't the compiler warning you that you return a pointer to a local variable (which will not survive the return of the function)? – Some programmer dude Nov 09 '18 at 15:54
  • 1
    this is an answer for a C++ question, but the same concepts apply.. and it's entertaining to read:) : https://stackoverflow.com/a/6445794/3476780 – yano Nov 09 '18 at 16:00
  • 1
    Have a read about 'automatic' variables... – Frankie_C Nov 09 '18 at 16:01
  • For your own benefit, configure gcc to report all warnings and report them as errors (`-Wall -Werror`). Your compiler is smart enough to [prevent you from even running this](https://godbolt.org/z/Go5x_v). – vgru Nov 09 '18 at 16:29

1 Answers1

4

The buffer in g will remain after the call to g terminates, since it was allocated on the heap.

The buffer in f was allocated on the stack (since it was declared without the static qualifier, which would put it in the process’ data section) so it will no longer be well-defined to attempt to access through a pointer when it goes out of scope (i.e. when the function exits); the return value will be a dangling pointer.

I get the following warning when compiling your code:

warning C4172: returning address of local variable or temporary: buffer
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • Yes, but I did compile here and both showed the changed content. How can that be. If the buffer function is a local variable? The behavior always changes and I would like to know why. – Yuri Nov 09 '18 at 16:11
  • 1
    @Yuri Undefined behavior means *anything can happen.* This includes sometimes working and sometimes not working. – Govind Parmar Nov 09 '18 at 16:15
  • Thanks for the answers. I was left with this doubt, I wanted to know how the compiler does this in assembly. – Yuri Nov 09 '18 at 16:21
  • 1
    @Yuri You could examine the assembly code generated by your compiler, or run your executable through a debugger step-by-step to see what's going on. – Govind Parmar Nov 09 '18 at 16:22
  • I will do this, I will use objdump to disassemble the code – Yuri Nov 09 '18 at 16:24
  • 1
    @GovindParmar Just to clarify for future readers: variables declared inside a function go on the stack UNLESS they have the static qualifier, then they go into data segment. Why it works when you put static is front is NOT because its a magic word that makes stack address persist, it instead changes where the variable lives in the programs memory so that it is no longer on the stack. – Bwebb Nov 09 '18 at 18:30
  • 1
    @Bwebb you’re right; I will update my post to clarify this explicitly shortly. – Govind Parmar Nov 09 '18 at 18:31