473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

malloc fails if the function is called twice, how?

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

May 21 '06 #1
7 2904
Novice wrote:
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.

--
Ian Collins.
May 21 '06 #2
Novice wrote:
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.

May 21 '06 #3
"Novice" <no****@novice.com> wrote in message
news:e4**********@mawar.singnet.com.sg
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.

--
John Carson
May 21 '06 #4
Novice wrote:
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.

May 21 '06 #5
Novice wrote:
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 21 '06 #6
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.

May 24 '06 #7
fr**********@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
7
by: Ian Roddis | last post by:
Hi all, I've written some code to make a hash data structure and associated funtions (insert, delete, search). In the delete function, I want to free() the key and the associated value. But...
12
by: gooch | last post by:
I originally posted the following code in a group discussing threads but after further research I think I have a c question about the code. I know there are a couple of non standard c includes here...
6
by: niklaus | last post by:
Hi, I have seen that the following code compiles in some environments like devc++ but fails on some env's like gcc on linux. Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
82
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
11
by: Jason | last post by:
I've inherited some old C code that I've been tasked to convert to C++. I've been running into some problems with memory allocation. First of all, is there any problems using "malloc" in C++? I...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.