1

a variable defined in a function is created on stack. then, when function call completed, the variable is vanished due to stack in/out.

the following code is passing a data structure

typedef struct
{
    test_out_t  output;
    test_in_t   input;
} message_t;

typedef struct
{
    uint8_t    len;
    uint8_t*   data_out;
} test_out_t;

typedef struct
{
    uint8_t    len;
    uint8_t*   data_in;
} test_in_t;

The function call is void test(message_t *msg);

in the function, I defined a array, and assigned the pointer points to this array(the memory location). However, when the function call completed, I am expecting the pointer points the value becomes undetermined/Zeros, since the stack variable is gone.

However, it still has the value of the stack variable if I call printf() inside the function.

with the following code, the msg.output.data_out contains the value of array which created in the function.

If comment out the printf inside the test(). the msg.output.data_out is all zeros.

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

#define DATA_SIZE   (8)


typedef struct
{
    uint8_t    len;
    uint8_t*   data_out;
} test_out_t;

typedef struct
{
    uint8_t    len;
    uint8_t*   data_in;
} test_in_t;

typedef struct
{
    test_out_t  output;
    test_in_t   input;
} message_t;



void test(message_t *msg);



void test(message_t *msg)
{
    uint8_t  stackdata[DATA_SIZE];
    memset(stackdata, 0, DATA_SIZE);
    for (int i=0; i<DATA_SIZE; i++)
      stackdata[i] = i+1;

    msg->output.len = DATA_SIZE;
    msg->output.data_out = stackdata;

    uint8_t data2[msg->input.len];
    memcpy(&data2, msg->input.data_in, msg->input.len);

    for (int i=0; i<msg->input.len; i++)
      printf("0x%X\t", data2[i]);
}

int main(void) {

  message_t msg;
  uint32_t data2 = 0x12345678;
  msg.input.len = 4;
  msg.input.data_in = (uint8_t*)&data2;


  test(&msg);
  printf("\n");
  for (int i=0; i<msg.output.len; i++)
    printf("0x%X\t", msg.output.data_out[i]);
  return 0;
}

I assume something related to printf()

BTW, I am using online compiler to run the code.

https://repl.it/languages/c

2
  • 2
    "I am expecting the pointer points the value becomes undetermined/Zeros, since the stack variable is gone." How and why do you expect anything from behavior that is clearly undefined??? And how do you tell "undetermined" values from "determined" value? Commented Aug 29, 2019 at 21:42
  • There is nothing stopping you to access the data if you have pointer to it. But accessing the data allocated on stack outside its scope is undefined behavior. Commented Aug 29, 2019 at 22:04

1 Answer 1

2

The rules of C say that, when the lifetime of an object ends1, no guarantee is made to you about it. Thus, you cannot properly use it because you have no guarantee about it. The rules do not say that anything erases or randomizes the object.

In typical implementations, when a function returns, the stack pointer is changed to point to the new top of stack. Nothing does any extra work to erase any data on the stack. So it is still there until something else happens.

That does not mean you can reliably use the space on the stack. A variety of things can alter the data or alter your use of it:

  • Other routine calls will use the stack.
  • Other operations in the current routine may use the stack.
  • Signals could cause the stack to be used.
  • If the compiler observes your unsupported use of the object, its optimization might transform your program in unexpected ways.

Footnote

1 “The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it” (C 2018 6.2.4 1). For regular objects defined in functions, their lifetimes end when execution of the function ends (usually because the function returned, but possibly because a longjmp was executed or the program is being terminated). So, when the lifetime ends, all it means is that the storage is no longer guaranteed to be reserved. The storage still exists. Nothing is guaranteed to change it. All that changes is that while the function is executing, you are guaranteed the storage is reserved for that object, and, after the function ends, the guarantee is gone.

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

1 Comment

that make sense, the undefined behavior.

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.