C/C++ practice question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J

    C/C++ practice question

    Dear Advanced users,

    Here are parts of a code sample and I need to find out a few things from
    this.
    Please tell me your input. I am supposed to get others help as well to make
    the answers as accurate as possible.


    char * p;

    p=test();

    char * test() {

    char buf[8];

    return (char *) buf;

    }



    1. If this is reasonable or not and why.

    2. Would this work at all?

    3. Why is this a good practice?

    4. How, exactly, could one get a second ¡®char *¡¯ to use back from this
    function? In other words, how can this function be modified to return a
    ¡®char *¡¯ from the function, and an additional ¡®char *¡¯ value in one
    function call. Please make sure that your answer will work even if the size
    of the char * desired is not known in the outside calling function.



    Include statements in called and calling functions.



    Thanks in advance..

    Joon






  • Rolf Magnus

    #2
    Re: C/C++ practice question

    J wrote:
    [color=blue]
    > Dear Advanced users,
    >
    > Here are parts of a code sample and I need to find out a few things
    > from this.
    > Please tell me your input. I am supposed to get others help as well to
    > make the answers as accurate as possible.
    >
    >
    > char * p;
    >
    > p=test();
    >
    > char * test() {
    >
    > char buf[8];
    >
    > return (char *) buf;
    >
    > }
    >
    >
    >
    > 1. If this is reasonable or not and why.[/color]

    No. buf will only exist as long as test() is running. As soon as that
    function returns, buf ceases to exist. Also, the cast is superfluous
    and should be removed. Unneeded casts often increase the risk of making
    an error.
    [color=blue]
    > 2. Would this work at all?[/color]

    No.
    [color=blue]
    > 3. Why is this a good practice?[/color]

    What? Returning pointers to non-static local variables? That's not only
    not good practice, it's even illegal in C++, or at least using the
    returned pointer is.
    [color=blue]
    > 4. How, exactly, could one get a second ¡®char *¡¯ to use back from
    > this function? In other words, how can this function be modified to
    > return a ¡®char *¡¯ from the function, and an additional ¡®char *¡¯
    > value in one function call.[/color]

    I don't understand that.
    [color=blue]
    > Please make sure that your answer will work even if the size of the
    > char * desired is not known in the outside calling function.[/color]

    The size of a char* is always known in a C++ program. It's a fixed
    value. Probably, you meant the size of the buffer that it points to
    (actually, it points to the first element of that buffer). Anyway, you
    can only know that size outside of the function if you either pass that
    size out too or provide some end marker in the buffer. In C style
    strings, this is e.g. '\0'.

    Comment

    • Gavin Deane

      #3
      Re: C/C++ practice question

      "Sin" <broa29@hotmail .com> wrote in message news:<0_IKa.460 5$iM4.725883@ne ws20.bellglobal .com>...[color=blue][color=green]
      > > 4. How, exactly, could one get a second ¡®char *¡¯ to use back from this
      > > function? In other words, how can this function be modified to return a
      > > ¡®char *¡¯ from the function, and an additional ¡®char *¡¯ value in one
      > > function call. Please make sure that your answer will work even if the[/color]
      > size[color=green]
      > > of the char * desired is not known in the outside calling function.[/color]
      >
      > Eumm. The question isn't exactly clear. One way to interpret this is that we
      > would want test to return distinct values which might, for example, be
      > compared later on. This is how it would be done in C++ :
      >
      > char* p1= test();
      > char* p2= test();
      > // use p1 and p2 for whatever
      > delete p1;
      > delete p2;
      >
      > char* test() {
      > return new char[8];
      > }
      >
      >
      > Another way to interpret this is that we want test to return TWO buffers at
      > the same time, in one function call. One way is to return a char * array. I
      > haven't used this syntax in over 5 years, so the code might be wrong... But
      > it'll give you the idea...
      >
      > char* p[]= test();
      > // use p[0] and p[1] for whatever[/color]

      You forgot

      delete p[0];
      delete p[1];

      Which nicely illustrates the dangers of using new anywhere other than
      in a constructor.
      [color=blue]
      > delete p[];
      >
      > char*[] test() {
      > char *p[]= new char*[2];
      > p[0]= new char[8];
      > p[1]= new char[8];
      > return p;
      > }[/color]

      Comment

      • Karl Heinz Buchegger

        #4
        Re: C/C++ practice question



        Sin wrote:[color=blue]
        >[/color]
        [color=blue]
        > Eumm. The question isn't exactly clear. One way to interpret this is that we
        > would want test to return distinct values which might, for example, be
        > compared later on. This is how it would be done in C++ :
        >
        > char* p1= test();
        > char* p2= test();
        > // use p1 and p2 for whatever
        > delete p1;[/color]

        delete [] p1;
        [color=blue]
        > delete p2;[/color]

        delete [] p2;
        [color=blue]
        >
        > char* test() {
        > return new char[8];
        > }
        >
        > Another way to interpret this is that we want test to return TWO buffers at
        > the same time, in one function call. One way is to return a char * array. I
        > haven't used this syntax in over 5 years, so the code might be wrong... But
        > it'll give you the idea...
        >
        > char* p[]= test();
        > // use p[0] and p[1] for whatever
        > delete p[];[/color]

        delete [] p[0];
        delete [] p[1];
        delete [] p;
        [color=blue]
        >
        > char*[] test() {
        > char *p[]= new char*[2];
        > p[0]= new char[8];
        > p[1]= new char[8];
        > return p;
        > }
        >
        > Another way would be to pass the values as parameters which can be modified.
        > Like this :
        >
        > char* p1;
        > char* p2;
        > test(&p1, &p2);
        > // use p1 & p2 for whatever
        > delete p1;[/color]

        delete [] p1;
        [color=blue]
        > delete p2;[/color]

        delete [] p2;
        [color=blue]
        >
        > void test(char **p1, char **p2) {
        > *p1= new char[8];
        > *p2= new char[8];
        > }
        >[/color]

        A nice demonstration, why returning dynamically allocated objects
        is a bad idea most of the time. The caller has to know about
        the details on how this allocation has been done.

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Karl Heinz Buchegger

          #5
          Re: C/C++ practice question



          Gavin Deane wrote:[color=blue]
          >[/color]
          [color=blue][color=green]
          > >
          > > char* p[]= test();
          > > // use p[0] and p[1] for whatever[/color]
          >
          > You forgot
          >
          > delete p[0];
          > delete p[1];
          >
          > Which nicely illustrates the dangers of using new anywhere other than
          > in a constructor.[/color]

          :-)

          delete [] p[0];
          delete [] p[1];

          Even more arguments why one shouldn't do this.


          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • Sin

            #6
            Re: C/C++ practice question

            > You forgot[color=blue]
            >
            > delete p[0];
            > delete p[1];
            >
            > Which nicely illustrates the dangers of using new anywhere other than
            > in a constructor.[/color]

            Right, sorry... This was my mistake, not just rusty memory... :)

            Alex.


            Comment

            Working...