malloc call not returning

  • Thread starter Norbert Leister
  • Start date
N

Norbert Leister

Hi NG,

I've the problem, that a malloc call is not returning.

<source-snip>

printf("a\n");
my_pointer = (struct_pointer)malloc(struct_size); /*size 1420*/
printf("b\n");
</source-snip>

I'm almost sure, that my program frees some memory, but if there is no
memory, the malloc should return some NULL-pointer.

The 'normal' output of the function is 'a' and 'b'. But sometimes (after
a while of running), the program stops after printing the 'a'. The other
threads of the programm are still responding to commands, but the one
who called the malloc does not reacts.

Have you somewhere had a similar problem?
Compiler: gcc-3.3.3-7 on Fedora Core 2


Greeting:


Norbert
 
I

Ian Collins

Norbert said:
Hi NG,

I've the problem, that a malloc call is not returning.

<source-snip>

printf("a\n");
my_pointer = (struct_pointer)malloc(struct_size); /*size 1420*/
printf("b\n");
</source-snip>

I'm almost sure, that my program frees some memory, but if there is no
memory, the malloc should return some NULL-pointer.

The 'normal' output of the function is 'a' and 'b'. But sometimes (after
a while of running), the program stops after printing the 'a'. The other
threads of the programm are still responding to commands, but the one
who called the malloc does not reacts.

Have you somewhere had a similar problem?
Compiler: gcc-3.3.3-7 on Fedora Core 2
Attach your debugger and see what the application is doing.
 
N

Norbert Leister

Ian said:
Attach your debugger and see what the application is doing.

it is some remotely executed code - that's why I'm using the printf to
see where the code is stopping

Greetings:


Norbert
 
R

Richard Tobin

Norbert Leister said:
I've the problem, that a malloc call is not returning.

If malloc() is really not returning, the most likely problem is that
you've got a malloc()-related error somewhere else in the program that
is messing up malloc()'s data structures.

Run your program under some kind of memory-checking tool such as
valgrind.

-- Richard
 
N

Norbert Leister

I did not knew, that I shoud'nt cast the pointer - because malloc is
returning some (void*) while my pointer is of some other type ... (and I
don't like implicite casts).

The 'if(NULL == my_pointer) ...' is straight after the output :) I did
not wanted to bore the NG with uninteresting code.

Greetings:


Makarius
 
C

Chris Dollin

Norbert said:
I did not knew, that I shoud'nt cast the pointer - because malloc is
returning some (void*) while my pointer is of some other type ... (and I
don't like implicite casts).

There are no implicit casts.

That's because casts are the name for the expressions `(Type) Expr`.

/Conversions/ can be implicit.

Implicit conversions can be very useful, reducing the verbosity of your
code and making it differently robusted against changes in the types of
its component variables.

For pointer types, it's important to note that a cast has much the same
effect as "shut up, I know what I'm doing", which is all very well
except when you make a mistake: classically, when you fail to declare
`malloc`, the compiler assumes it returns `int`, your cast to `Spoo*`
is silently accepted, and the assignment of the complete garbage value
to its pointer destination goes undetected until the Most Embarrasing
Moment.

[Yes, a friendly compiler will tell you that you've forgotten to
declare `malloc`, that you've cast an integer to a pointer, did
you mean that, sir? etc; but friendliness isn't required by the
Standard, which is quite happy if your implementation is an axe
weilding psychopath in its spare time, viz, whenever there's un
defined behaviour to fall into.]

--
"You're not supposed to /think/ about it, /The Beiderbeck Connection/
you're supposed to say NO!" Jill Swinburn

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
N

Norbert Leister

hi,

Chris said:
> [Yes, a friendly compiler will tell you that you've forgotten to
> declare `malloc`, that you've cast an integer to a pointer, did
> you mean that, sir? etc; but friendliness isn't required by the
> Standard, which is quite happy if your implementation is an axe
> weilding psychopath in its spare time, viz, whenever there's un
> defined behaviour to fall into.]
>



when I write

my_pointer = malloc(struct_size);

i get an error compiling the code with g++. I sometimes use compiling my
c-code in g++ go get some extendend compiler warnings. So i use a
typecast for all mallocs I'm using...

Greeting:


Norbert
 
R

Richard Tobin

Norbert Leister said:
when I write

my_pointer = malloc(struct_size);

i get an error compiling the code with g++. I sometimes use compiling my
c-code in g++ go get some extendend compiler warnings. So i use a
typecast for all mallocs I'm using...

So you're using a C++ compiler to get more warnings about C code
despite the fact that some of them are wrong and stop your program
compiling? This seems a dubious trade-off to me.

-- Richard
 
C

Chris Dollin

Norbert said:
hi,

Chris said:
[Yes, a friendly compiler will tell you that you've forgotten to
declare `malloc`, that you've cast an integer to a pointer, did
you mean that, sir? etc; but friendliness isn't required by the
Standard, which is quite happy if your implementation is an axe
weilding psychopath in its spare time, viz, whenever there's un
defined behaviour to fall into.]
when I write

my_pointer = malloc(struct_size);

i get an error compiling the code with g++.

g++ isn't a C compiler.
I sometimes use compiling my
c-code in g++ go get some extendend compiler warnings.

Turn up the warnings for gcc, rather than use g++.
 
J

JT

when I write

my_pointer = malloc(struct_size);

i get an error compiling the code with g++. I sometimes use compiling my
c-code in g++ go get some extendend compiler warnings. So i use a
typecast for all mallocs I'm using...

Dear Norbert:

Many posters have commented that it is bad idea
to compile C code using a C++ compiler.

Here is a concrete reason why: there are many serious
differences in semantics, and your program may
execute incorrectly.

See this list of differences between ISO C and ISO C++:
http://david.tribble.com/text/cdiffs.htm

- JT
 
N

Norbert Leister

Hi,

> Dear Norbert:
>
> Many posters have commented that it is bad idea
> to compile C code using a C++ compiler.

Sorry gusy - i think my answer was (again) too short

I'm using gcc for normal compiling and running of my project (.h and .c
files).

But sometimes (only for the creation of warnings) I use the g++ because
it checks some missing type-casts and warns me if I switch the usage of
booleans and enums (I've got some extended return values). The gcc does
NOT recognises a
return FALSE;
when the function should return some enum. (gcc and g++ called with the
same params)

But I'm running only the gcc-compiled stuff.

Greetings:


Norbert
 
M

Martin Ambuhl

Norbert said:
when I write

my_pointer = malloc(struct_size);

i get an error compiling the code with g++.

g++ is a C++ compiler. If you have problems with that different
I sometimes use compiling my
c-code in g++ go get some extendend compiler warnings.

No, you get C++ warnings. If you are compiling C, just turn on the
warnings you want with gcc. The choices are large, and the
documentation is explicit. It is a common error, but an error
nonetheless, to think that warnings about C++ errors are useful for C
programs. In fact, using a C++ compiler to compile C programs is just
silly as well as wrong. C++ compilers and C compilers handle different
languages. You could use a Fortran compiler and get "extended compiler
warnings," but would you?
So i use a
typecast for all mallocs I'm using...

That's silly. Why don't you use the silly C++ casts instead, since a
C++ compiler might well "warn" you that C-style casts are a bad idea?
 
K

Keith Thompson

Norbert Leister said:
I'm using gcc for normal compiling and running of my project (.h and
.c files).

But sometimes (only for the creation of warnings) I use the g++
because it checks some missing type-casts and warns me if I switch the
usage of booleans and enums (I've got some extended return
values). The gcc does NOT recognises a
return FALSE;
when the function should return some enum. (gcc and g++ called with
the same params)

But I'm running only the gcc-compiled stuff.

The "missing type-casts" (BTW, the term is "casts", not "type-casts")
are probably ones that are required in C++ but not in C. g++ is
telling you things that are not relevant to C. (It will also print an
error message if you use "class" as an identifier, which is perfectly
legal in C.)

Using a C++ compiler to find additional errors in C code can make
sense *if* you realize which warnings are relevant and which are not.
You don't need to modify your code to eliminate *all* C++ diagnostics.
 
F

Flash Gordon

Norbert Leister wrote, On 02/05/07 18:37:
Hi,



Sorry gusy - i think my answer was (again) too short

I'm using gcc for normal compiling and running of my project (.h and .c
files).

But sometimes (only for the creation of warnings) I use the g++ because
it checks some missing type-casts and warns me if I switch the usage of
booleans and enums (I've got some extended return values). The gcc does
NOT recognises a
return FALSE;
when the function should return some enum. (gcc and g++ called with the
same params)

But I'm running only the gcc-compiled stuff.

You are still using the wrong tool for the job. If you want you C code
analysed use a tool designed to analyse C code, not a tool designed to
compile C++ code. They are different problems. Had you read the
comp.lang.c FAQ at http://c-faq.com you would have found references to
Lint, one version of which is available from http://www.splint.org/
 
D

Default User

Norbert said:
Hi,



Sorry gusy - i think my answer was (again) too short

I'm using gcc for normal compiling and running of my project (.h and
.c files).

But sometimes (only for the creation of warnings) I use the g++
because it checks some missing type-casts

Can you give some examples of these "missing" casts?




Brian
 
I

Ian Collins

Norbert said:
Hi,



Sorry gusy - i think my answer was (again) too short

I'm using gcc for normal compiling and running of my project (.h and .c
files).

But sometimes (only for the creation of warnings) I use the g++ because
it checks some missing type-casts and warns me if I switch the usage of
booleans and enums (I've got some extended return values). The gcc does
NOT recognises a
return FALSE;
when the function should return some enum. (gcc and g++ called with the
same params)
Consider using bool if you are only building with gcc.
 
K

Keith Thompson

Ian Collins said:
Consider using bool if you are only building with gcc.

To be clear, gcc (in C mode) doesn't support a C++-style "bool"
keyword; it does support the _Bool keyword and the <stdbool.h> header,
as defined by C99. (Note that <stdbool.h> is provided as part of gcc
itself, unlike most headers which are part of a distinct runtime
library.)
 
J

J. J. Farrell

Sorry gusy - i think my answer was (again) too short

I'm using gcc for normal compiling and running of my project (.h and .c
files).

But sometimes (only for the creation of warnings) I use the g++ because
it checks some missing type-casts and warns me if I switch the usage of
booleans and enums (I've got some extended return values). The gcc does
NOT recognises a
return FALSE;
when the function should return some enum. (gcc and g++ called with the
same params)

But I'm running only the gcc-compiled stuff.

Although this might strictly be off-topic, what params are you giving
to gcc? I'll be amazed if running your code through a C++ compiler
will give you any warnings relevant to C which you can't get by giving
appropriate parameters to gcc. Since it may also give you lots of
incorrect warnings and errors due to the differences between C and C+
+, it's a very strange way to work.
 
I

Ian Collins

J. J. Farrell said:
Although this might strictly be off-topic, what params are you giving
to gcc? I'll be amazed if running your code through a C++ compiler
will give you any warnings relevant to C which you can't get by giving
appropriate parameters to gcc. Since it may also give you lots of
incorrect warnings and errors due to the differences between C and C+
+, it's a very strange way to work.
It looks like he is using something like

typedef enum {FALSE,TRUE} Bool;
typedef enum {GOOD,BAD,UGLY} Result;

Bool f() { return UGLY; }

which gcc -Wall -ansi -pedantic is quite happy with.
 
R

Richard Heathfield

Flash Gordon said:
Norbert Leister wrote, On 02/05/07 18:37:

You are still using the wrong tool for the job. If you want you C code
analysed use a tool designed to analyse C code, not a tool designed to
compile C++ code.

No, Flash, he's right - if he wants more warnings, using a compiler for
a different language is a great way to get them. Personally, for this
purpose I can wholeheartedly recommend using a COBOL compiler.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top