will this cause memory leaks if I don't make the variables become pointers? or will the compiler automatically do this for me? I am using Visual C++ 6.0.
Code:#include<iostream.h> int main() { int number = 255; return 0; }
This is a discussion on about memory leaks with this simple program within the C++ Programming forums, part of the General Programming Boards category; will this cause memory leaks if I don't make the variables become pointers? or will the compiler automatically do this ...
will this cause memory leaks if I don't make the variables become pointers? or will the compiler automatically do this for me? I am using Visual C++ 6.0.
Code:#include<iostream.h> int main() { int number = 255; return 0; }
look here
i think it depends how you use it after it points and before you delete
No and no, there is no need for pointers in that 'program'
so the variable won't be memory resident after I terminate the program? so why are pointers needed?
you only get memory leaks if you forget to delete unnecessary pointers.
Good question, the answer will not become apparent until you get a little further in C++. What is a pointer? A pointer is some that points to an address of where a datatype is stored. Take the following code for example.
Alas, I'm probably not the best person to explain this, but say you have a very large Class. Whenever you want a function to manipulate or use data from your class you must pass it as a paramter. Say we have Enemy as a class.Code:int main( void ) { int nNum = 10; // Initialize nNum to 10 from Stack // Initialize a pointer to an int , and set to NULL int *pNum = NULL; // Set what pNum points to as the address of nNum pNum = &nNum; // If we want the address of nNum cout << "Address = " << pNum << endl; // If we dereference the pointer, we get the data cout << "Data = " << *pNum << endl; return ( 0 ); }
void Foo( int nSomething, Enemy Temp );
This would make a copy of the WHOLE class and pass it to the function. This is bad on numerous levels. Lots of calls to constructors and such and just very slow. We would like to just give the function the address of where the class is and let it manipulate the ACTUAL data instead of a copy.
void Foo( int nSomething, Enemy &Temp );
That's basically one reason pointers are good. Another is you have an array and won't know until run-time how many elements you will need. Consider the following code.
These might not make that much sense to you now, but they should become clearer as time passes. Hope I didn't mess anything up while explaining. Perhaps someone else can explain it more clearly.Code:void Foo( int nItems ) { char Array[ nItems ]; // This will NOT compile!! return; } void Foo( int nItems ) { char *pArray = new char [ nItems ]; // This works . // Something . // Something . // Something delete [] pArray; // Make sure you always free it return; }
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers