test
test

Reputation: 1

So much memory being malloc'd that I get "Killed" when running my program deep enough

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?

Upvotes: 0

Views: 554

Answers (2)

pmg
pmg

Reputation: 108986

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

Upvotes: 1

rubenvb
rubenvb

Reputation: 76795

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.

Upvotes: 0

Related Questions