473,699 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2911
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.singne t.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<cha r*>(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<cha r*>(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**********@gm ail.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
4026
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 construct ptr = realloc(ptr, size); *ptr = malloc(string_length); strncpy(ptr, src, string_length); to call realloc() multiple times. This should be ok, right?
7
2513
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 since I want to be able to use this code, I can't discount the chance that the key or the value are static values on the stack. If this is the case, then free() will (obviously) fail. On Solaris <sys/ucontext.h> has a function stack_inbounds that...
12
3273
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 and the POSIX stuff is non standard but this is how I stumbled onto this question. #include <INTEGRITY.h> #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <signal.h>
6
2829
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 expression and can be used in global namespace/file scope. Which part of the standard says or describes this . #include<stdio.h> #include<stdlib.h>
68
15683
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; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
82
31092
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 currently implementing, NULL is returned. Is it wrong ?)
58
4655
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 the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
11
1840
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 know using "new" is preferable, but for now I have to use what is provided. Second, do FILE pointers need to have memory allocated? My specific problem is this: (all code examples are abbreviated for clarity)
173
8104
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. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7750
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.