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

Why malloc get stucked?

Hi All,
I'm facing a problem with malloc. I have a program which opens a file
to write something using ofstream.
I used to run more than a process same time and in that case one of the
process get stucked in malloc.

ofstream* _file ;
_file = NULL ;

if(access("filename", 0) == 0)
{
cout << "File already exists" ;
return ;
}
_file = new ofstream("filename", ios::trunc) ;
if(!_file->good)
{
stderr("Error in file open") ;
}

The process get stucked during the "new ofstream".
I'm working in unix. I tried mcheck/mtrace and many other, but still
can;t solve it.
The process stack is:
Thread 1 (Thread 182905645888 (LWP 29543)):
#0 0x0000002a95d91713 in _int_malloc () from /lib64/tls/libc.so.6
#1 0x0000002a95d905d2 in malloc () from /lib64/tls/libc.so.6
#2 0x0000002a95a54da5 in operator new () from
/usr/lib64/libstdc++.so.5
#3 0x0000000000436305 in MyFile::openFile ()
Can anybody help in this regard?

Thanks,
Subha

Dec 7 '06 #1
5 1731

Subha wrote:
Hi All,
I'm facing a problem with malloc. I have a program which opens a file
to write something using ofstream.
I used to run more than a process same time and in that case one of the
process get stucked in malloc.

ofstream* _file ;
_file = NULL ;
You should combine the two statements to
ofstream* _file = NULL;

That way it's harder to inadvertently remove the assignment of NULL and
leave you with an uninitialized pointer. (This is just a style thing,
not the cause of your problem.)
>
if(access("filename", 0) == 0)
{
cout << "File already exists" ;
return ;
}
_file = new ofstream("filename", ios::trunc) ;
And if the code you're showing is really a good representative, just
move the declaration of _file to here:
ofstream* _file = new ofstream(...);

This does a couple things. One, it moves the declaration closer to the
use, which (IMO) makes understanding easier because it limits the
scope. Two, you never have an invalid _file pointer at all. (Again,
just a style thing.)

For that matter, why not create _file on the stack? (There could easily
be a reason why you don't, but even in that case consider a smart
pointer of some variety.) This would probably fix the problem you're
having right now, but something else would almost certainly break
sooner or later.
if(!_file->good)
{
stderr("Error in file open") ;
}

The process get stucked during the "new ofstream".
I'm working in unix. I tried mcheck/mtrace and many other, but still
can;t solve it.
The process stack is:
Thread 1 (Thread 182905645888 (LWP 29543)):
#0 0x0000002a95d91713 in _int_malloc () from /lib64/tls/libc.so.6
#1 0x0000002a95d905d2 in malloc () from /lib64/tls/libc.so.6
#2 0x0000002a95a54da5 in operator new () from
/usr/lib64/libstdc++.so.5
#3 0x0000000000436305 in MyFile::openFile ()
Can anybody help in this regard?
But to answer your question... it's probably a heap corruption issue. I
don't see anything wrong with the code you presented, and even if there
was there should essentially never be a crash when calling new.

So there's probably an out-of-bounds reference in a heap-allocated
array, a use of an uninitialized pointer, something like that which is
causing your heap to go bad. There are programs that can help you find
errors like this, such as I think Purify and Valgrind, though my
experience is sorely lacking exposure to these tools so I can't testify
to them being able to do exactly what you want.

Evan
>
Thanks,
Subha
Dec 7 '06 #2
Hi Evan,
Thanks for your reply.
I did all but still having the same problem. It's purify clean.
Colud you pleas etell me possible reasons of this? When I run thae same
process in solaris it's completely OK. Problem I'm facing when it's on
Linux.
Thanks,
subha

Evan wrote:
Subha wrote:
Hi All,
I'm facing a problem with malloc. I have a program which opens a file
to write something using ofstream.
I used to run more than a process same time and in that case one of the
process get stucked in malloc.

ofstream* _file ;
_file = NULL ;

You should combine the two statements to
ofstream* _file = NULL;

That way it's harder to inadvertently remove the assignment of NULL and
leave you with an uninitialized pointer. (This is just a style thing,
not the cause of your problem.)

if(access("filename", 0) == 0)
{
cout << "File already exists" ;
return ;
}
_file = new ofstream("filename", ios::trunc) ;

And if the code you're showing is really a good representative, just
move the declaration of _file to here:
ofstream* _file = new ofstream(...);

This does a couple things. One, it moves the declaration closer to the
use, which (IMO) makes understanding easier because it limits the
scope. Two, you never have an invalid _file pointer at all. (Again,
just a style thing.)

For that matter, why not create _file on the stack? (There could easily
be a reason why you don't, but even in that case consider a smart
pointer of some variety.) This would probably fix the problem you're
having right now, but something else would almost certainly break
sooner or later.
if(!_file->good)
{
stderr("Error in file open") ;
}

The process get stucked during the "new ofstream".
I'm working in unix. I tried mcheck/mtrace and many other, but still
can;t solve it.
The process stack is:
Thread 1 (Thread 182905645888 (LWP 29543)):
#0 0x0000002a95d91713 in _int_malloc () from /lib64/tls/libc.so.6
#1 0x0000002a95d905d2 in malloc () from /lib64/tls/libc.so.6
#2 0x0000002a95a54da5 in operator new () from
/usr/lib64/libstdc++.so.5
#3 0x0000000000436305 in MyFile::openFile ()
Can anybody help in this regard?

But to answer your question... it's probably a heap corruption issue. I
don't see anything wrong with the code you presented, and even if there
was there should essentially never be a crash when calling new.

So there's probably an out-of-bounds reference in a heap-allocated
array, a use of an uninitialized pointer, something like that which is
causing your heap to go bad. There are programs that can help you find
errors like this, such as I think Purify and Valgrind, though my
experience is sorely lacking exposure to these tools so I can't testify
to them being able to do exactly what you want.

Evan

Thanks,
Subha
Dec 8 '06 #3
I've seen this problem before with old Solaris compilers. The
run-time library isn't necessarily initialized if you have a
strange combination of other run-time libraries. That doesn't
help you much in the Linux world, but my guess is the problem is
related to how you compiled and linked in the 64-bit linux world.

Could you please post your makefile's linking step and possibly
the main()?

/Glen
Subha wrote:
Hi Evan,
Thanks for your reply.
I did all but still having the same problem. It's purify clean.
Colud you pleas etell me possible reasons of this? When I run thae same
process in solaris it's completely OK. Problem I'm facing when it's on
Linux.
Thanks,
subha

Evan wrote:
>Subha wrote:
>>Hi All,
I'm facing a problem with malloc. I have a program which opens a file
to write something using ofstream.
I used to run more than a process same time and in that case one of the
process get stucked in malloc.

ofstream* _file ;
_file = NULL ;
You should combine the two statements to
ofstream* _file = NULL;

That way it's harder to inadvertently remove the assignment of NULL and
leave you with an uninitialized pointer. (This is just a style thing,
not the cause of your problem.)
>>if(access("filename", 0) == 0)
{
cout << "File already exists" ;
return ;
}
_file = new ofstream("filename", ios::trunc) ;
And if the code you're showing is really a good representative, just
move the declaration of _file to here:
ofstream* _file = new ofstream(...);

This does a couple things. One, it moves the declaration closer to the
use, which (IMO) makes understanding easier because it limits the
scope. Two, you never have an invalid _file pointer at all. (Again,
just a style thing.)

For that matter, why not create _file on the stack? (There could easily
be a reason why you don't, but even in that case consider a smart
pointer of some variety.) This would probably fix the problem you're
having right now, but something else would almost certainly break
sooner or later.
>>if(!_file->good)
{
stderr("Error in file open") ;
}

The process get stucked during the "new ofstream".
I'm working in unix. I tried mcheck/mtrace and many other, but still
can;t solve it.
The process stack is:
Thread 1 (Thread 182905645888 (LWP 29543)):
#0 0x0000002a95d91713 in _int_malloc () from /lib64/tls/libc.so.6
#1 0x0000002a95d905d2 in malloc () from /lib64/tls/libc.so.6
#2 0x0000002a95a54da5 in operator new () from
/usr/lib64/libstdc++.so.5
#3 0x0000000000436305 in MyFile::openFile ()
Can anybody help in this regard?
But to answer your question... it's probably a heap corruption issue. I
don't see anything wrong with the code you presented, and even if there
was there should essentially never be a crash when calling new.

So there's probably an out-of-bounds reference in a heap-allocated
array, a use of an uninitialized pointer, something like that which is
causing your heap to go bad. There are programs that can help you find
errors like this, such as I think Purify and Valgrind, though my
experience is sorely lacking exposure to these tools so I can't testify
to them being able to do exactly what you want.

Evan
>>Thanks,
Subha
Dec 8 '06 #4
Subha wrote:
Hi Evan,
Thanks for your reply.
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Dec 8 '06 #5
Glen Dayton wrote:
I've seen this problem before with old Solaris compilers. The
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Dec 8 '06 #6

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

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
59
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h>...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
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;...
4
by: omer.mush | last post by:
Hi, I am using ASP.Net 2.0 along with C# and Atlas. I have some image panels alongwith asp:panel controls. I am using image panels as tabs, when user clicks a specific portion of the image, I...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...

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.