malloc call not returning

Discussion in 'C Programming' started by Norbert Leister, May 2, 2007.

  1. 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
     
    Norbert Leister, May 2, 2007
    #1
    1. Advertising

  2. Norbert Leister

    Ian Collins Guest

    Norbert Leister wrote:
    >
    > 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.

    --
    Ian Collins.
     
    Ian Collins, May 2, 2007
    #2
    1. Advertising

  3. Ian Collins wrote:
    > 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
     
    Norbert Leister, May 2, 2007
    #3
  4. In article <f19hcs$6pb$-chemnitz.de>,
    Norbert Leister <> wrote:

    >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
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, May 2, 2007
    #4
  5. 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

    CBFalconer wrote:
    > Two fundamental errors. First, don't cast the return value from
    > malloc. Doing so may have hidden compiler error reports, such as
    > caused by failure to #include <stdlib.h>. Second, test the
    > returned result - if it is NULL the malloc failed.
    >
     
    Norbert Leister, May 2, 2007
    #5
  6. Norbert Leister

    Chris Dollin Guest

    Norbert Leister wrote:

    > 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
     
    Chris Dollin, May 2, 2007
    #6
  7. hi,

    Chris Dollin wrote:
    > [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
     
    Norbert Leister, May 2, 2007
    #7
  8. In article <f1a77h$vn6$-chemnitz.de>,
    Norbert Leister <> wrote:

    >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
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, May 2, 2007
    #8
  9. Norbert Leister

    Chris Dollin Guest

    Norbert Leister wrote:

    > hi,
    >
    > Chris Dollin wrote:
    > > [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++.

    --
    "It's just the beginning we've seen" - Colosseum, /Tomorrow's Blues/

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
     
    Chris Dollin, May 2, 2007
    #9
  10. Norbert Leister

    JT Guest

    On May 2, 2:29 pm, Norbert Leister <> wrote:
    > 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
     
    JT, May 2, 2007
    #10
  11. 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
     
    Norbert Leister, May 2, 2007
    #11
  12. 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?
     
    Martin Ambuhl, May 2, 2007
    #12
  13. 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) <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"
     
    Keith Thompson, May 2, 2007
    #13
  14. Norbert Leister

    Flash Gordon Guest

    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
     
    Flash Gordon, May 2, 2007
    #14
  15. Norbert Leister

    Default User Guest

    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
     
    Default User, May 2, 2007
    #15
  16. Norbert Leister

    Ian Collins Guest

    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.
     
    Ian Collins, May 2, 2007
    #16
  17. Ian Collins <> 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) <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"
     
    Keith Thompson, May 3, 2007
    #17
  18. On May 2, 10:37 am, Norbert Leister <> 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.
     
    J. J. Farrell, May 3, 2007
    #18
  19. Norbert Leister

    Ian Collins Guest

    J. J. Farrell wrote:
    > On May 2, 10:37 am, Norbert Leister <> 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.
     
    Ian Collins, May 3, 2007
    #19
  20. 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.
     
    Richard Heathfield, May 3, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    719
  2. ravi
    Replies:
    0
    Views:
    468
  3. Peter
    Replies:
    34
    Views:
    2,016
    Richard Tobin
    Oct 22, 2004
  4. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    334
    Captain Winston
    Mar 30, 2006
  5. kj

    to malloc or not to malloc?

    kj, Jun 4, 2009, in forum: C Programming
    Replies:
    5
    Views:
    314
Loading...

Share This Page