"large" malloc and EOVERFLOW errno!!!

M

muister

Hi, I am getting an error (errno EOVERFLOW) from a rather small
malloc:

htbl->table = (List *)malloc(15 * sizeof(List))

where sizeof(List) = 20. htbl->table is a (List *). If I lowered the
'15' to '11', everything works. I am running in a cygwin environment
(with over 200 MB ram free).

Your help is greatly appreciated.
 
D

Darrell Grainger

Hi, I am getting an error (errno EOVERFLOW) from a rather small
malloc:

htbl->table = (List *)malloc(15 * sizeof(List))

where sizeof(List) = 20. htbl->table is a (List *). If I lowered the
'15' to '11', everything works. I am running in a cygwin environment
(with over 200 MB ram free).

Your help is greatly appreciated.

The only thing I can see wrong is casting the result of malloc. If you do
not have #include <stdlib.h>, the cast could be hiding this.

1) make sure you have the appropriate #include statement.
2) change this line to:

htbl->table = malloc(15 * sizeof(List))

the only other possibility is that you have other memory allocations and
this is just putting things over the edge. Note that having 200 MB of RAM
does not mean all of it will be available for malloc to use. There could
be a limit on the memory malloc has access to.
 
R

Richard Bos

Hi, I am getting an error (errno EOVERFLOW) from a rather small
malloc:

htbl->table = (List *)malloc(15 * sizeof(List))

where sizeof(List) = 20. htbl->table is a (List *). If I lowered the
'15' to '11', everything works. I am running in a cygwin environment
(with over 200 MB ram free).

First thought: you cast malloc(). This is never necessary in C. Get rid
of the cast, make sure you are not accidentally compiling this as C++
rather than C, and do not forget to provide a proper declaration for
malloc(), by #including <stdlib.h>.

Second thought: are you sure it's malloc() which sets errno? If a
previous statement caused an EOVERFLOW, malloc() is not allowed to reset
it to 0 just because it itself had no error.

Third thought: that's a very odd error for malloc(). If it were out of
memory, it should simply return a null pointer. Are you sure you haven't
munged the allocation tables before this statement (e.g., by writing in
memory you should not write in, or by free()ing a pointer you've already
free()d, or which you didn't get from malloc() in the first place)? That
could cause malloc() to get confused and misbehave.

Richard
 
J

Jens.Toerring

In comp.lang.c muister said:
Hi, I am getting an error (errno EOVERFLOW) from a rather small
malloc:
htbl->table = (List *)malloc(15 * sizeof(List))
where sizeof(List) = 20. htbl->table is a (List *). If I lowered the
'15' to '11', everything works. I am running in a cygwin environment
(with over 200 MB ram free).

First question: why do you check errno? Did malloc() return NULL?
(And you don't need to cast the return value of malloc(), if you
put it in because the compiler was complaining about an assignment
between incompatible types, i.e. from an int to a pointer, than
you forgot to include <stdlib.h>.)

Even if it fails malloc() not necessarily must set errno on failure,
and when it does it sets it to ENOMEM, at least on POSIX systems
(and I guess cygwin falls under that category). Checking errno
doesn't give you much new information since you already know that
since malloc() did return NULL. So one guess is that the EOVERFLOW
is something that errno was set to already before the malloc() call
(which, if it did succeed, won't touch errno).

EOVERFLOW typically indicates that the value is too large for the
data type you try to assign it to. So a check if htbl->table is
really the pointer you assume it to be might be in order.

Regards, Jens
 
H

Hans-Bernhard Broeker

[Missing F'up2 reduction fixed.]

In comp.lang.c.moderated muister said:
Hi, I am getting an error (errno EOVERFLOW) from a rather small
malloc:
htbl->table = (List *)malloc(15 * sizeof(List))

No you're not: malloc doesn't set errno to EOVERFLOW to signal an
error. You're not using the right method to check for errors from
malloc().

Neither is 15*20 = 300 a 'large' malloc by any modern standards.

And, on a side note, you should never cast the return value of malloc().
 
R

Raj

Hi,

I don't think malloc is causing this behavior. malloc basically either
returns a valid pointer to the newly allocated free space or returns
NULL if it can't allocate the requested amount of space. It never
under-allocates (it sure does over-allocate though, for house-keeping
purposes)!

EOVERFLOW primarily occurs when you are trying to mix incorrect data
types or probably mixing 32 and 64 bit code together...

Let me know, if that's not the case...

-Raj
 
J

Jens.Toerring

In comp.lang.c muister said:
Hi, I am getting an error (errno EOVERFLOW) from a rather small
malloc:
htbl->table = (List *)malloc(15 * sizeof(List))

where sizeof(List) = 20. htbl->table is a (List *). If I lowered the
'15' to '11', everything works. I am running in a cygwin environment
(with over 200 MB ram free).

First of all, malloc() isn't required to set errno as far as I can
see from C standard (and EOVERFLOW isn't even mentioned). On some
systems (POSIX comes to mind and it's probably save to assume that
cygwin falls under that category) errno gets set to ENOMEM, but not
to EOVERFLOW (that would indicate that you tried to assign a value
to a type that is not large enough to store it). It's also im-
portant to know that no function is allowed to set errno to zero,
so typically it only gets changed in case of failure.

Without more information I can only gues that one of the following
three alternatives leads to your problem:

- malloc() returned a non-NULL value but you don't check that and
only look at errno (but which in case of success doesn't tell
you anything because errno is still set to the value it had be-
fore the call)

- on your implementation malloc() does not set errno on failure
(since that doesn't convey additional information anyway)

- htbl->table isn't really a List pointer but something not wide
enough to store the return value of malloc() (which might ex-
plain the EOVERFLOW value of errno).

Regards, Jens
 
W

William Ahern

muister said:
Hi, I am getting an error (errno EOVERFLOW) from a rather small
malloc:
htbl->table = (List *)malloc(15 * sizeof(List))
where sizeof(List) = 20. htbl->table is a (List *). If I lowered the
'15' to '11', everything works. I am running in a cygwin environment
(with over 200 MB ram free).

Is malloc() actually return NULL, or are you checking errno regardless of
what malloc() returns?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top