Home > Software engineering >  Do pointer return types always needs to be allocated on heap?
Do pointer return types always needs to be allocated on heap?

Time:03-07

I am new to C and I am currently learning Heap and Stack. In our class we had a problematic function like this

int* Problematic(int capacity) {
     int data[capacity];
     // do something
     return data;
}

I know this won't return anything but a dangling pointer(is it?) since stack will deallocate everything in the function when it finishes executing. And here comes my questions:

  1. Will this problem happened to all the functions which their return type is a pointer but they are running on stack?(Since they just return an address and stack delete every thing when the function finished running)?
  2. Is there any other things that pointer can do besides connecting stack to heap(Like Pointing to an address on heap)?
  3. From my understanding, the difference between stack and heap is that for stack, instead of manually allocating memory by programmer, stack allocating memory automatically by system continuously. So I am wondering if I have a function like this int ReturnInt() {const int a = 5;return a;} what actually this function returns, if it returns an address, won't stack deleted the 5 as before? If this function returns an integer 5, how to explain this integer in memory since it just add 4 bytes in RAM and changes still happens in memory address?

Thank you so much for help :)

CodePudding user response:

  1. Yes. Returning a local non-static address has little value. Such returned addresses are unusable for dereferencing. You can still printf("%p\n",(void*)the_address) them but that's about all you can do with them. (Returning the address of a local static makes sense, though. Such a returned local address is safe to dereference.)

  2. Pointers can point to anything: globals, statics, and they can be passed from a caller (who could allocate their target on the stack for example).

  3. int ReturnInt(){const int a = 5;return a;} returns through a register on most platforms. If that's not possible, the compiler will have made sure the caller has stack-allocated space for the return value.

CodePudding user response:

Will this problem happened to all the functions which their return type is a pointer but they are running on stack?(Since they just return an address and stack delete every thing when the function finished running)?

Yes, if they return a pointer to something allocated on the function's stack.

Is there any other functions that pointer has besides connecting stack to heap(Pointing a address on heap)?

Sure. For example, one object allocated from the heap might contain a pointer to another object allocated on the heap.

From my understanding, the difference between stack and heap is that for stack, instead of manually allocating memory by programmer, stack allocating memory automatically by system continuously. So I am wondering if I have a function like this int ReturnInt() {const int a = 5;return a;} what actually this function returns, if it returns an address, won't stack deleted the 5 as before? If this function returns an integer 5, how to explain this integer in memory since it just add 4 bytes in RAM and changes still happens in memory address?

It doesn't return an address. Its return type is int and it returns the integer value 5, not any address at all. The function returns the value 5. There is no requirement that the implementation store that 5 in memory and it may or may not do so as it wishes. It could, for example, return the value 5 in a register.

CodePudding user response:

#2 Question (not clear but heres a shot)

you also have a third storage space option - static. Thats fixed size determined by what you ask for at compile time

  #include<stdio.h>
  char buffer[500];
  ...
  int main(int argc, char**argv){
  }

buffer is 500 bytes statically allocated

CodePudding user response:

  1. absolutely. If a function returns a pointer to a data allocated on its stack, the stack becomes invalid after exit from the function and the pointer will point to invalid location.

  2. the question is not clear.

  3. stack is a space in memory which is allocated for internal use of a function. It gets reserved for the function call and is released for any other use after function returns. In your example int ReturnInt() {const int a = 5;return a;} there is no pointers. The result is returned by value. In other words, the value of variable a is copied to the location of the function return value before its return. This preserves the value and allows it to be used after the function return. The following with pointers will not work: int *ReturnIntPtr(){int a = 5; return &a;}. Here you return a pointer to stack which is not valid after the return.

  • Related