Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,105 people online right now. Registration is fast and FREE... Join Now!




freeing memory?

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

freeing memory?, my program is leaking memory all over the place!

help-linux
31 Aug, 2006 - 02:52 PM
Post #1

D.I.C Head
Group Icon

Joined: 12 Aug, 2006
Posts: 54



Thanked: 1 times
Dream Kudos: 50
My Contributions
when i write a program i dont often use pointers unless a function needs one.

my problem is my programs leak memory all over the place, and i was wondering if it is possible to free up some of this memory.

this is an example of the code i use.

CODE


void some_function(string foo)
{
          string bar = foo;
          //do somthing with bar
          if(bar.length() > 10)
          {
                    bar.erase(0, 5);
          }

           some_other_function(bar);
}



how do i free up bar and foo?
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: Freeing Memory?
31 Aug, 2006 - 04:04 PM
Post #2

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
the code you have posted contains no pointers. the string class is a self-managed class so it handles its own memory automatically. i am not sure why you believe any memory leaks exist unless there is more code elsewhere where you are actually utalizing pointers.
User is offlineProfile CardPM
+Quote Post

help-linux
RE: Freeing Memory?
31 Aug, 2006 - 04:38 PM
Post #3

D.I.C Head
Group Icon

Joined: 12 Aug, 2006
Posts: 54



Thanked: 1 times
Dream Kudos: 50
My Contributions
char * i have used alot of char *'s

also i have started using pointers to structs
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: Freeing Memory?
31 Aug, 2006 - 04:46 PM
Post #4

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
i don't see any character pointers in the code you posted.
User is offlineProfile CardPM
+Quote Post

help-linux
RE: Freeing Memory?
31 Aug, 2006 - 06:15 PM
Post #5

D.I.C Head
Group Icon

Joined: 12 Aug, 2006
Posts: 54



Thanked: 1 times
Dream Kudos: 50
My Contributions
the code i posted was just an example, here you go:

CODE

char hello[100];
hello = "hello world";
cout << hello << endl;
//never use hello again


i justr want to know in general how to free up as much memory as possible.

This post has been edited by help-linux: 31 Aug, 2006 - 06:16 PM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Freeing Memory?
31 Aug, 2006 - 06:24 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
To explicitly destroy variables and objects in C++, one must explicitly declare them using the new operator, then destroy them with the subsequent delete operator. Having said that, the majority of one's memory leaks will come from objects, not mere variables.

The automatic garbage collection in C++ can be spoty, but it's better than java (well, then java used to be).
User is offlineProfile CardPM
+Quote Post

Nova Dragoon
RE: Freeing Memory?
31 Aug, 2006 - 06:24 PM
Post #7

The Innocent Shall Suffer, Big Time
Group Icon

Joined: 16 Aug, 2001
Posts: 6,132



Thanked: 7 times
Dream Kudos: 515
Expert In: Python, Linux

My Contributions
hello should be freed up as it goes out of context,
User is offlineProfile CardPM
+Quote Post

Mrafcho001
RE: Freeing Memory?
31 Aug, 2006 - 06:25 PM
Post #8

D.I.C Addict
Group Icon

Joined: 1 Nov, 2005
Posts: 753



Thanked: 5 times
Dream Kudos: 120
My Contributions
again no leakage there..

CODE

char *s = new char[100];
... use

delete [] s; // free the memory


thats how you dynamically alocate and free memory in C++.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Freeing Memory?
1 Sep, 2006 - 05:48 AM
Post #9

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE(help-linux @ 1 Sep, 2006 - 07:45 AM) *

the code i posted was just an example, here you go:

CODE

char hello[100];
hello = "hello world";
cout << hello << endl;
//never use hello again


i justr want to know in general how to free up as much memory as possible.


hello = "hello world";
I don't think this would compile...You'll need strcpy() to set the contents. Otherwise you get an LValue Required Error.


QUOTE(Nova Dragoon @ 1 Sep, 2006 - 07:54 AM) *

hello should be freed up as it goes out of context,

You are kinda right.
I disassembled the file and checked it out.
The new and delete operators call malloc() and free() to explicitly allocate/deallocate memory,

Mrafcho001's code doesn't do any of that, but rather loads the string offset in a register and is pushed onto the stack.
But the stack is cleared before the main() fn goes out of scope.

So it doesn't cause a memory leak.

Although using Data Structures without memory allocation will result in it.
User is offlineProfile CardPM
+Quote Post

help-linux
RE: Freeing Memory?
1 Sep, 2006 - 02:54 PM
Post #10

D.I.C Head
Group Icon

Joined: 12 Aug, 2006
Posts: 54



Thanked: 1 times
Dream Kudos: 50
My Contributions
QUOTE(born2c0de @ 1 Sep, 2006 - 06:48 AM) *

QUOTE(help-linux @ 1 Sep, 2006 - 07:45 AM) *

the code i posted was just an example, here you go:

CODE

char hello[100];
hello = "hello world";
cout << hello << endl;
//never use hello again


i justr want to know in general how to free up as much memory as possible.


hello = "hello world";
I don't think this would compile...You'll need strcpy() to set the contents. Otherwise you get an LValue Required Error.


QUOTE(Nova Dragoon @ 1 Sep, 2006 - 07:54 AM) *

hello should be freed up as it goes out of context,

You are kinda right.
I disassembled the file and checked it out.
The new and delete operators call malloc() and free() to explicitly allocate/deallocate memory,

Mrafcho001's code doesn't do any of that, but rather loads the string offset in a register and is pushed onto the stack.
But the stack is cleared before the main() fn goes out of scope.

So it doesn't cause a memory leak.

Although using Data Structures without memory allocation will result in it.


thanks for the replys, so when i use pointers to a "new" operator? expression? i need to use "delete[]" after it?

my program will be running for months without stopping so are there any other things i can do?
User is offlineProfile CardPM
+Quote Post

Mrafcho001
RE: Freeing Memory?
1 Sep, 2006 - 02:56 PM
Post #11

D.I.C Addict
Group Icon

Joined: 1 Nov, 2005
Posts: 753



Thanked: 5 times
Dream Kudos: 120
My Contributions
not run a program for months
User is offlineProfile CardPM
+Quote Post

help-linux
RE: Freeing Memory?
1 Sep, 2006 - 03:27 PM
Post #12

D.I.C Head
Group Icon

Joined: 12 Aug, 2006
Posts: 54



Thanked: 1 times
Dream Kudos: 50
My Contributions
i mite get it to restart after 3 days or so. is there anyway to detect how much memory it is using?
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 12/5/08 03:04AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month