Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

Can someone help me with the basics of pointers. What I want to know is in C++ if a pointer that points to the heap can be accessed globally.

OpenStudy (anonymous):

for example.. int main(){ int* pontr; pontr = new int; return 0; } void (){ int x = 5; pontr = &x }

OpenStudy (e.mccormick):

Do you know how scope works?

OpenStudy (anonymous):

A variable is to a value as a pointer is to an address. A pointer is a variable that is assigned to an address. Because it is assigned to an address, it has exclusive features like dereferencing (*) and pointer arithmetic. In other ways, it is no different than a variable. If you want a global pointer, it is no different than a global variable ``` int y; /* Global variable y */ int* py; /* Global pointer py */ int main(){ /* At this point py is a dangling pointer, unless we set it to NULL. */ /* Dangling pointers are bad, because dereferecing (i.e. *py) will cause */ /* a segmentation fault */ py = new int; /* Now py points to a dynamically allocated integer */ f(); /* Now that the function exited, local variable x was popped from the stack. */ /* Now py is a dangling pointer and dereferencing py will cause a segmentation fault */ return 0; } void f() { int x = 5; /* Here x is a local variable to f, and it is on the stack */ /* Currently py points to dynamically allocated memory, if we change */ /* the value of py, then we will lose the address of that memory and it will */ /* become a memory leak. W should have called delete first */ py = &x /* Here py now points to x. We can use *py to get 5, the value of x */ } ```

OpenStudy (anonymous):

Suppose you want to pass a variable by reference... Here is how not to do it with `g` and how to do it with `f` and what is going on. ``` int main() { int y = 2; g(y); /* Now y == 2 */ /* The referencing operator & will pass the address of y to the function */ f(&y); /* Now y == 5 */ } void g(int y) { /* This y is its own variable, with its own memory address */ int x = 5; /* This assignment has no affect on the y in main */ y = x; /* Now y and x will go away, because function is over */ } void f(int* py) { /* So py is a pointer to y, since it is set to y's address */ int x = 5; /* We can now de-reference that address, and get something equivalent to y */ *py = x; /* Basically *py in f(), is the same as y in main() */ /* Now py and x will go away, because the function is over */ } ```

OpenStudy (lyrae):

^^^^ (pointers and references) is a important concept since c++ function calls (unlike e.g. java) copies object parameter as new values instead of passing their references you might end up using to much automatic (stack) if you're uncarefull. Like wio pointed out you have to declare your variable/pointer in the global scope for it to be accessible *everywhere*, although it's usually good (best) practice to keep the global scope as clean as possible (preferably entirely empty) and stick with classes and passing references instead. Where the pointer is accessible shouldn't have anything to do with where the pointer is pointing. There are other memory areas (besides automatic and dynamic (heap)) to where a pointer can point ex. functions and static.

OpenStudy (anonymous):

I explained why your example code was wrong. I should explain what you should have done if you wanted a global pointer instead of pass by reference. ``` int* pontr; int main(){ /* You need to allocate memory for an int on the heap. */ pontr = new int; f(); return 0; } void (){ int x = 5; /* We are copying the value of x into the heap memory we are pointing at */ *pontr = x; } ``` See, you don't want to point to `x` because it will be taken off the stack when `f()` is done. You really just want to keep its value. Suppose you didn't want to use heap memory. You could also use stack memory as well, so long as you know it will be around when it needs to be around. ``` int* pontr; int main(){ int x; /* We can use existing stack memory for the int. */ pontr = &x; f(); /* We won't have to worry about dangling pointers because x */ /* won't go away until main exists, which ends the program */ return 0; } void (){ int x = 5; /* We are copying the value of x into the stack memory we are pointing at */ *pontr = x; } ``` The upside to stack memory over heap memory is that it gets allocated much more quickly and it frees itself automatically which means no memory leaks. The upside to heap memory is that you can free it whenever you want (the scope doesn't have to be exited), and you can allocate it during run time. For some tasks using stack memory is nearly impossible because you will need user input to determine the size.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!