|
From: Daniel G. <sp...@ho...> - 2002-07-04 07:19:19
|
Hmm...well you could write a function yourself that kept track of the
memory, or use a global variable.
Very basically, you'd have a global variable for memory allocated, and each
time you did Pointer = new Class you could call the sizeof function with
that object and do sizeof(*Pointer)
Maybe it would look like the following:
#include <iostream>
class Car
{
public:
//...
};
double gVariable; // global variable
int main( int argc, char *argv[] )
{
Car *Pointer = NULL;
Pointer = new Car;
gVariable += ( sizeof(*Pointer) );
//...
std::cout << "The memory allocated in total was " << gVariable;
return 0;
}
What does everyone think?
>From: "Javier Turek" <jav...@ci...>
>Reply-To: "Javier Turek" <jav...@ci...>
>To: "DEVC" <dev...@li...>
>Subject: [Dev-C++] Memory
>Date: Thu, 4 Jul 2002 01:53:31 -0300
>
>Hello!
>Does somebody know of a library to use with dev-c++
>to control what is going on with the memory?
>I mean that I want to know how much memory
>I use for my variables and pointers, and at the end
>of my program I must know if all the memory I asked for,
>has been freed.
>
>Thanks.
>Javier
>from Argentina
>
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
|
|
From: Daniel G. <sp...@ho...> - 2002-07-04 12:37:08
|
Ok I'm a little lost...
you can't destroy classes, only instances of classes (objects). If you
destroy the object the memory is released - I assume by "losing memory" you
mean memory is not deallocated i.e. made available.
does anyone know if it is possible to allocate X memory for an application
and only let it use that much? I'm thinking that X MB of memory could be
allocated then at the end of the program check to see if it is all available
again.
like if ( CurrentMem == InitialMem )
{
cout << "Memory has been managed perfectly.";
}
>Thank you.
>I will try to explain myself better.
>I have to make some classes for the univesity
>and the profesor told me that my program
>doesn't has to lose memory. For example:
>I've got an structure that uses a list of nodes.
>If I destroy the class, maybe, I'll need to
>destroy all nodes. I need to verify that
>I'm not loosing memory (asking for it and don't
>release it) at the end of the program.
>I know there are modules which you can use
>for these things.
>
>Thanks again,
>Javier
>
>----- Original Message -----
>From: "Daniel Glenfield" <sp...@ho...>
>To: <jav...@ci...>; <dev...@li...>
>Sent: Thursday, July 04, 2002 4:18 AM
>Subject: Re: [Dev-C++] Memory
>
>
> > Hmm...well you could write a function yourself that kept track of the
> > memory, or use a global variable.
> >
> > Very basically, you'd have a global variable for memory allocated, and
>each
> > time you did Pointer = new Class you could call the sizeof function with
> > that object and do sizeof(*Pointer)
> >
> > Maybe it would look like the following:
> >
> >
> > #include <iostream>
> >
> > class Car
> > {
> > public:
> > //...
> > };
> >
> > double gVariable; // global variable
> >
> > int main( int argc, char *argv[] )
> > {
> > Car *Pointer = NULL;
> > Pointer = new Car;
> > gVariable += ( sizeof(*Pointer) );
> >
> > //...
> >
> > std::cout << "The memory allocated in total was " << gVariable;
> > return 0;
> > }
> >
> >
> > What does everyone think?
> >
> > >From: "Javier Turek" <jav...@ci...>
> > >Reply-To: "Javier Turek" <jav...@ci...>
> > >To: "DEVC" <dev...@li...>
> > >Subject: [Dev-C++] Memory
> > >Date: Thu, 4 Jul 2002 01:53:31 -0300
> > >
> > >Hello!
> > >Does somebody know of a library to use with dev-c++
> > >to control what is going on with the memory?
> > >I mean that I want to know how much memory
> > >I use for my variables and pointers, and at the end
> > >of my program I must know if all the memory I asked for,
> > >has been freed.
> > >
> > >Thanks.
> > >Javier
> > >from Argentina
> > >
> >
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
|
|
From: Ioannis V. <no...@ho...> - 2002-07-04 18:43:52
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Daniel Glenfield
> Sent: Thursday, July 04, 2002 3:37 PM
> To: jav...@ci...; dev...@li...
> Subject: Re: [Dev-C++] Memory
>
>
> Ok I'm a little lost...
> you can't destroy classes, only instances of classes
> (objects). If you
> destroy the object the memory is released - I assume by
> "losing memory" you
> mean memory is not deallocated i.e. made available.
>
> does anyone know if it is possible to allocate X memory for
> an application
> and only let it use that much? I'm thinking that X MB of
> memory could be
> allocated then at the end of the program check to see if it
> is all available
> again.
>
> like if ( CurrentMem == InitialMem )
> {
> cout << "Memory has been managed perfectly.";
> }
There is no need for such mess in the first place. Use the standard
library containers instead of manually allocating and deallocating
memory, and using built in arrays. For normal situations use the
standard library vector.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|
|
From: Daniel G. <sp...@ho...> - 2003-03-01 15:19:58
|
For C++ you must delete any objects you create with the new operator.
Variables created normally will be destroyed when they go out of scope.
So
int main(int argc, char** argv)
{
int SomeInt = 0;
return 0;
}
// SomeInt has been destroyed.
or try
int main(int argc, char** argv)
{
{
// The { doesn;t do anything except limit
// the scope of everything within.
int SomeInt = 0;
} // someInt now destroyed.
return 0;
}
but if you do
MyClass* Ptr; // Ptr can point to a MyClass object
Ptr = new Myclass;
//...
delete Ptr; // Delete the object and free the memory.
This is because with 'new' you allocate memory at run time, not at compile
time.
>From: "Robson Bernard" <rob...@su...>
>To: "C++ Mail List" <dev...@li...>
>Subject: [Dev-C++] Memory
>Date: Sat, 1 Mar 2003 11:47:26 -0300
>
>Hi coders,
>
>I have a question that does not want to shut up! When i code a program, do
>i
>have to delete all variables before exiting the main function or does the
>operating system cares of free all the memory allocated? If i do not delete
>the variables before main function and use the program several times, may
>it
>cause the system memory to be unavailble?
>
>Any input welcome!
>
>Thanks!
>
>
>
>-------------------------------------------------------
>This sf.net email is sponsored by:ThinkGeek
>Welcome to geek heaven.
>http://thinkgeek.com/sf
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
It's fast, it's easy and it's free. Get MSN Messenger today!
http://messenger.msn.co.uk
|
|
From: Robson B. <rob...@su...> - 2003-03-01 15:28:00
|
Thanks for input, people! |