calloc crashing -- dump core

S

Sheth Raxit

Hi

I am debugging multithreaded/netowrk prog code, It dumps core


hostname% gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-
solaris2.8/2.95.3/
specs
gcc version 2.95.3 20010315 (release)


icprg04% uname -a
SunOS <host> 5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R


====cut paste from dbx session this is ***dbx ./binary-name ./core
***
====
(dbx) thread t@65
Current function is get_remote_thick_profile
392 if (((*list) = (char *) calloc (1, sizeof (char))) ==
(char *)NULL)
t@65 (l@65) stopped in realfree at 0xff0d4834
0xff0d4834: realfree+0x00ec: st %l0, [%i0 + %o1]
(dbx) where
current thread: t@65
[1] realfree(0xf0628, 0x400, 0x93b28, 0x494e3240, 0x0, 0xf0a38), at
0xff0d4834
[2] cleanfree(0x0, 0x10, 0x93278, 0xff139980, 0xff168284,
0xff16fad4), at 0xff0d505c
[3] _malloc_unlocked(0x8, 0x18, 0xec398, 0xec3a0, 0xffffffff,
0xfefa1000), at 0xff0d41b4
[4] malloc(0x1, 0x1, 0x94224, 0xff16fa90, 0xff168284, 0xff1709b0),
at 0xff0d40a4
[5] calloc(0x1, 0x1, 0x1, 0xff139980, 0xfefa1000, 0x1000), at
0xff0c0e68
Other Function call


Interestingly Calloc is called as below ! and it is crashing.;
392 if (((*list) = (char *) calloc (1, sizeof (char))) ==
(char *)NULL)


What i guess is...


1. Definetly this calloc is not source of bug, but there MAY be
memory
corruption elsewhere.
2. Is this Bug ? ideally calloc should not crash :) like this way.
and
debugger should give me the source of real-error


--Raxit
 
J

jacob navia

Sheth said:
Hi

I am debugging multithreaded/netowrk prog code, It dumps core


hostname% gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-
solaris2.8/2.95.3/
specs
gcc version 2.95.3 20010315 (release)


icprg04% uname -a
SunOS <host> 5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R


====cut paste from dbx session this is ***dbx ./binary-name ./core
***
====
(dbx) thread t@65
Current function is get_remote_thick_profile
392 if (((*list) = (char *) calloc (1, sizeof (char))) ==
(char *)NULL)

Look again at that. And if you do not find the bug you should write 100
times:

"I should look carefully at the code"

Good handwriting required!

P.S. calloc (1,sizeof(char)) allocates ONE character. Then it is cast
into a char POINTER, that is probably bigger than 1 character!!!

FIX:

if (((*list) = (char *) calloc (1, sizeof (char *))) ==(char *)NULL)
^

Note the char * instead of just char?
 
L

Laurent Deniau

Look again at that. And if you do not find the bug you should write 100
times:

"I should look carefully at the code"

Good handwriting required!

P.S. calloc (1,sizeof(char)) allocates ONE character. Then it is cast
into a char POINTER,

which is right...

T* p = (T*) calloc(1, sizeof *T);

I suspect the problem to be elsewhere.

a+, ld.
 
J

jacob navia

Laurent said:
which is right...

T* p = (T*) calloc(1, sizeof *T);

I suspect the problem to be elsewhere.

a+, ld.

There is no *T look again. There is only sizeof(char).
 
S

Sheth Raxit

Look again at that. And if you do not find the bug you should write 100
times:

"I should look carefully at the code"
I think i need to... :) , using for loop and printf. :) :)
Good handwriting required!

P.S. calloc (1,sizeof(char)) allocates ONE character. Then it is cast
into a char POINTER, that is probably bigger than 1 character!!!
I am trying to allocating space for one character, and later i am
reallocating the same space.
FIX:

if (((*list) = (char *) calloc (1, sizeof (char *))) ==(char *)NULL)
^

Note the char * instead of just char?
why sizeof(char *) ? return value is char* <which means pointing to
character>, which is still correct.

even we assume you are correct, why it is crashing. ?
 
S

Sheth Raxit

There is no *T look again. There is only sizeof(char).
actually list is passed as char **list in the function.

int f1(char **list)
{
...
...

if (((*list) = (char *) calloc (1, sizeof (char))) ==(char *)NULL)
...
...
}
 
R

Richard Heathfield

Sheth Raxit said:

Interestingly Calloc is called as below ! and it is crashing.;
392 if (((*list) = (char *) calloc (1, sizeof (char))) ==
(char *)NULL)

You might find this easier to read if you write it as:

*list = calloc(1, 1);
if(*list == NULL)
{

Incidentally, does this re-write suggest anything to you about the amount
of storage you're allocating?
What i guess is...

Guessing isn't going to help you. What is the value of list before this
allocation? Does it point to an existing char * object? If not,
dereferencing it (as you do here) is asking for trouble.

1. Definetly this calloc is not source of bug, but there MAY be
memory
corruption elsewhere.
2. Is this Bug ? ideally calloc should not crash :) like this way.
and
debugger should give me the source of real-error

Debuggers are not mind-readers. They're more like torches (flashlights).
They can shine a light beam for you, but it is up to you to point it in
the direction you want to see.
 
V

vippstar

actually list is passed as char **list in the function.

int f1(char **list)
{
...
...

if (((*list) = (char *) calloc (1, sizeof (char))) ==(char *)NULL)
...
...

}

Please post a minimal test case that demonstrates your problem.

That function could be valid if used like this:
 
L

Laurent Deniau

oups:

T* p = (T*) calloc(1, sizeof *p);
There is no *T look again. There is only sizeof(char).

s/\*T/\*p/

still, if T is char, *p is of type char, then sizeof(char) is ok.

a+, ld.
 
L

Laurent Deniau

<snip>>T* p = (T*) calloc(1, sizeof *T);

Drop the cast to not silence a possible compiler warning about a possibly
missing prototype

I never put a cast in front of calloc/malloc/realloc. It is there
because the PO did it (C/C++ cross-compilation?) and I wanted to show
the idiom so I have reused to closest valid form.

a+, ld.
 
J

Joachim Schmitz

Joachim Schmitz said:
Drop the cast to not silence a possible compiler warning about a possibly
missing prototype
Also make it sizeof *p:
T* p = calloc(1, sizeof *p);
 
J

Joachim Schmitz

jacob navia said:
There is no *T look again. There is only sizeof(char).
Look again, apparently list is a pointer to a pointer to char, so
sizeof(char) is correct

Bye, Jojo
 
J

Joachim Schmitz

jacob navia said:
Look again at that. And if you do not find the bug you should write 100
times:

"I should look carefully at the code"

Good handwriting required!

P.S. calloc (1,sizeof(char)) allocates ONE character. Then it is cast
into a char POINTER, that is probably bigger than 1 character!!!

FIX:

if (((*list) = (char *) calloc (1, sizeof (char *))) ==(char *)NULL)
Not if list points to a pointer to char, which seems to be the case here.

So better use the variable name rather than it's type and also loose the
casts
if (((*list) = calloc (1, sizeof **list)) == NULL)

Here sizeof **list == sizeof char, but it has the extra benefit that you
don't need to change this code if you change the type of list

Bye, Jojo
 
C

CBFalconer

Laurent said:
.... snip ...

oups:

T* p = (T*) calloc(1, sizeof *p);
^^^^
Eliminate the cast, which does nothing except suppress error
messages. calloc returns a void*, which needs to be tested for
non-NULL.

And why calloc, anyhow. Whats wrong with malloc?
 
J

Joachim Schmitz

CBFalconer said:
^^^^
Eliminate the cast, which does nothing except suppress error
messages. calloc returns a void*, which needs to be tested for
non-NULL.

And why calloc, anyhow. Whats wrong with malloc?
You're barking up the wrong tree, ask the OP instead. Presumably the OP
wanted the allocated memory to be initialized to 0.

Bye, Jojo
 

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

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top