0

If I declare a pointer inside a function, is it located in the function stack or on the heap?

Ex:

typedef  uint32 my_element[16];
void foo()
{
my_element element* CSAAddress = 5;
}

In this case, where is the element my_element located in memory?

kaan_a
  • 3,503
  • 1
  • 28
  • 52
bouqbouq
  • 973
  • 2
  • 14
  • 34
  • If you set the pointer to 0 then it isn't pointing anywhere. You could set it to some other numerical value but then it is pointing to a random thing in memory. If you want to point it to an object you made thern you need to make the object and point the pointer to it. – Jerry Jeremiah May 28 '20 at 08:33
  • The pointer itself will be stored in stack. But where it is pointing to depends on the allocation. – Raman May 28 '20 at 08:36
  • 4
    What is element defined to be? Is `my_element element*` meant to be a type? – Jerry Jeremiah May 28 '20 at 08:37

2 Answers2

4

Your example is syntactically invalid.

For:

#include <stdint.h>
typedef  uint32_t my_element[16];
void foo(void) { my_element *element = 0; }

element will have automatic storage duration.

Whether an implementation will use a stack, a register, completely optimize the variable out (these three are most likely) or something different the C standard does not specify.

The C standard describes semantics, not implementation details.

For

#include <stdint.h>
typedef  uint32_t my_element[16];
void bar(void) { my_element *element = (my_element*)5; }

(the cast is required to satisfy constraints) the behavior would most likely be undefined because producing a pointer that's not sufficiently aligned for its target type results in undefined behavior (http://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p7).

Moreover, even if you created a valid pointer pointing to address 5 (e.g., char *p5 = (char*)5; (chars have minimal alignment requirements) it would not be very useful (dereferencable) because operating systems generally don't allow you to map things to that address (on Linux, you need root privileges to map the first memory page)).

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
1

Your pointer will be stored in the stack, but if you are allocating a memory dynamically, then the allocated space is assigned on the heap.

Also, do note that you are pointing to random memory space by using my_element element* = 5; Do note that your expression - my_element element* CSAAddress = 5; is a syntax error.

NOTE: The following is referred to list the main point related to the question from link provided below

Stack segment is used to store variables which are created inside functions (function could be main function or user-defined function), variable like -

  • Local variables of the function (including pointer variables)

  • Arguments passed to function

  • Return address

To answer your question, the my_element is a pointer which will be stored on stack. You should also refer this to get a better understanding of where your variables are stored.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • There are enough compiler issues I’m not actually sure what was meant. Look at the line again: `my_element element* CSAAddress = 5;`. This does not compile, because `my_element element` is not a type. – Daniel H May 28 '20 at 08:43
  • @DanielH I wrote the answer supposing that `my_element` is a user-defined type and must be defined somewhere. I guess the question needs editing to properly specify this. – Abhishek Bhagate May 28 '20 at 08:46
  • Regardless, you can't assign an integer to a pointer in C, with the exception of `0` being a null pointer constant. – Lundin May 28 '20 at 09:08
  • **Also, do note that you are pointing to random memory space by using `my_element element* CSAAddress = 5;`** I did point that out in my answer. – Abhishek Bhagate May 28 '20 at 09:18
  • Yes, `my_element` is a user-defined type at the top of the code snippet. But `my_element element` is not; it is a syntax error. I don’t know if the OP meant `my_element element = 5` (with `CSAAddress` an unrelated variable), or `my_element* CSAAddress = 5`, or what. – Daniel H May 28 '20 at 09:24
  • @DanielH yep, your are right. I will edit that into my answer as well. – Abhishek Bhagate May 28 '20 at 09:25