Work around error 'Address of stack memory associated with local variable returned'

tam5

I am trying to call a method that will generate a 2D char array (array of strings) and return it to be used in another function.

My example:

char ** example(void)
{
    char *test[3];

    int i;
    for (i = 0; i < 3; i++) {
        test[i] = malloc(3 * sizeof(char));
    }

    test[foo][bar] = 'baz'; // of course I would declare 'foo' and 'bar'
    // ...
    // ...

    return test;
}

And then I would like to be able to use the array as follows:

void otherMethod(void)
{
    char ** args = example();
    // do stuff with args
}

The problem is that this produces the error:

warning: address of stack memory associated with local variable 'test' returned [-Wreturn-stack-address]

I could solve this problem by defining test in the global scope as opposed to locally, but I would very much rather not do this as it seems messy, especially if I am to have several of these.

Is there a way to create and return an array of strings in C without defining it globally?

Sergey Kalinichenko

You are on the right track. All you need to do is to change the allocation of the test[3]; itself from automatic (aka "stack") to dynamic (aka "heap"):

char **test = malloc(3 * sizeof(char*));

This makes it legal to return test from your function, because it would no longer be returning an address associated with stack allocation.

Of course the caller would be required to free both the pointers inside the return, and the return itself. You may want to consider supplying a helper function for that.

Another approach would be to take char test[] as a function parameter:

void example(char *test[], size_t count) {
    for (size_t i = 0 ; i < count ; i++) {
        test[i] = malloc(3 * sizeof(char));
    }
    ...
    // return is not required
}

Now the caller would have to pass an array of suitable size into your function, so that you could avoid allocating it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - Avoiding warning "address of stack memory associated with local variable returned"

From Dev

Using C-string gives Warning: "Address of stack memory associated with local variable returned"

From Dev

"Stack around the variable was corrupted" error

From Dev

"Stack around the variable was corrupted" error

From Dev

Finding the memory address of a Java local variable on the Dalvik Stack from native code

From Dev

C: Error with stack around the variable 's' was corrupted

From Dev

Getting error: Stack around the variable was corrupted

From Dev

warning: address of local variable 'angles' returned [-Wreturn-local-addr]

From Dev

Stack around the variable was corrupted

From Dev

Stack around variable was corrupted

From Dev

Stack around variable was corrupted

From Dev

Error when accessing array - stack around the variable 'scores' was corrupted

From Dev

Memory address of local variable changes depending on the presense of lambda parameters

From Dev

stack around the variable...was corrupted

From Dev

Stack around variable 'x' was corrupted

From Dev

Stack around the variable 'sortArray' was corrupted

From Dev

Stack around variable was corrupted - C

From Dev

generate random stack memory address

From Dev

storage of address of variable in memory

From Java

Is the address of a local variable a constexpr?

From Dev

Why is returning address of local variable or temporary only a warning and not an error?

From Dev

Memory error in stack

From Dev

Watch a variable (memory address) change in Linux kernel, and print stack trace when it changes?

From Dev

Local variable returned in recursive function

From Dev

Basic C++ error. Run-Time Check Failure #2 - Stack around the variable 'matrix' was corrupted

From Dev

Why does my function gives me "Stack around the variable 'url' was corrupted." error?

From Dev

C - Stack around the variable 'name' was corrupted

From Dev

Stack around the variable was corrupted with pointer arithmetics

From Dev

Array issues: Stack around the variable 'arr' was corrupted

Related Related

  1. 1

    C - Avoiding warning "address of stack memory associated with local variable returned"

  2. 2

    Using C-string gives Warning: "Address of stack memory associated with local variable returned"

  3. 3

    "Stack around the variable was corrupted" error

  4. 4

    "Stack around the variable was corrupted" error

  5. 5

    Finding the memory address of a Java local variable on the Dalvik Stack from native code

  6. 6

    C: Error with stack around the variable 's' was corrupted

  7. 7

    Getting error: Stack around the variable was corrupted

  8. 8

    warning: address of local variable 'angles' returned [-Wreturn-local-addr]

  9. 9

    Stack around the variable was corrupted

  10. 10

    Stack around variable was corrupted

  11. 11

    Stack around variable was corrupted

  12. 12

    Error when accessing array - stack around the variable 'scores' was corrupted

  13. 13

    Memory address of local variable changes depending on the presense of lambda parameters

  14. 14

    stack around the variable...was corrupted

  15. 15

    Stack around variable 'x' was corrupted

  16. 16

    Stack around the variable 'sortArray' was corrupted

  17. 17

    Stack around variable was corrupted - C

  18. 18

    generate random stack memory address

  19. 19

    storage of address of variable in memory

  20. 20

    Is the address of a local variable a constexpr?

  21. 21

    Why is returning address of local variable or temporary only a warning and not an error?

  22. 22

    Memory error in stack

  23. 23

    Watch a variable (memory address) change in Linux kernel, and print stack trace when it changes?

  24. 24

    Local variable returned in recursive function

  25. 25

    Basic C++ error. Run-Time Check Failure #2 - Stack around the variable 'matrix' was corrupted

  26. 26

    Why does my function gives me "Stack around the variable 'url' was corrupted." error?

  27. 27

    C - Stack around the variable 'name' was corrupted

  28. 28

    Stack around the variable was corrupted with pointer arithmetics

  29. 29

    Array issues: Stack around the variable 'arr' was corrupted

HotTag

Archive