C char* and C++ new

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

    C char* and C++ new

    I've got a problem. I can't seem to figure out how to make a char*
    buffer for use as a socket buffer using operator 'new' in it's basic
    form. In affect, I need to allocate space dynamically in memory
    pointed to by a char*.

    It doesn't appear I can use 'char buffer[length + 5];' and then pass
    buffer around as a char*. The compiler complains about being unable
    to evaluate the non-constant term between the brackets.

    So I thought I might be able to do something like this with new:

    int length += 5;
    char* buffer = new char[length];

    It appears to work - at least my 1995 vintage Unix 10.20 compiler
    doesn't complain about the non-constantness. But my program is
    seriously broken. It looks like a buffer over-run with garbage in the
    stream. I have also wondered if my declaration did work but my use is
    wrong. Perhaps I really have a char** and not a char*. Do I need to
    terminate the message with a \0? What am I doing wrong and how can I
    achieve a dynamic allocation like this?

    Dave
  • Victor Bazarov

    #2
    Re: C char* and C++ new

    "Dave" <spagnet@austin .rr.com> wrote...[color=blue]
    > I've got a problem. I can't seem to figure out how to make a char*
    > buffer for use as a socket buffer using operator 'new' in it's basic
    > form. In affect, I need to allocate space dynamically in memory
    > pointed to by a char*.
    >
    > It doesn't appear I can use 'char buffer[length + 5];' and then pass
    > buffer around as a char*. The compiler complains about being unable
    > to evaluate the non-constant term between the brackets.
    >
    > So I thought I might be able to do something like this with new:
    >
    > int length += 5;[/color]

    Ahem... You mean

    int length = someotherlength + 5;

    don't you?
    [color=blue]
    > char* buffer = new char[length];
    >
    > It appears to work[/color]

    Really? Your declaration of 'length' shouldn't even compile.
    [color=blue]
    > - at least my 1995 vintage Unix 10.20 compiler
    > doesn't complain about the non-constantness.[/color]

    In this form of new expression, you are allowed to have non-constant
    expression in brackets. Mind you, it's not a declaration, it's
    an expression that you use to initialise your pointer.
    [color=blue]
    > But my program is
    > seriously broken.[/color]

    Not due to your 'new' expression.
    [color=blue]
    > It looks like a buffer over-run with garbage in the
    > stream.[/color]

    Well, you must be overrunning the buffer... But that usually happens
    due to an error in either calculation of an index or of the size of
    the array. Make sure you calculate them right.
    [color=blue]
    > I have also wondered if my declaration did work but my use is
    > wrong.[/color]

    Probably.
    [color=blue]
    > Perhaps I really have a char** and not a char*.[/color]

    No, you seem to be doing this part correctly.
    [color=blue]
    > Do I need to
    > terminate the message with a \0?[/color]

    That's up to your algorithm. Does it require (presume) the zero at
    the end?
    [color=blue]
    > What am I doing wrong and how can I
    > achieve a dynamic allocation like this?[/color]

    Take any book on C++ and read the chapter on dynamic memory allocation.
    It's such a basic stuff that nobody should have to explain it in a NG
    posting.

    Also, read the FAQ section 5, especially question 5.8. Right up your
    alley.

    Victor


    Comment

    • jeffc

      #3
      Re: C char* and C++ new


      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
      news:40pNb.5598 1$5V2.67078@att bi_s53...[color=blue][color=green]
      > > What am I doing wrong and how can I
      > > achieve a dynamic allocation like this?[/color]
      >
      > Take any book on C++ and read the chapter on dynamic memory allocation.
      > It's such a basic stuff that nobody should have to explain it in a NG
      > posting.[/color]

      Ouch!


      Comment

      • Dave

        #4
        Re: C char* and C++ new

        "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:<40pNb.559 81$5V2.67078@at tbi_s53>...[color=blue]
        > "Dave" <spagnet@austin .rr.com> wrote...[color=green]
        >> snip<<
        > >
        > > So I thought I might be able to do something like this with new:
        > >
        > > int length += 5;[/color]
        >
        > Ahem... You mean
        >
        > int length = someotherlength + 5;
        >
        > don't you?
        >[color=green]
        > > char* buffer = new char[length];
        > >
        > > It appears to work[/color]
        >
        > Really? Your declaration of 'length' shouldn't even compile.
        >[color=green]
        > > - at least my 1995 vintage Unix 10.20 compiler
        > > doesn't complain about the non-constantness.[/color]
        >
        > In this form of new expression, you are allowed to have non-constant
        > expression in brackets. Mind you, it's not a declaration, it's
        > an expression that you use to initialise your pointer.
        >[color=green]
        > > But my program is
        > > seriously broken.[/color]
        >
        > Not due to your 'new' expression.
        >[color=green]
        > > It looks like a buffer over-run with garbage in the
        > > stream.[/color]
        >
        > Well, you must be overrunning the buffer... But that usually happens
        > due to an error in either calculation of an index or of the size of
        > the array. Make sure you calculate them right.
        >[color=green]
        > > I have also wondered if my declaration did work but my use is
        > > wrong.[/color]
        >
        > Probably.
        >[color=green]
        > > Perhaps I really have a char** and not a char*.[/color]
        >
        > No, you seem to be doing this part correctly.
        >[color=green]
        > > Do I need to
        > > terminate the message with a \0?[/color]
        >
        > That's up to your algorithm. Does it require (presume) the zero at
        > the end?
        >[color=green]
        > > What am I doing wrong and how can I
        > > achieve a dynamic allocation like this?[/color]
        >
        > Take any book on C++ and read the chapter on dynamic memory allocation.
        > It's such a basic stuff that nobody should have to explain it in a NG
        > posting.
        >
        > Also, read the FAQ section 5, especially question 5.8. Right up your
        > alley.
        >
        > Victor[/color]

        Thanks Victor. I'll read section 5. I have several books on C++
        including Stroustrups 3rd Ed of C++ Programming Language and Osborne's
        Complete Reference. Unfortunately they all love the terse example of
        creating a fixed size array. I have yet to find a good example of
        dynamically allocating arrays using 'new'. This may be a task best
        suited for malloc.

        Comment

        • Victor Bazarov

          #5
          Re: C char* and C++ new

          "jeffc" <nobody@nowhere .com> wrote...[color=blue]
          >
          > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
          > news:40pNb.5598 1$5V2.67078@att bi_s53...[color=green][color=darkred]
          > > > What am I doing wrong and how can I
          > > > achieve a dynamic allocation like this?[/color]
          > >
          > > Take any book on C++ and read the chapter on dynamic memory allocation.
          > > It's such a basic stuff that nobody should have to explain it in a NG
          > > posting.[/color]
          >
          > Ouch![/color]

          What?!


          Comment

          • Howard

            #6
            Re: C char* and C++ new


            [color=blue]
            > Thanks Victor. I'll read section 5. I have several books on C++
            > including Stroustrups 3rd Ed of C++ Programming Language and Osborne's
            > Complete Reference. Unfortunately they all love the terse example of
            > creating a fixed size array. I have yet to find a good example of
            > dynamically allocating arrays using 'new'. This may be a task best
            > suited for malloc.[/color]

            No, no, no, no, no! :-) Don't use malloc. Using new[] and delete[] is much
            better. You just need a book with better examples. Stroustrup covers so
            much ground that there's not much room for example code.

            One good method of allocating arrays that might vary in size is to allocate
            an array as large as it will ever need to be, and never resizing it.
            Assuming you know what the maximum size is, of course. You don't have to
            have an array that's EXACTLY the size you need, just one that's AT LEAST
            that size. You can always keep the size you're ACTUALLY using in a variable
            (which you probably had to do anyway in order to allocate it, right?).

            As others have suggested, if you don't know how big the array might need to
            get, you can re-allocate it later, but if you do, it's better to grow it by,
            say, twice its old size, than just to the amount requested. And never
            shrink it. That saves a lot of re-allocations.

            Probably the best idea is to use the std::vector class. That stl class has
            saved me a LOT of pain, believe me! :-)

            -Howard



            Comment

            • Adam Fineman

              #7
              Re: C char* and C++ new

              Dave wrote:[color=blue]
              > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message news:<40pNb.559 81$5V2.67078@at tbi_s53>...
              >[/color]
              <snip>[color=blue][color=green]
              >>
              >>Also, read the FAQ section 5, especially question 5.8. Right up your
              >>alley.
              >>
              >>Victor[/color]
              >
              >
              > Thanks Victor. I'll read section 5.[/color]

              Perhaps you should read the entire FAQ before posting again. I'm not
              writing this to be nasty; if you aquaint yourself with the FAQ, you'll
              find it much easier to get help in the future. Also, you'll almost
              certainly find the answer to your particular problem.

              <snip>
              [color=blue]
              > This may be a task best
              > suited for malloc.[/color]

              Ugh. Read the FAQ.

              - Adam

              --
              Reverse domain name to reply.

              Comment

              • jeffc

                #8
                Re: C char* and C++ new


                "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                news:08zNb.7452 8$xy6.133050@at tbi_s02...[color=blue]
                > "jeffc" <nobody@nowhere .com> wrote...[color=green]
                > >
                > > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                > > news:40pNb.5598 1$5V2.67078@att bi_s53...[color=darkred]
                > > > > What am I doing wrong and how can I
                > > > > achieve a dynamic allocation like this?
                > > >
                > > > Take any book on C++ and read the chapter on dynamic memory[/color][/color][/color]
                allocation.[color=blue][color=green][color=darkred]
                > > > It's such a basic stuff that nobody should have to explain it in a NG
                > > > posting.[/color]
                > >
                > > Ouch![/color]
                >
                > What?![/color]

                See Adam's response for better manners.


                Comment

                Working...