Question:
Diffrence between pointers and variables?
?
2010-10-16 06:24:10 UTC
Are data in variables referenced by pointers (INT *var) stored on the heap and non-pointer variables (INT var) stored on the stack?
Four answers:
M3taSpl0itâ„¢
2010-10-16 06:43:42 UTC
Stack:

- local variables (variables declared inside a function) are put on the stack - unless they are also declared as 'static' or 'register'

- function parameters are allocated on the stack

- local variables that are declared on the stack are not automatically initialized by the system so they usually have garbage in them until you set them

- variables on the stack disappear when the function exits (thus, if a function is called multiple times, it's local variables and parameters are recreated and destroyed each time the function is called end exited).



Heap:

- declared variables (as opposed to dynamically created ie new, malloc) are created on the heap before program execution begins, they exist the entire life of the program (although scope may prevent access to them - they still exist) and they are initialized to all zeros

- global variables are on the heap

- static local variables are on the heap (this is how they keep their value between function calls)

- memory allocated by new, malloc and calloc are on the heap



now come to the pointer , pointer is a variable which holds the address of other memory heap or stack (depends on declaration of variables )

e.g :

when we create variables dynamically in which case they do not go onto the stack but rather onto the heap , we use pointers in that case to hold the newly created space's address (the address returned by the new operator).



Int lies in stack when local when declared local and in heap when declared global



Q : Are data in variables referenced by pointers (INT *var) stored on the heap



No , it can point to in on heap or stack both



I hope this helps))
anonymous
2010-10-16 06:42:33 UTC
Where variables are stored isn't really relevant to how variables differ from pointers. An integer can be in a register, on the stack, or in specifically allocated memory. A pointer can be in these places too.



Think of it this way:



* A character (char) is usually one byte. It's a short number which represents a letter.



* A pointer to a character (char*) is usually four bytes. It's a number which has the address in memory where a character (char) lives.



* By dereferencing a pointer (*ptr_to_char), you tell the language to get the address from ptr_to_char, then look at that address, and get the character from there, or to change the character there (when written like *ptr_to_char = 'x').



* By changing a pointer, you change the address it points to. So, ptr_to_char += 1 moves the pointer, to point to the next character, if there is more than one (a string). It's like moving the character your finger POINTS to in the page of a book. Yes, pointers literally point to things, that's where they get their name from.



It's exactly the same for integers.



However, I didn't use integers as the first example, since they're a little more confusing. Whereas a char is usually a different size from a pointer, an int is usually the same size. Also, a memory address is often just an integer inside the system, so in C, you can mix and match these, with a bit of conversion. Consider:



int i = 1;

int* ptr_to_i = &i;



This defines an integer, with the value 1, and a pointer to an integer, with the value of whatever memory address "i" is stored at. "int j = *ptr_to_i" sets j to the value of i; "int j = ptr_to_i" sets j to the ADDRESS of i. Your compiler should warn about doing that, since you didn't declare j to be a pointer, just to be an integer. However, the compiler won't really care, if you really want to do that.
green meklar
2010-10-16 09:19:57 UTC
Pointers are a type of variable. There is no fundamental difference between them in hardware; the only difference is in how the program interprets them at runtime. Whether a variable is allocated on the stack or the heap depends on whether it is a local variable inside a function (which goes on the stack), or a member variable of an object (which goes on the heap).
broadway
2016-12-15 11:20:28 UTC
distinction guess function pointer and pointer function. permit's think of "fp" as function pointer and "pf" as pointer function "fp" is a pointer- it fairly is pointing to a pair function say f(x) "pf" isn't a pointer- fairly it fairly is a function which describes the character of a pointer. now why could we call this function as "pf"? why cant we merely call it as "function" ??-bcoz this function will finally act as a pointer.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...