tags:

views:

56

answers:

2

I have a program that goes n^2 layers deep of recursion and mallocs a bunch of memory to concatenate char*s together. With a large enough n, the process just gets killed by the server (since it is consuming too much memory). How can I release this memory and still have my data?

It's mallocs look like

test = (char *)malloc(sizeof(char) * 256);

I need this data straight until the end of the program. How can I deal with this?

A: 

Without thinking about it deeper, why do you need all of the data in memory? Several things you need to do:

  1. Write the char's to file
  2. use a global "master" char array, and only malloc for that, reuse the memory for the next segment. If this is your present setup, see 1
  3. Use valgrind to see if you're leaking all that memory.
rubenvb
+1  A: 

You can't. Once you release the memory the data is gone.

What you may be able to do is to make better use of the memory you have available. With the code you posted, I can't think of a way to help you manage the memory any better though

pmg