freeing memory?

my program is leaking memory all over the place!

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 5037 Views - Last Post: 06 September 2006 - 09:00 AM Rate Topic: -----

#1 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

freeing memory?

Posted 31 August 2006 - 03:52 PM

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.


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?

Is This A Good Question/Topic? 0
  • +

Replies To: freeing memory?

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: freeing memory?

Posted 31 August 2006 - 05:04 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

Re: freeing memory?

Posted 31 August 2006 - 05:38 PM

char * i have used alot of char *'s

also i have started using pointers to structs
Was This Post Helpful? 0
  • +
  • -

#4 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: freeing memory?

Posted 31 August 2006 - 05:46 PM

i don't see any character pointers in the code you posted.
Was This Post Helpful? 0
  • +
  • -

#5 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

Re: freeing memory?

Posted 31 August 2006 - 07:15 PM

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

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 August 2006 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: freeing memory?

Posted 31 August 2006 - 07:24 PM

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).
Was This Post Helpful? 0
  • +
  • -

#7 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: freeing memory?

Posted 31 August 2006 - 07:24 PM

hello should be freed up as it goes out of context,
Was This Post Helpful? 0
  • +
  • -

#8 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: freeing memory?

Posted 31 August 2006 - 07:25 PM

again no leakage there..

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

delete [] s; // free the memory



thats how you dynamically alocate and free memory in C++.
Was This Post Helpful? 0
  • +
  • -

#9 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: freeing memory?

Posted 01 September 2006 - 06:48 AM

View Posthelp-linux, on 1 Sep, 2006 - 07:45 AM, said:

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

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.


View PostNova Dragoon, on 1 Sep, 2006 - 07:54 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#10 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

Re: freeing memory?

Posted 01 September 2006 - 03:54 PM

View Postborn2c0de, on 1 Sep, 2006 - 06:48 AM, said:

View Posthelp-linux, on 1 Sep, 2006 - 07:45 AM, said:

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

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.


View PostNova Dragoon, on 1 Sep, 2006 - 07:54 AM, said:

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?
Was This Post Helpful? 0
  • +
  • -

#11 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: freeing memory?

Posted 01 September 2006 - 03:56 PM

not run a program for months
Was This Post Helpful? 0
  • +
  • -

#12 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

Re: freeing memory?

Posted 01 September 2006 - 04:27 PM

i mite get it to restart after 3 days or so. is there anyway to detect how much memory it is using?
Was This Post Helpful? 0
  • +
  • -

#13 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: freeing memory?

Posted 02 September 2006 - 12:02 AM

If your program is going to be up for months, there will always be a phase for the program where it simply waits and does no work (dormant phase).
You can free up variables in that phase and allocate memory when you need them again.

And you don't always have to store data in variables while your program is running.
You can store it in a data file and free the variavles, and read from it again when required.
This will control your memory leak problem and will prevent unnecessary usage of memory.
Was This Post Helpful? 0
  • +
  • -

#14 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

Re: freeing memory?

Posted 02 September 2006 - 01:04 AM

hmm, i dont think i can do that because at the moment all variable(when it has fully started") go out of scope anyway.

my real problem is, in a "parse" function where everytime called "leaks" and because of the nature of my program it is never dorment.(at its lowest, it is called every 30 seconds)

my program also interacts with another program to get most of its variables and all these are different everytime it is called.

i may be getting the term "memory leak" wrong be my program uses more memory when this function is called.
Was This Post Helpful? 0
  • +
  • -

#15 violent_crimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 06

Re: freeing memory?

Posted 02 September 2006 - 06:29 AM

int nLength = 64;

char *szChar;
szChar = new char [nLength+1];

//we now have dynamically allocated a char array;
//the char array has 64 places;

delete [] szChar;
szChar = NULL;


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2