how to prevent the allocation of previously used memory by malloc?

S

sunyin

Dear experts,

When i tried to use "malloc" in a program to allocate memory space, i
notice that sometimes it allocates memory that is already in use and
the stored data is lost. Could any expert give me some advice on how
to avoid this? Many thanks!

sunyin
 
J

Jon Bell

When i tried to use "malloc" in a program to allocate memory space, i
notice that sometimes it allocates memory that is already in use and
the stored data is lost.

That should not happen. Can you post a short complete program that
demonstrates this behavior, along with its output?
 
J

John Harrison

sunyin said:
Dear experts,

When i tried to use "malloc" in a program to allocate memory space, i
notice that sometimes it allocates memory that is already in use and
the stored data is lost. Could any expert give me some advice on how
to avoid this? Many thanks!

sunyin

malloc does not do that. You have a bug in your program.

If you can't find the bug yourself then remove as much code as you can so
that you have a small program that still has the bug and then post the whole
program here.

john
 
S

sunyin

this does not always happen, but it does happen sometimes. i was
trying to build a tree and used malloc to create a node each time it
is added to the tree. but an char** created using "new" was detroyed.
could it be the problem with "new"?

Thanks!
 
J

John Harrison

sunyin said:
this does not always happen, but it does happen sometimes. i was
trying to build a tree and used malloc to create a node each time it
is added to the tree. but an char** created using "new" was detroyed.
could it be the problem with "new"?

Thanks!

No, it means you have a bug in your code. Neither new nor malloc operates
like that. Why not post the code?

john
 
K

Karl Heinz Buchegger

sunyin said:
this does not always happen, but it does happen sometimes. i was
trying to build a tree and used malloc to create a node each time it
is added to the tree. but an char** created using "new" was detroyed.
could it be the problem with "new"?

Theoretically it could be. After all 'new' is implemented
by some code and there could be a bug that was not detected
by the programmers of your library.

Practically: no. It could not be. Libraries shiped with a compiler
are extensively tested and while there might be some bugs in some
obscure areas of the library, the memory management part is of
such a central importance, that practically all bugs are discovered
during testing.

The much more likely reason is: There is a bug in *your* program.
A typical scenary would be: You need to make (and fix) at least 1 million
bugs in your own code, before you discover one in the base library.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top