Re: Return of a string that fails

Discussion in 'C Programming' started by vippstar@gmail.com, May 20, 2008.

  1. Guest

    On May 20, 1:52 pm, nembo kid <nembo@kid> wrote:
    > My homework is to make a simple function that, given a unsigned char,
    > should returns its bit string representation.

    HW questions, hmmmmm...

    > I tried to drop down some code (as follows) but it doesn't work.
    > Compiling is successfully, but print8bit() doesn't return anything.
    >
    > Was wondering where I'm mistaking. Thanks in advance for your help.

    You return a pointer to an object whose lifetime is over. That's
    undefined behavior.
    <snip code>
     
    , May 20, 2008
    #1
    1. Advertising

  2. Guest

    On May 20, 2:12 pm, nembo kid <nembo@kid> wrote:
    > ha scritto:
    >
    > > You return a pointer to an object whose lifetime is over. That's
    > > undefined behavior.

    >
    > Ok, found..,.that silly I am.
    >
    > Because 's[]' is allocated on the stack. But if I'd allocate s[] on the
    > heap I shouldn't have any issue....seems that the same happens with
    > linked list (that are allocated on the heap).
    >
    > So either I'd use the qualificator 'static' to "keep on alive" s after
    > exiting from the function or pass its address as parameter (a pointer to
    > a pointer).
    >
    > Both solutions are equivalent?

    No, the second solution, assuming void f(unsigned char c, char **s)
    will not work unless you assign *s a static array or a value from
    malloc/calloc/realloc.
    Here's the best solution:

    void f(unsigned char c, char *s)

    And write to s. You may assume that 's' is at least CHAR_BIT+1 long,
    and the user of the function is responsible to make sure it is.
     
    , May 20, 2008
    #2
    1. Advertising

  3. Bart Guest

    On May 20, 12:12 pm, nembo kid <nembo@kid> wrote:
    > ha scritto:
    >
    > > You return a pointer to an object whose lifetime is over. That's
    > > undefined behavior.

    >
    > Ok, found..,.that silly I am.
    >
    > Because 's[]' is allocated on the stack. But if I'd allocate s[] on the
    > heap I shouldn't have any issue....seems that the same happens with
    > linked list (that are allocated on the heap).
    >
    > So either I'd use the qualificator 'static' to "keep on alive" s after
    > exiting from the function or pass its address as parameter (a pointer to
    > a pointer).
    >
    > Both solutions are equivalent?


    Using 'static' is an instant fix.

    But has the disadvantage that, if the caller doesn't use or copy the
    value in s straightaway, the function can be called elsewhere and s
    can be overwritten.


    -- Bartc
     
    Bart, May 20, 2008
    #3
  4. Richard Bos Guest

    nembo kid <nembo@kid> wrote:

    > ha scritto:
    >
    > > You return a pointer to an object whose lifetime is over. That's
    > > undefined behavior.

    >
    > Ok, found..,.that silly I am.
    >
    > Because 's[]' is allocated on the stack. But if I'd allocate s[] on the
    > heap I shouldn't have any issue....seems that the same happens with
    > linked list (that are allocated on the heap).


    "Stack" and "heap" are deceptive terms. What matters is the duration of
    the object you return, not where it is placed. You might want to read
    questions 7.5a and 7.5b from the FAQ:
    <http://c-faq.com/malloc/retaggr.html> and
    <http://c-faq.com/malloc/retaggr2.html>.

    Richard
     
    Richard Bos, May 20, 2008
    #4
  5. wrote:
    >
    > On May 20, 1:52 pm, nembo kid <nembo@kid> wrote:
    > > My homework is to make a simple function that, given a unsigned char,
    > > should returns its bit string representation.

    > HW questions, hmmmmm...


    Well, (1) he says it's homework, right up front, and (2) he posts
    his current code and asks for help on finding his error. Sounds like
    someone who deserves help, which I see you have provided. I don't
    think anyone here has a problem helping people with their homework.
    It's the ones who come here asking us to _do_ their homework that
    are the problem.

    Finally, those who post with gender-neutral names will be refered to
    as "he" until contradictory information is provided. :)

    [...]

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, May 20, 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. John Saunders
    Replies:
    1
    Views:
    691
    John Saunders
    Nov 18, 2003
  2. Earl Eiland
    Replies:
    3
    Views:
    533
    Peter Hansen
    May 16, 2005
  3. Trent Mick
    Replies:
    0
    Views:
    486
    Trent Mick
    May 16, 2005
  4. Greenhorn
    Replies:
    15
    Views:
    847
    Keith Thompson
    Mar 6, 2005
  5. Replies:
    2
    Views:
    306
    Grant Wagner
    Dec 23, 2004
Loading...

Share This Page