0

The following code:

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

int main() { 
    char *s ; 
    char *fun() ;   
    s = fun() ;    
    printf ("%s",s) ;
    return 0; 
} 

char *fun() {  
    char buffer[30] ;
    strcpy ( buffer, "RAM - Rarely Adequate Memory" ) ; 
    return ( buffer ) ;  
} 

Gives unexpected results whenever the size of buffer is changed and does not give the required answer.

By making char buffer[30] static the code prints "RAM-Rarely Adequate Memory" which is right.
How does static make a difference?

Jarod42
  • 203,559
  • 14
  • 181
  • 302

6 Answers6

3

(automatic) local variables are destroyed as soon as you leave the block where they were declared. Keeping a reference to them is just as wrong as keeping a pointer to a deallocated memory block.

static (local) variables have the same lifespan as the program that declares them.

Implementation-wise, a static variable is allocated in the same space as the global variables.
Semantically, it is still only visible inside the block where it was declared, but it retains its value outside its declaration scope.

Beware, though. Retaining a reference to a static variable might have unpleasant side effects.

For instance, each call to your function will reset the string to its initial value, so the callers should make their own copies of it if they don't want any other piece of the code to be able to mess with the value they got from your function.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
2

You're returning a local variable, which is triggering undefined behaviour. char buffer[30] is a stack variable, so when the function exits, it goes out of scope, and is cleaned up.

Making it static means it is does not go out of scope when the function exits, and thus works correctly.

In C, one would usually fix this by passing in a buffer to write to:

void fun(char *buffer, size_t len)
{
    // Write some stuff into buffer
}

In C++, use std::string.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
1

Without static, the variable buffer passes out of scope as soon as the function fun terminates. The main routine is then left holding a pointer to a defunct variable, and dereferencing it causes Undefined Behavior.

Beta
  • 96,650
  • 16
  • 149
  • 150
1

buffer is a local variable, wich is destroyed one time fun ends.

if buffer is static, is always in the program.

if you want that fun return an array, you can use dynamic memory.

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

int main( ){

    char *s;    
    char *fun();

    s = fun();    
    printf ("%s",s);

    // in c pure 
    //free(s);
    // in c++
    delete s;
}


char *fun(){         
    // in c pure
    //char * buffer = (char*) malloc(30*sizeof(char));        
    // in c++
    char *buffer = new char[30];

    strcpy ( buffer, "RAM - Rarely Adequate Memory" );        
    return ( buffer ) ;    
} 
Narkha
  • 1,197
  • 2
  • 12
  • 30
1

First, pointer to local variable should not be returned.

Second, auto local variable will disappear as soon as the code block (function) exits. But static local variable will stay there till the whole program is ended. For the same reason, you can access that static local variable next time you get into that code block.

Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
0

The buffer without static is placed on the processor's stack. When fun ends, the stack memory that was used for this function is freed.

But this freeing does not actually change the contents of these used stack memory locations. This means that the string you copied to buffer will initially still be there.

As the program progresses, this freed stack space (which was first used by your buffer), will get used for other stuff. But it's hard to tell when this will happen, as it all depends on how the compiler planned to use stack space.

In theory, your program could have nicely printed out RAM - Rarely Adequate Memory; when this part of the stack were not overwritten. So actually you are lucky, as you could have had a latent bug that could bite at a later stage (for example after you started adding code to main(), or changed compiler options for example.)

meaning-matters
  • 21,929
  • 10
  • 82
  • 142