473,397 Members | 2,056 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,397 software developers and data experts.

malloc() fail

Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get the
following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff]
../ka[0x804d3e4]
../ka[0x804b72a]
../ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string, and a
pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes
*/
d->data = (short*)malloc(sizeof(short));

/*
Set the fist byte of the string to zero, the terminating 0 byte.
*/
d->size = 1;
memset(d->data, 0, sizeof(short));

/*
And append the other char array, with the append function.
*/
append(other);
}

I totally don't get my mistake. Does anyone of you knows what I am doing
wrong?
Sep 21 '08 #1
11 2478
The Doctor wrote:
I totally don't get my mistake. Does anyone of you knows what I am doing
wrong?
Without seeing the entire code it may be difficult to say. There may
be an error in your memory handling, string resizing, or anything.
Sep 21 '08 #2
On 2008-09-21 13:02, The Doctor wrote:
Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get the
following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff]
./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string, and a
pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes
*/
d->data = (short*)malloc(sizeof(short));
I can't see anything wrong with the code so far, you need to post (at
least) the definition of Data (please see the FAQ on how to post a
question).

A question though, why use malloc and not new?

--
Erik Wikström
Sep 21 '08 #3
On Sun, 21 Sep 2008 12:08:34 +0000, Erik Wikström wrote:
On 2008-09-21 13:02, The Doctor wrote:
>Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get
the following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff] ./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string,
and a
> pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes */
d->data = (short*)malloc(sizeof(short));

I can't see anything wrong with the code so far, you need to post (at
least) the definition of Data (please see the FAQ on how to post a
question).

A question though, why use malloc and not new?
Data:
struct Data
{
int size;
short* data
};
Would new and delete[] have prevented this then?

It is impossible to post the whole code, since it is huge.
Sep 21 '08 #4
The Doctor wrote:
Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get the
following error:
new String(""); returns a pointer, but String string; is not a pointer,
so you cannot assign one to the other.
Anyway, why do you new the String? Whats wrong with this:

String str("blahblahblah");

You seem to have a Java or .NET background.
*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
You corrupted some memory, perhaps by deleting memory twice or by
writing over the bound of an array.
The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string, and a
pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));
Any reason the Data structure is not part of the String class itself?

class String
{
// constructors and member functions...
protected:
size_t length;
short* data;
};
/*
Allocate new data space, this is where the application crashes
*/
d->data = (short*)malloc(sizeof(short));
malloc is a C function. C has a rule of thumb: NEVER cast the result of
malloc. Why? Because it masks bugs in your code. In C, you don't need
the cast.

But in C++, we can't use malloc without a cast, what to do? Don't use
malloc, use new[]!

With the above data structure in the class, you would do:

length = 0; // let length be the real string length
data = new short[length + 1]; // +1 for terminating 0
data[length] = '\0';

Don't forget to delete[] the data in the destructor!

Another common mistake is to forget the Rule of Three:
Whenever you have to define a destructor, a copy constructor or an copy
assignment operator, you most likely have to define the other two.

Did you define a copy constructor and an assignment operator?
/*
Set the fist byte of the string to zero, the terminating 0 byte.
*/
d->size = 1;
memset(d->data, 0, sizeof(short));
memset should rarely be used in C++, too.
/*
And append the other char array, with the append function.
*/
append(other);
}

I totally don't get my mistake. Does anyone of you knows what I am doing
wrong?
You posted too less code.
You didn't use the proper C++ operations.

There are also std::string, std::wstring and std::vector, which would
take care of the memory issues.

--
Thomas
Sep 21 '08 #5
The Doctor wrote:
On Sun, 21 Sep 2008 12:08:34 +0000, Erik Wikström wrote:
>On 2008-09-21 13:02, The Doctor wrote:
>>Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get
the following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
It means you have a memory error, like e.g. writing past the end of an array
or trying to free a block of memory you already have freed, or calling free
with an address you didn't get from malloc.
>>0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff] ./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string,
and a
>>pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes */
d->data = (short*)malloc(sizeof(short));
A typical problem of this kind of bug is that the program often doesn't
crash at the place where the actual error is. It's likely that the last
memory allocation or deallocation you did before this one broke the memory
manager's internal data. If you happen to be using linux/x86, you can try
using valgrind. It's a memory debugger that can tell you in which line of
your code the real error is.
>I can't see anything wrong with the code so far, you need to post (at
least) the definition of Data (please see the FAQ on how to post a
question).

A question though, why use malloc and not new?

Data:
struct Data
{
int size;
short* data
};
That doesn't really help much.
Would new and delete[] have prevented this then?
Probably not.
It is impossible to post the whole code, since it is huge.
You sould rather reduce your program to the smallest complete program that
still contains the error. Chances are that you will find the error in the
process. If not, you can post that reduced version of the program.

Sep 21 '08 #6
On Sep 21, 1:02 pm, The Doctor <do....@email.mewrote:
Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also
writing an application, which uses the string class, and uses
a lot of them. But, after about thirty String string = new
String("blahblahblah"), I get the following error:
*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff]
./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]
The code where the app crashes:
String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string,and a
pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));
Why not "new Data", instead of malloc? Or even "new Data()",
ensuring zero initialization.
/*
Allocate new data space, this is where the application crashes
*/
d->data = (short*)malloc(sizeof(short));
This looks very strange. Why not just declare a short in Data,
rather than a pointer to it? (The pointer might make sense if
you were pointing to more than one, but even then, unless the
goal is to learn how to do low level memory management,
std::vector would be preferrable.)
/*
Set the fist byte of the string to zero, the terminating 0 byte.
*/
d->size = 1;
memset(d->data, 0, sizeof(short));
Why memset? Why not simply "*d->data = 0 ;"?
/*
And append the other char array, with the append function.
*/
append(other);
}
I totally don't get my mistake. Does anyone of you knows what
I am doing wrong?
Probably a lot of things; most of the code above looks funny.
But without seeing more (e.g. the append function, the structure
of Data, and above all, the class invariants), it's impossible
to say.

Note too that a memory corruption error in malloc will be
detected after (and often long after) the corruption actually
took place, and often in a completely different component.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 22 '08 #7
On Sep 21, 3:07 pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
The Doctor wrote:
[...]
d->data = (short*)malloc(sizeof(short));
malloc is a C function. C has a rule of thumb: NEVER cast the
result of malloc. Why? Because it masks bugs in your code. In
C, you don't need the cast.
But in C++, we can't use malloc without a cast, what to do?
Don't use malloc, use new[]!
If he's allocating sizeof(short), and assigning it to a short*
(which seems to be the case), it's new he should use, and not
new[].

Thinking about it, I wonder if he isn't trying to use the dirty
struct hack. And that what he really wants to do is malloc(
sizeof( short ) + numberOfCharactersInString ).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 22 '08 #8
James Kanze wrote:
On Sep 21, 3:07 pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
>The Doctor wrote:

[...]
>> d->data = (short*)malloc(sizeof(short));
>malloc is a C function. C has a rule of thumb: NEVER cast the
result of malloc. Why? Because it masks bugs in your code. In
C, you don't need the cast.
>But in C++, we can't use malloc without a cast, what to do?
Don't use malloc, use new[]!

If he's allocating sizeof(short), and assigning it to a short*
(which seems to be the case), it's new he should use, and not
new[].
If he's allocating only a single sizeof(short) and assigning to a
short*, he shouldn't use dynamic allocation.

My guess is that its the empty string (zero terminator only). The data
structure is a pimpl (for whatever reason) and the data member of this
struct is a wide string pointer. In that case, he will allocate
sizeof(short) * (length+1) memory. The +1 is for the terminating zero:

The Doctor wrote:
/*
Set the fist byte of the string to zero, the terminating 0 byte.
*/
d->size = 1;
memset(d->data, 0, sizeof(short));
James Kanze wrote:
Thinking about it, I wonder if he isn't trying to use the dirty
struct hack. And that what he really wants to do is malloc(
sizeof( short ) + numberOfCharactersInString ).
Another possible interpretation.

--
Thomas
Sep 22 '08 #9
The Doctor wrote:
Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get the
following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff]
./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string, and a
pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes
*/
d->data = (short*)malloc(sizeof(short));

/*
Set the fist byte of the string to zero, the terminating 0 byte.
*/
d->size = 1;
memset(d->data, 0, sizeof(short));

/*
And append the other char array, with the append function.
*/
append(other);
}

I totally don't get my mistake. Does anyone of you knows what I am doing
wrong?

Show us what String::~String looks like..

BTW - what's wrong with :

typedef std::basic_string<shortString;
String foo( other, other + strlen(other) );

??? i.e. get rid of your own String class completely?
Sep 28 '08 #10
Gianni Mariani wrote:
The Doctor wrote:
>Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get
the following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff]
./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string, and
a pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes
*/
d->data = (short*)malloc(sizeof(short));

/*
Set the fist byte of the string to zero, the terminating 0 byte.
*/
d->size = 1;
memset(d->data, 0, sizeof(short));

/*
And append the other char array, with the append function.
*/
append(other);
}

I totally don't get my mistake. Does anyone of you knows what I am
doing wrong?


Show us what String::~String looks like..

BTW - what's wrong with :

typedef std::basic_string<shortString;
1) It's not as much fun. Fun was the OP's stated goal.

2) The standard doesn't define char_traits specializations for anything
other than char and wchar_t. char_traits<shortcould mean different
things with different standard library implementations, and so could
basic_string<short>.
String foo( other, other + strlen(other) );

??? i.e. get rid of your own String class completely?
Sep 28 '08 #11
On Sep 28, 5:37 am, Jeff Schwab <j...@schwabcenter.comwrote:
Gianni Mariani wrote:
[...]
BTW - what's wrong with :
typedef std::basic_string<shortString;
1) It's not as much fun. Fun was the OP's stated goal.
2) The standard doesn't define char_traits specializations for anything
other than char and wchar_t. char_traits<shortcould mean different
things with different standard library implementations, and so could
basic_string<short>.
For that matter, the standard doesn't require a generic
implementation char_traits at all, in which case,
basic_string<shortwon't even compile. (Most compilers do
provide one, but I've heard actual complaints that the semantics
it defines are not the same, at least for unsigned integral
types, with g++ and VC++.)

When all is said and done, all making std::basic_string a
template bought us was to make it easier for the standards
committee to add new string types down the road, e.g.
std::basic_string< char16_t (which will be present in the next
version of the standard).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 28 '08 #12

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

Similar topics

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...
25
by: H.A. Sujith | last post by:
If malloc fails what should I do? 1. Exit imediately. 2. Print an error message (or put a log entry) and exit. 3. Print an error message (or put a log entry) and continue execution (after...
27
by: Chess Saurus | last post by:
I'm getting a little bit tired of writing if (a = malloc(...) == NULL) { // error code } I mean, is it really possible that a malloc call could fail, except in the case of running out of...
54
by: Neo | last post by:
Hi Folks, I've a simple qestion related to dynamic memory allocation in C here is the code: #include <stdio.h> int main() {
14
by: Marlene Stebbins | last post by:
At one point in my program I have about a dozen calls to malloc. I want to check for malloc failure, but I don't want to write: if((buffer_x = malloc(BUFSIZE * sizeof(*buffer_x))) == NULL) {...
13
by: Mehta Shailendrakumar | last post by:
Hi all, What is the significance of "malloc(0)"? It doesn't return a NULL pointer, then what does it return? Where one can use such a "malloced" pointer? Thanks for help. Regards,...
40
by: ramu | last post by:
Hi, what happens when i run the below code? main() { int *p; while(1) p= (int *)malloc(1000); } Do i get segmentation fault?
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
34
by: niranjan.singh | last post by:
This is regarding to test an SDK memory stuff. In what situation malloc gets fail. any comment/reply pls.... regards
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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.