Re: malloc()/realloc() - have I got this right?

Discussion in 'C Programming' started by Flash Gordon, May 31, 2008.

  1. Flash Gordon

    Flash Gordon Guest

    rio wrote, On 31/05/08 06:51:
    > "CBFalconer" <> ha scritto nel messaggio > #include
    > <stdio.h>
    >> #include <stdlib.h>
    >> #include "ggets.h"
    >>
    >> #define INITSIZE 112 /* power of 2 minus 16, helps malloc */
    >> #define DELTASIZE (INITSIZE + 16)
    >>
    >> enum {OK = 0, NOMEM};

    >
    > #define OK 0
    > #define NOMEM 1
    > #define EOF 2


    Rather problematic seeing as stdlib.h already defines EOF

    > #define EOFOK 4
    >
    >> int fggets(char* *ln, FILE *f)
    >> {
    >> int cursize, ch, ix;
    >> char *buffer, *temp;
    >>
    >> *ln = NULL; /* default */

    >
    > if(ln==0||f==0) return ERROR;
    >
    > if there is an error: better segfault
    > but if there is a segfault: better return error


    The above two statements seem to directly contradict each other so I'm
    not sure what you are trying to say.

    <snip>
    --
    Flash Gordon
     
    Flash Gordon, May 31, 2008
    #1
    1. Advertising

  2. rio wrote:
    > "Flash Gordon" <> ha scritto nel messaggio
    > news:-gordon.me.uk...
    >> rio wrote, On 31/05/08 06:51:
    >>> "CBFalconer" <> ha scritto nel messaggio >
    >>> #include if there is an error: better segfault
    >>> but if there is a segfault: better return error

    >>
    >> The above two statements seem to directly contradict each other so
    >> I'm not sure what you are trying to say.

    >
    > if there is an data error in a function is better
    > segfault the function
    > but
    > is *much better* [than seg fault] the function return an error value

    A segvault might be better han continue processing with bogus data but
    better than a segvault is an assert(), as it would easily tell you in which
    file and line the error occured, rather than having you to run it thru a
    debugger to find some stack trace. Also0 a segvault might not give you what
    you need to debug, e.g. if the stack itself got corrupted.
    But still a segvault as well as a failed assertion is a bug in the program
    IMHO.

    Bye, Jojo
     
    Joachim Schmitz, May 31, 2008
    #2
    1. Advertising

  3. Flash Gordon

    santosh Guest

    Joachim Schmitz wrote:

    <snip>

    > A segvault might be better han continue processing with bogus data but
    > better than a segvault is an assert(), as it would easily tell you in
    > which file and line the error occured, rather than having you to run
    > it thru a debugger to find some stack trace.


    Also behaviour on an assertion failure is defined by the standard while
    a segfault is not guaranteed to occur on all platforms.

    <snip>
     
    santosh, May 31, 2008
    #3
  4. "rio" <> writes:
    > "Flash Gordon" <> ha scritto nel messaggio
    > news:-gordon.me.uk...
    >> rio wrote, On 31/05/08 06:51:
    >>> "CBFalconer" <> ha scritto nel messaggio > #include
    >>> if there is an error: better segfault
    >>> but if there is a segfault: better return error

    >>
    >> The above two statements seem to directly contradict each other so I'm not
    >> sure what you are trying to say.

    >
    > if there is an data error in a function is better
    > segfault the function
    > but
    > is *much better* [than seg fault] the function return an error value


    "rio", are you the same person who previously posted as
    "RoSsIaCrIiLoIA" and as "av <>"?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 31, 2008
    #4
  5. santosh wrote:
    > Joachim Schmitz wrote:
    >
    > <snip>
    >
    >> A segvault might be better han continue processing with bogus data
    >> but better than a segvault is an assert(), as it would easily tell
    >> you in which file and line the error occured, rather than having you
    >> to run it thru a debugger to find some stack trace.

    >
    > Also behaviour on an assertion failure is defined by the standard
    > while a segfault is not guaranteed to occur on all platforms.

    And that is indeed another why I regard an assert() better than a segfault.
    I just forgot to mention.

    Bye, Jojo
     
    Joachim Schmitz, Jun 1, 2008
    #5
    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. Bren
    Replies:
    8
    Views:
    2,042
    Stephen Howe
    Sep 4, 2003
  2. DrBob
    Replies:
    2
    Views:
    589
    Unforgiven
    Nov 26, 2003
  3. Jun Woong

    Re: Override malloc,calloc,realloc and free?

    Jun Woong, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,093
    Jun Woong
    Jun 26, 2003
  4. Jens Thoms Toerring

    Re: malloc()/realloc() - have I got this right?

    Jens Thoms Toerring, May 28, 2008, in forum: C Programming
    Replies:
    0
    Views:
    370
    Jens Thoms Toerring
    May 28, 2008
  5. Keith Thompson

    Re: malloc()/realloc() - have I got this right?

    Keith Thompson, May 30, 2008, in forum: C Programming
    Replies:
    17
    Views:
    511
    Keith Thompson
    May 31, 2008
Loading...

Share This Page