OK, so I deleted that line. But I'm pretty sure that's nothing to do with the initial problem. I tried this as a solution to that problem
OK, so I deleted that line. But I'm pretty sure that's nothing to do with the initial problem. I tried this as a solution to that problem
> Here's the complete code of the function to make clear what I want it to do:
Unfortunately, we really need the complete code to find where you're trashing memory. An isolated function which seems correct says nothing about anything else you're doing.
When you're dealing with memory corruption, "cause" and "effect" are very different things. Where the problem shows up is often nothing to do with what actually caused it.
What on earth are you trying to achieve with this?
Code:if(!(formula = malloc(21))) { free(formula);//free up resources if(!(formula = malloc(21))) exit(-1); }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thanks for all your suggestions/answers.
Unfortunately I can't post the complete code, for two reasons. 1) It's ~6500 lines 2) It's a pretty unique program that gives my company a big advantage over other companies (if it works, that is >.< )Originally Posted by Salem
I have no idea what to do now...
I tried this as it was a suggestion by King Mir here: http://cboard.cprogramming.com/showp...93&postcount=4Originally Posted by Salem
I took it as a second try for memory allocation??
Last edited by rkooij; 10-09-2006 at 07:40 AM.
Free up other resources. If malloc fails, that means you have allocated too much memmory. If you free some of that memmory, you can try malloc again. If it still does not work, your only option is to exit the program. If you have no other resources to free, then you can just quit.Originally Posted by rkooij
But really for small programs, malloc will not fail unless A) You're running a lot of memmory intensive programs at the same time(unlikely) B) You allocate an unreasonably huge continuous array. C) you have a memmory leak. A memmory leak is when you keep allocating a block of memmory multiple times without freeing it.
I gave that suggestion because I could see nothing else wrong with your code. You can print a character or something to test if malloc is really failing or if the problem is elsewhere in your code. If it is failing, then you probably have a memmory leak somewhere, so make sure you free or realloc all memmory you allocate with malloc or calloc.
Last edited by King Mir; 10-09-2006 at 08:22 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
> Unfortunately I can't post the complete code, for two reasons. 1) It's ~6500 lines
We wouldn't want to wade through that much code either.
> I have no idea what to do now...
Well seeing as you're working for a company, get them to spend a few $$$ on something like Purify or boundschecker. These can take you direct to the root cause of the memory problem rather than you staring at the effect.
Put it this way, how many days have you spent on this issue already (@say $50 per hour to the company), and how much longer are you likely to take on this issue alone.
For linux users, try Electric Fence or Valgrind.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
King Mir, I think you've misunderstood something. It's not exit() that frees memory. It's free().Originally Posted by King Mir
"The Internet treats censorship as damage and routes around it." - John Gilmore
And I think you misunterstood what King Mir wanted to reach with this bit of code
Free up other resources. If malloc fails, that means you have allocated too much memmory. If you free some of that memmory, you can try malloc again. If it still does not work, your only option is to exit the program. If you have no other resources to free, then you can just quit.
Well, it would've been much easier to understand when he would've said that in the first place.
Anyway, I'm sorry for not reading his next replies.
"The Internet treats censorship as damage and routes around it." - John Gilmore
Sorry for being unclear.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
OK thanks guys. I've downloaded a trial of IBM PurifyPlus now and will see what it can do for me