malloc fails if the function is called twice, how?

N

Novice

When Main calls a function, which has a couple of malloc() statements, and
their corresponding free() statements, why did it crash?

How should I fix this problem?

TIA
 
I

Ian Collins

Novice said:
When Main calls a function, which has a couple of malloc() statements, and
their corresponding free() statements, why did it crash?

How should I fix this problem?
Show the code! There could be any number of reasons.
 
R

Rolf Magnus

Novice said:
When Main calls a function, which has a couple of malloc() statements, and
their corresponding free() statements, why did it crash?

Probably because you made an error.
How should I fix this problem?

By removing the error.
 
J

John Carson

Novice said:
When Main calls a function, which has a couple of malloc()
statements, and their corresponding free() statements, why did it
crash?
How should I fix this problem?

TIA


The problem is on line 47 of your code.
 
R

Ron Natalie

Novice said:
When Main calls a function, which has a couple of malloc() statements, and
their corresponding free() statements, why did it crash?

How should I fix this problem?
Look carefully. Crashes in malloc/free are a result of some undefined
behavior corrupting the malloc internal tables. Most likely reasons
are:

1. Freeing a value not directly obtained from malloc.

char* x = static_cast<char*>(malloc(10));
x++;
free(x);

or

char x;
free(&x);

2. Freeing a value twice.

void* p = malloc(20);
free(p);
free(p);

3. Writing off the end of a malloc'd bloc

char* x = static_cast<char*>(malloc(4));
strcpy(x, "abcd");

Some compilers have debug tools built in to help you troubleshoot this
and there are some external third party tools available. However,
usually so serious introspection of your code will help.

Frankly, if you are a new C++ programmer (or perhaps even an old one),
the fact that you are messing with malloc at all means you have serious
problems in your design approach.
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Novice said:
When Main calls a function, which has a couple of malloc() statements, and
their corresponding free() statements, why did it crash?

How should I fix this problem?

1) Delete your code completely and the error disappears.

2) You could also just change it to:

#include <iostream>
using namespace std;

int main()
{
cout << "Hi Mom! My memory allocation error disappeared now!!!\n";
return 0;
}


Best regards / Med venlig hilsen
Martin Jørgensen
 
F

frankgerlach

I am suprised none of the responding people mentioned the use of a
memory checker such as Purify (www.ibm.com, trial version available) or
valgrind. The latter is a free tool and very valuable when debugging
memory problems.
 
V

Victor Bazarov

I am suprised none of the responding people mentioned the use of a
memory checker such as Purify (www.ibm.com, trial version available)
or valgrind. The latter is a free tool and very valuable when
debugging memory problems.

....unless your problems are on Windows or any other platform where it
is not available. Generally speaking, _if_ tools exist, use it. But
what if it doesn't? Perhaps that's why people here refrain from giving
platform-specific product recommendations.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top