Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc call not returning

Reply
Thread Tools

malloc call not returning

 
 
Norbert Leister
Guest
Posts: n/a
 
      05-02-2007
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
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-02-2007
Norbert Leister wrote:

> 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
[naughty words deleted] language, ask them in <news:comp.lang.c++>. If
your problem is specifically with g++, ask in a gnu-related newsgroup.

> 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?
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-02-2007
Norbert Leister <> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-02-2007
Norbert Leister wrote, On 02/05/07 18:37:
> 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.


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/
--
Flash Gordon
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-02-2007
Norbert Leister wrote:

> 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


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




Brian
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-02-2007
Norbert Leister wrote:
> 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)
>

Consider using bool if you are only building with gcc.

--
Ian Collins.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-03-2007
Ian Collins <ian-> writes:
[...]
>> 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.


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.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      05-03-2007
On May 2, 10:37 am, Norbert Leister <makar...@web.de> wrote:
> >
> > 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.


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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-03-2007
J. J. Farrell wrote:
> On May 2, 10:37 am, Norbert Leister <makar...@web.de> wrote:
>> >
>> > 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.

>
> 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.

--
Ian Collins.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-03-2007
Flash Gordon said:

> Norbert Leister wrote, On 02/05/07 18:37:
>>
>> 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).


<snip>

> 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc? kj C Programming 5 06-05-2009 02:22 PM
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57