+ 2
Pointers? Oh my God
I will be really appreciated if someone 'teach' me what these pointer, stack and heap things are and how to use pointers prpperly. please :)
5 Antworten
+ 4
Imagine that your computer's memory is made of tiny blocks containing datas.
When you create a variable or a function, you use some of your computer's memory to stock it somewhere. Let's say I created a 'i' variable of type int, containing 42. 'i' is a variable and is in one of those memory blocks, containing it's value : 42.
A pointer towards 'i' will point towards the block my variable is in. Meaning that I'm pointing towards the content of the block and not the variable itself, so I can modify directly the content of the block and it will modify my variable too.
+ 2
You can cover a whole book on this topic, but let's try to keep it short.
The stack and heap are 2 areas in RAM memory that is under control of your program. The stack size is normally set in the DEF file for your program. The size of the heap is also set but it can grow (by requesting more memory from the OS).
You can consider the stack as dedicated to functions. Function bodies are "pushed" onto the stack as they are called or "popped" from the stack when they return. You can think of the stack as growing "up" or shrinking "down" in accordance with how deep the level of nested function calls is. For example:
void boo() {
//Stack here is main, foo, boo
}
void foo() {
//Stack here is main, foo
boo() ;
}
int main() {
//Stack here is main
foo() ;
}
The stack will also contain the local variables defined inside function bodies:
void foo() {
MyClass x; //x will live (be in scope) in foo while foo is on the stack.
}
The lifetime of local variables is tied to their owning functions. When the above foo() function is "popped" off the stack when foo returns the destructor of x will automatically be called.
So I've run out of space, I will add another answer for the heap.
+ 2
Previous answer continued...
The heap is where you dynamically allocate memory - by the use of pointers. (And in C/C++ you are responsible to also release this heap allocated memory yourself. In languages like Java/C# the GC will reclaim the memory when not in use anymore, but not in C++). So if we consider foo again:
void foo() {
MyClass x;
MyClass* y = new MyClass();
}
As already stated x is a local stack instance variable and will live (be in scope) in foo while foo is on the stack. y OTOH is a stack instance pointer variable, and its lifetime is likewise tied to the life of foo. But the instance of MyClass it points to (allocated by the call to 'new') is on the heap and its lifetime is not tied to the lifetime of foo.
In this case, when foo returns, unlike for x, y's destructor will not be called (you have to explicitly call "delete y" for that to happen) and in this case you get your classic memory leak.
For foo to be memory safe:
void foo() {
MyClass x;
MyClass* y = new MyClass();
//Do stuff with x and y:
x.func();
y->func() ;
....
delete y; //This will call y's destructor
} //x's destructor will automatically be called here
+ 1
I'm really really glad to read your answer master.
But, the thing is why or where should I describe a pointer and why should I explicitly delete it? Why I'm not always using local varaibles, what is the point of pointers? :)
I know it seems i didn't get the stuff.
+ 1
@Yunus:
The reason you want to use heap-allocated variables is precisely because they are not dependent on the scope and lifetime of functions. Here is an example :
int makeArray(int** pp, int size) {
/* The allocated array is returned via pp*/
*pp = new int[size] ;
if(*pp)
return 0;
else
return -1;
}
void bar() {
int *p;
int x = 10;
int res = makeArray(&p, x) ;
If(res) {
cout << "Error!" ;
return;
}
//Use p here
for(int i=0; i<x; i++)
p[i] = 5+i;
... etc...
delete [] p;
}
void foo(int *p, in size) {
int res = makeArray(&p, size) ;
If(res) {
cout << "Error!" ;
return;
}
//Use p here
p[3] = 6;
... etc...
}
int main() {
bar() ;
int *p;
foo(&p, 20);
//Use p here
for(int i=0; i<x; i++)
p[i] = 8+i;
... etc...
delete [] p;
}
The point to grasp is that the array allocated on the heap to double pointer local variable pp inside MakeArray survives when MakeArray returns, and is used by pointer p in the calling functions.
Also note that pointer p inside foo and main are 2 different and distinct variables (even though they have the same name), since their scope is limited to their function bodies.