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

bizarre malloc problem

First, let me announce that this is very possibly off-topic because
malloc is a specific third party accessory to c, etc. I spent about an
hour trying to find a more appropriate newsgroup and failed. If anyone
could point one out, I would be much obliged. I'm using MSVC .NET, but
the only .NET specific newsgroups I could find were vbasic newsgroups
and you'll agree a C malloc question is less off-topic here than there,
at least.

I seem to be suffering the exact opposite of fragmentation: I have no
problem whatsoever allocation a large array of structures, but if I try
to allocate room for just 1 single structure, the program immediately
throws an exception. Debugging indicates the exception is indeed
thrown by malloc itself and not by some later line of code.

I temporarily solved the problem as follows:
original code:

void myfunction( void )
{
struct mystructure *m;

m = (struct mystructure *) malloc( sizeof(struct mystructure) );

...
}

band-aid solution code:

#define MAX_MYSTRUCTURES 1000
void myfunction( void )
{
static int x = 0;
static struct mystructure *buf = NULL;
struct mystructure *m;

if ( !buf )
buf = (struct mystructure *) malloc( MAX_MYSTRUCTURES *
sizeof(struct mystructure) );

m = &buf[x++];

...
}

Amazingly the original code throws an exception when it reaches the
malloc line, while the band-aid fix code, which allocates MUCH MORE
memory, does not.
(the structure in question is pretty small-- 4 pointers, 4 ints and 3
chars)

If anyone could go above and beyond and offer me some insight even
though this isn't pure unblemished by-the-books C, I would be much
indebted to you.

Snis

Nov 15 '05 #1
8 2527
>First, let me announce that this is very possibly off-topic because
malloc is a specific third party accessory to c, etc. I spent about an
malloc() is an integral part of the C library and is very on-topic.
hour trying to find a more appropriate newsgroup and failed. If anyone
could point one out, I would be much obliged. I'm using MSVC .NET, but
the only .NET specific newsgroups I could find were vbasic newsgroups
and you'll agree a C malloc question is less off-topic here than there,
at least. I seem to be suffering the exact opposite of fragmentation: I have no
problem whatsoever allocation a large array of structures, but if I try
to allocate room for just 1 single structure, the program immediately
throws an exception. Debugging indicates the exception is indeed
thrown by malloc itself and not by some later line of code.
When malloc() throws an exception, it is usually because something
stomped the malloc() arena *SOMEWHERE* before that malloc() call.
And you really have no idea where the problem is.
I temporarily solved the problem as follows:
original code:

void myfunction( void )
{
struct mystructure *m;

m = (struct mystructure *) malloc( sizeof(struct mystructure) );

...
}

band-aid solution code:

#define MAX_MYSTRUCTURES 1000
void myfunction( void )
{
static int x = 0;
static struct mystructure *buf = NULL;
struct mystructure *m;

if ( !buf )
buf = (struct mystructure *) malloc( MAX_MYSTRUCTURES *
sizeof(struct mystructure) );

m = &buf[x++];

...
}

Amazingly the original code throws an exception when it reaches the
malloc line, while the band-aid fix code, which allocates MUCH MORE
memory, does not.
"reaches the malloc line" - you mean RUNS OUT OF MEMORY?
How do you know this, since your code doesn't check for malloc()
returning NULL?
(the structure in question is pretty small-- 4 pointers, 4 ints and 3
chars)
Show me the code that writes on the structure you allocate.
Are you sure you don't have subscripts that go out of range?
Strings that are too long?
If anyone could go above and beyond and offer me some insight even
though this isn't pure unblemished by-the-books C, I would be much
indebted to you.


Show us the code. ALL of it.

Gordon L. Burditt
Nov 15 '05 #2


Snis Pilbor wrote:
I seem to be suffering the exact opposite of fragmentation: I have no
problem whatsoever allocation a large array of structures, but if I try
to allocate room for just 1 single structure, the program immediately
throws an exception. Debugging indicates the exception is indeed
thrown by malloc itself and not by some later line of code.


Learn this mantra, "Minimal, complete, compilable program that
demonstrates the problem."
Only that way can we tell what's going on. There is little or no chance
the implementation is broken in such a fundamental way. So you are
doing something wrong. As you don't know what that is, you aren't
qualified (and I mean that in a non-mean way) to decide what code we
should look at.

Try again, striving for the goal above. You may find the problem on
your own when constructing that.

Brian

Nov 15 '05 #3
Snis Pilbor wrote:
First, let me announce that this is very possibly off-topic because
malloc is a specific third party accessory to c, etc.
malloc is not a 3rd party accessory, it is part of the standard C
library and so on topic.
I spent about an
hour trying to find a more appropriate newsgroup and failed. If anyone
could point one out, I would be much obliged. I'm using MSVC .NET, but
the only .NET specific newsgroups I could find were vbasic newsgroups
and you'll agree a C malloc question is less off-topic here than there,
at least.

I seem to be suffering the exact opposite of fragmentation: I have no
problem whatsoever allocation a large array of structures, but if I try
to allocate room for just 1 single structure, the program immediately
throws an exception. Debugging indicates the exception is indeed
thrown by malloc itself and not by some later line of code.
This normally means you have corrupted the the structures used to manage
the heap (on implementations with a heap). This is generally the result
of either running off the end of a buffer or freeing a pointer twice.
I temporarily solved the problem as follows:
original code:

void myfunction( void )
{
struct mystructure *m;

m = (struct mystructure *) malloc( sizeof(struct mystructure) );
You don't need the cast. If the compiler complains when you remove the
cast then either you have failed to include stdlib.h or you are
compiling as C++ instead of C. A simpler, less error prone option would be:

struct mystructure *m = malloc( sizeof *m );

However, this is not the cause of your problem in this case.
...
}

band-aid solution code:

#define MAX_MYSTRUCTURES 1000
void myfunction( void )
{
static int x = 0;
static struct mystructure *buf = NULL;
struct mystructure *m;

if ( !buf )
buf = (struct mystructure *) malloc( MAX_MYSTRUCTURES *
sizeof(struct mystructure) );

m = &buf[x++];

...
}

Amazingly the original code throws an exception when it reaches the
malloc line, while the band-aid fix code, which allocates MUCH MORE
memory, does not.
(the structure in question is pretty small-- 4 pointers, 4 ints and 3
chars)

If anyone could go above and beyond and offer me some insight even
though this isn't pure unblemished by-the-books C, I would be much
indebted to you.


Check the rest of your code for buffer overruns. That band aid will fail
the moment you have an important demo.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #4

Flash Gordon wrote:
Snis Pilbor wrote:

(snip)
I seem to be suffering the exact opposite of fragmentation: I have no
problem whatsoever allocation a large array of structures, but if I try
to allocate room for just 1 single structure, the program immediately
throws an exception. Debugging indicates the exception is indeed
thrown by malloc itself and not by some later line of code.


This normally means you have corrupted the the structures used to manage
the heap (on implementations with a heap). This is generally the result
of either running off the end of a buffer or freeing a pointer twice.

(snip)

Ahh, thank you so very kindly for this insight. Knowing this, I
shifted my attention to a different part of my code and believe I was
able to find the culprit.

Old bad code:

char *str_alloc( char *string )
{
char *x = (char *) malloc( strlen(string) * sizeof(char));
strcpy(x,string);
return x;
}

Fixed code:

char *str_alloc( char *string )
{
char *x = (char *) malloc( (strlen(string)+1) * sizeof(char) );
strcpy(x,string);
return x;
}

Wow, I have learned a lot from this. One of these days I am going to
have to teach myself the intricate details of how malloc works. The
naive expectation would be to assume the strcpy in the unfixed code
above would have immediately excepted. But that would be the clumsy,
slow, inefficient Java way to do it. Thanks again for the help,
everyone who chipped in. Oh, and sorry about not posting the entire
code in my OP-- the entire code is over 2000 lines so I didn't imagine
that would have been appropriate.

Snis

Nov 15 '05 #5
On 2005-06-28 21:18:09 -0400, "Snis Pilbor" <sn********@yahoo.com> said:

Flash Gordon wrote:
Snis Pilbor wrote: (snip)
I seem to be suffering the exact opposite of fragmentation: I have no
problem whatsoever allocation a large array of structures, but if I try
to allocate room for just 1 single structure, the program immediately
throws an exception. Debugging indicates the exception is indeed
thrown by malloc itself and not by some later line of code.


This normally means you have corrupted the the structures used to manage
the heap (on implementations with a heap). This is generally the result
of either running off the end of a buffer or freeing a pointer twice.

(snip)

Ahh, thank you so very kindly for this insight. Knowing this, I
shifted my attention to a different part of my code and believe I was
able to find the culprit.

Old bad code:

char *str_alloc( char *string )
{
char *x = (char *) malloc( strlen(string) * sizeof(char));
strcpy(x,string);
return x;
}

Fixed code:

char *str_alloc( char *string )
{
char *x = (char *) malloc( (strlen(string)+1) * sizeof(char) );
strcpy(x,string);
return x;
}

1. Cast is unnecessary, and could hide potential bugs
2. "* sizeof(char)" is redundant, sizeof(char) is always 1
3. No check for malloc failure

Re-fixed code:

char *str_alloc( char *string )
{
char *x = malloc(strlen(string)+1);

if(x)
{
strcpy(x,string);
}

return x;
}
Wow, I have learned a lot from this. One of these days I am going to have to teach myself the intricate details of how malloc works. The
naive expectation would be to assume the strcpy in the unfixed code
above would have immediately excepted. But that would be the clumsy,
slow, inefficient Java way to do it. Thanks again for the help,
everyone who chipped in. Oh, and sorry about not posting the entire
code in my OP-- the entire code is over 2000 lines so I didn't imagine
that would have been appropriate.

That's usually what happens :). When you try to cut your code down to
a post-able sample that demonstrates the problem, you end up finding
the problem yourself.

--
Clark S. Cox, III
cl*******@gmail.com

Nov 15 '05 #6
Snis Pilbor wrote:

First, let me announce that this is very possibly off-topic because
malloc is a specific third party accessory to c, etc. I spent about .... snip ...
I temporarily solved the problem as follows:
original code:

void myfunction( void )
{
struct mystructure *m;

m = (struct mystructure *) malloc( sizeof(struct mystructure) );
...
}


That code is casting the result from malloc, which it should not
do. The cast is very likely hiding the serious error of failing to
#include <stdlib.h>. malloc is very much on topic here, because
it, and its actions, are specified by the C standard.

The statement should read:

m = malloc(sizeof *m);

which should not have any problems if the type of m is properly
defined, and stdlib.h has been included.
--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Nov 15 '05 #7
Snis Pilbor wrote:
.... snip ...
Fixed code:

char *str_alloc( char *string )
{
char *x = (char *) malloc( (strlen(string)+1) * sizeof(char) );
strcpy(x,string);
return x;
}

Wow, I have learned a lot from this. One of these days I am going
to have to teach myself the intricate details of how malloc works.

.... snip ...

Not enough. You are still casting malloc, which is NEVER needed
and only serves to prevent the compiler diagnosing errors.
sizeof(char) is 1 by definition. Thus your malloc call should be:

char *x;

if (x = malloc(1 + strlen(string))) strcpy(x, string);

since it is fairly hard to copy into non-assigned storage. You
should always test the success of malloc (and realloc).

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

Nov 15 '05 #8
On Tue, 28 Jun 2005 21:33:54 -0400, Clark S. Cox III wrote:

....
Re-fixed code:

char *str_alloc( char *string )
Better still make that

char *str_alloc( const char *string )

because str_alloc() doesn't write to the original string.
{
char *x = malloc(strlen(string)+1);

if(x)
{
strcpy(x,string);
}

return x;
}
Wow, I have learned a lot from this. One of these days I am going to
have to teach myself the intricate details of how malloc works.
There's no need to learn ho malloc() works internally, that will vary from
implementation to implementation. You just need to know how to use it
properly.
The
naive expectation would be to assume the strcpy in the unfixed code
above would have immediately excepted. But that would be the clumsy,
slow, inefficient Java way to do it. Thanks again for the help,
everyone who chipped in. Oh, and sorry about not posting the entire
code in my OP-- the entire code is over 2000 lines so I didn't imagine
that would have been appropriate.


Yes, C doesn't inherently have bounds checking. This isn't a malloc()
rwlated probblem specifically, you could just as easily have written past
the end of a declared array.

Lawrence
Nov 15 '05 #9

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

Similar topics

4
by: Alan Little | last post by:
This is very bizarre. Could someone else have a look at this? Maybe you can see something I'm overlooking. Go here: http://www.newsletters.forbes.com/enews/admin/deliver.php4 U: bugtest P:...
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...
116
by: Kevin Torr | last post by:
http://www.yep-mm.com/res/soCrypt.c I have 2 malloc's in my program, and when I write the contents of them to the screen or to a file, there aren addition 4 characters. As far as I can tell,...
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;...
1
by: zoehart | last post by:
I'm working with VBScript to build a text email message. I'm seeing a variety of bizarre formatting issues. The following lines of code MT = MT & vbCrLf & "Card Type: " & CardType MT = MT &...
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
0
by: ckfan.painter | last post by:
I've run into a seemingly bizarre problem with insert() for std::vector. (This was done on Microsoft Visual C++ 2005 express version 8...maybe it is a compiler specific bug?) Here's the code: ...
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: 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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.