I am using VC++, I have created objects of a class and i am taking care to delete after i am done with using that object. Since I have limited Memory, I need to get memory freed everytime i Delete. But I am seeing that after delete also the memory is still occupied. Could you please let me know if u have faced similar issues and i am using GlobalMemoryStatus for checking the memory so is it reliable
Please help me.
Thanks
BhMu
[GlobalMemoryStatus can return incorrect information. Use the GlobalMemoryStatusEx function instead. ]
Secondly, this gives you the systems memory status (not just for your application). Are you allocating massive amount of memory, if not you probably wont notice any changes. Other applications using memory can also give you false positives (if they themselves are allocating/deallocating).
Also the delete and delete[] operators only work with dynamically allocated memory, if you are calling the normal constructor to initialize the objects they are automatically deleted when they exit scope ({ starts a scope and } ends it)
so for example
1 2 3 4 5 6 7
class a {};
a newA ()
{
a newa;
return newa;
}
returns a value that is automatically deleted, however:
1 2 3 4 5
a newA ()
{
a * newa = new a;
return *newa;
}
does the same thing but the value is not deleted until you choose to which is done via: delete obj or delete[] obj if obj is an array, however the delete operator does not work on objects created the same way as the first newA, those are automatically handled and destroyed when exiting the scope they are created in
if i am correct the second newA is returning the address thats created
and i use the first newA as an example of what wouldnt work, whether i say deleted or destructed doesnt matter that much, its a simple typo
because the operator {} means scope and as soon as you hit the line with the } on it, any variables declared after the { are automatically destructed, you can even use the symbols without some kind of if statment, ie:
1 2 3 4 5 6 7 8 9 10 11
int main ()
{
{
std::string x = "Hi";
}//destructor is called on x
//x = x.substr(0, 1); wouldnt work here, x is already deleted from memory
{
int x = 0;
}//destructor is called on x
return 0;
}
this compiles and works, anything referencing x inside the first set references a string, second set its an int, this is, however, a bad habit to get into unless you must absolutely destroy the variables inside to free up memory, but names work with it as well
this isnt something that i am just saying, this is what a doctor of computer science specializing in programming algorithms says, that in C++ scopes are much much more complex and useful than almost any other language
GFreak, what you say there is correct, but if you're using it to justify the statement that
1 2 3 4 5 6 7
class a {};
a newA ()
{
a newa;
return newa;
}
doesn't work, you are mistaken.
newA returns an a by value. It is a copy of newa and doesn't refer to it, nor is it tied to newa's lifetime in any way. This has more to do with passing arguments than it does with object lifetime/scope.