1

This question popped into my head when I saw the following warning.

struct Wrapper{
    char somechar[10];
};

auto returnFoo() {
    char somechar[10];
    return somechar;        // Warning: address of stack memory returned!
}

auto returnFoo() {
    Wrapper wpr;
    return wpr;             // OK
}

I understand the warning. But I don't get why it goes away when I use the wrapper.

I guess the compiler can figure out how big the class is and copy the right amount of bytes but can't do the same for the pointer (char[])??

Where is the "Size" of the class stored (I guess not in the stack since that would be wasteful, maybe in the data segment?)

EDIT: I think I phrased the question wrong, I know about sizeof and have some experience with CPP. I get that char[] decays to a char*. And I understand that the first example returns a pointer, and the second one returns an object.

What I'm asking is more how the internals of the compiler work. For the compiler the memory layout is excactly the same (give or take), what it needs to do is copy the array and return it back. It needs to know how many bytes to copy though. My question was, why is it able to figure it out when using an object, but not with a char[]

15
  • char[10] is a very primitive type mostly identical to its C namesake.
    – bipll
    Sep 8, 2020 at 17:17
  • "Where is the "Size" of the class stored" -- probably in the compiler symbol table. Sep 8, 2020 at 17:17
  • 2
    The size of the class (struct) is stored in the definition, as in sizeof(Wrapper). If you return a value, the value gets copied.
    – tadman
    Sep 8, 2020 at 17:17
  • Why isn't the compiler smart enough to know the size of a statically allocated stack char[] ... Where exactly are you "statically" allocating char[]? Add static in front of the declaration to make it static. After that, it won't give any warning in that case.
    – brc-dd
    Sep 8, 2020 at 17:17
  • 1
    Arrays naturally decays to pointers to their first element. So when you do return somechar; it's actually equivalent to return &somechar[0]; Sep 8, 2020 at 17:21

1 Answer 1

7

Functions cannot return arrays in C++. This is simply how the language is specified. This is the same in C.

In the first example, the array name decays to a pointer to first element of the array, and the deduced return type is pointer to char. Pointers can be returned, but the pointer will be invalid because the automatic storage of the pointed object has been deallocated when the function returns.

I don't get why it goes away when I use the wrapper.

When you use the wrapper, you don't return a pointer. You return copy of the local class instance. The array is stored within the class.

I guess the compiler can figure out how big the class is

The compiler knows how the class has been defined, and therefore it knows the size. You can find out what the compiler knows yourself by using sizeof(Wrapper).

Where is the "Size" of the class stored

Depends on language implementation. In a typical case, the compiler will generate instruction for CPU such as "increment stack pointer by 10", because the compiler knows that the size is 10. It knows the size because it knows the definition.

Where is this definition stored (the symbol table, the data segment, the stack, the text segement)?

None of those. You've stored the definition in the source file. The compiler will parse that and store the information in some internal representation within the memory of the compiler process.

It is the compiler which produces the instructions for the CPU, so only the compiler needs to know the size. The produced program doesn't produce instructions (self modification is not possible in C++ language), so it doesn't need to know about type definitions.

3
  • "There is no need to "store" it." Isn't there? how does the compiler know how many bytes to copy from the stack back to the caller
    – frankelot
    Sep 8, 2020 at 17:27
  • Thanks! "because the compiler knows that the size is 10. It knows the size because it knows the definition." this is the core of my question. Where is this definition stored (the symbol table, the data segment, the stack, the text segement)?
    – frankelot
    Sep 8, 2020 at 17:36
  • This makes a lot of sense, thanks for the help! I just realised that the things I mentioned "data segment, text segment, etc" are needed at runtime and the size of a class is not. Now I'm wondering how does sizeof work. but that's another quesiton. I gues the compiler replaces it by the actual size at compile time\
    – frankelot
    Sep 8, 2020 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.