malloc to new

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

    #1

    malloc to new

    Hi,

    i'm trying to use new/delete in C++ instead of malloc/free.
    I have this :

    int g_TTable_size=1 30000; // for example
    struct hash_t
    {
    int depth;
    int flags;
    int eval;
    };

    And this works fine :
    hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
    if(pt_htable!=N ULL) {free(pt_htable );}

    But this not :-((
    hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];
    neither :
    hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
    And this may be ok, i don't know yet :
    if(pt_htable!=N ULL) {delete pt_htable;}

    What am i doing wrong ?

    Thanks for your help.
  • Jeff Flinn

    #2
    Re: malloc to new


    "Jeff" <nomail@nospam. com> wrote in message
    news:416fd979$0 $26683$8fcfb975 @news.wanadoo.f r...[color=blue]
    > Hi,
    >
    > i'm trying to use new/delete in C++ instead of malloc/free.
    > I have this :
    >
    > int g_TTable_size=1 30000; // for example
    > struct hash_t
    > {
    > int depth;
    > int flags;
    > int eval;
    > };
    >
    > And this works fine :
    > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
    > if(pt_htable!=N ULL) {free(pt_htable );}
    >
    > But this not :-((
    > hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];
    > neither :
    > hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
    > And this may be ok, i don't know yet :
    > if(pt_htable!=N ULL) {delete pt_htable;}
    >
    > What am i doing wrong ?[/color]

    Your not using the available tools:

    #include <vector>

    std::vector<has h_t> htable( g_TTable_size );

    Jeff


    Comment

    • Victor Bazarov

      #3
      Re: malloc to new

      Jeff wrote:[color=blue]
      > i'm trying to use new/delete in C++ instead of malloc/free.
      > I have this :
      >
      > int g_TTable_size=1 30000; // for example
      > struct hash_t
      > {
      > int depth;
      > int flags;
      > int eval;
      > };
      >
      > And this works fine :
      > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
      > if(pt_htable!=N ULL) {free(pt_htable );}
      >
      > But this not :-((
      > hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];[/color]

      a) There is no need to cast the result. Drop the cast.
      b) The size if wrong. The expression in the brackets should not have
      the 'sizeof' part.
      [color=blue]
      > neither :
      > hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];[/color]

      a) This is better size-wise. But still, drop the damn cast.
      [color=blue]
      > And this may be ok, i don't know yet :
      > if(pt_htable!=N ULL) {delete pt_htable;}[/color]

      No, this is not OK. If you allocate using 'new[]', you need to use
      delete[]. Also, if allocation fails, an exception is thrown, the 'new'
      does not return NULL unless you use the 'nothrow' variation. Just say

      delete[] pt_htable;
      [color=blue]
      >
      > What am i doing wrong ?[/color]

      You're probably not reading your C++ book as carefully as you should.

      V

      Comment

      • JKop

        #4
        Re: malloc to new

        Jeff posted:
        [color=blue]
        > Hi,
        >
        > i'm trying to use new/delete in C++ instead of malloc/free.
        > I have this :
        >
        > int g_TTable_size=1 30000; // for example
        > struct hash_t
        > {
        > int depth;
        > int flags;
        > int eval;
        > };
        >
        > And this works fine :
        > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
        > if(pt_htable!=N ULL) {free(pt_htable );}
        >
        > But this not :-((
        > hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];
        > neither :
        > hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
        > And this may be ok, i don't know yet :
        > if(pt_htable!=N ULL) {delete pt_htable;}[/color]


        hash_t *pt_htablearray 1 = new hash_t[g_TTable_size];

        delete [] pt_htablearray;


        hash_t *pt_htablearray 2 = reinterpret_cas t<hash_t* const>( new char
        [g_TTable_size * sizeof(hash_t)] );

        delete [] pt_htablearray2 ;


        -JKop

        Comment

        • John Harrison

          #5
          Re: malloc to new


          "Jeff" <nomail@nospam. com> wrote in message
          news:416fd979$0 $26683$8fcfb975 @news.wanadoo.f r...[color=blue]
          > Hi,
          >
          > i'm trying to use new/delete in C++ instead of malloc/free.
          > I have this :
          >
          > int g_TTable_size=1 30000; // for example
          > struct hash_t
          > {
          > int depth;
          > int flags;
          > int eval;
          > };
          >
          > And this works fine :
          > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
          > if(pt_htable!=N ULL) {free(pt_htable );}
          >
          > But this not :-((
          > hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];
          > neither :
          > hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
          > And this may be ok, i don't know yet :
          > if(pt_htable!=N ULL) {delete pt_htable;}
          >
          > What am i doing wrong ?
          >[/color]

          This is correct

          hash_t *pt_htable = new hash_t[g_TTable_size];

          delete[] pt_htable;

          If you use new[], then you use delete[]. You don't need to test for NULL
          before doing delete or delete[].

          john


          Comment

          • Alf P. Steinbach

            #6
            Re: malloc to new

            * Jeff:[color=blue]
            >
            > i'm trying to use new/delete in C++ instead of malloc/free.
            >
            > I have this :
            >
            > int g_TTable_size=1 30000; // for example[/color]

            Should be const.

            [color=blue]
            > struct hash_t
            > {
            > int depth;
            > int flags;
            > int eval;
            > };
            >
            > And this works fine :
            > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
            > if(pt_htable!=N ULL) {free(pt_htable );}
            >
            > But this not :-((
            > hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];[/color]

            'new' isn't supposed to be used that way.

            [color=blue]
            > neither :
            > hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];[/color]

            This should be OK, except for the C style cast which you should remove.

            Note that if the allocation fails you will get a std::bad_alloc exception
            (with a standard-conforming C++ implementation) .

            [color=blue]
            > And this may be ok, i don't know yet :
            > if(pt_htable!=N ULL) {delete pt_htable;}[/color]

            You don't need to check for NULL because operator 'delete' does that for
            you.


            [color=blue]
            > What am i doing wrong ?[/color]

            Technically it's impossible to say since the second attempt at 'new' should
            work.

            Generally, it's coding at a too low abstraction level, using C++ as C.

            Remove all the casts and stuff, and also think about removing direct pointer
            handling, e.g. by using std::vector as suggested by Jeff Flinn.

            --
            A: Because it messes up the order in which people normally read text.
            Q: Why is it such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?

            Comment

            • Gianni Mariani

              #7
              Re: malloc to new

              Jeff wrote:[color=blue]
              > Hi,
              >
              > i'm trying to use new/delete in C++ instead of malloc/free.
              > I have this :
              >
              > int g_TTable_size=1 30000; // for example
              > struct hash_t
              > {
              > int depth;
              > int flags;
              > int eval;
              > };
              >
              > And this works fine :
              > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
              > if(pt_htable!=N ULL) {free(pt_htable );}
              >
              > But this not :-((
              > hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];[/color]

              Very very ugly but it should work on most platforms for your struct -
              but it's a very bad habbit to get into.

              a much nicer way is :
              hash_t *pt_htable = new hash_t[g_TTable_size];

              [color=blue]
              > neither :
              > hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];[/color]

              new is type safe, you don't (MUST NOT) type cast the result.

              So what is the error ?
              [color=blue]
              > And this may be ok, i don't know yet :
              > if(pt_htable!=N ULL) {delete pt_htable;}[/color]

              use :
              delete[] pt_htable;

              // or if you did the ugly thing
              delete[] (char*) pt_htable;

              When deleting an array allocated with new T[N], you must use delete[].

              Also, you don't need to check for NULL. delete check for that for you.
              [color=blue]
              >
              > What am i doing wrong ?[/color]

              Other than the nasty cast thing and type casting the return from new ?
              [color=blue]
              >
              > Thanks for your help.[/color]

              Comment

              • Karl Heinz Buchegger

                #8
                Re: malloc to new

                Jeff wrote:[color=blue]
                >
                > Hi,
                >
                > i'm trying to use new/delete in C++ instead of malloc/free.
                > I have this :
                >
                > int g_TTable_size=1 30000; // for example
                > struct hash_t
                > {
                > int depth;
                > int flags;
                > int eval;
                > };
                >
                > And this works fine :
                > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
                > if(pt_htable!=N ULL) {free(pt_htable );}
                >
                > But this not :-((
                > hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];
                > neither :
                > hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
                > And this may be ok, i don't know yet :
                > if(pt_htable!=N ULL) {delete pt_htable;}
                >
                > What am i doing wrong ?[/color]

                Not reading a text book?

                hash_t *pt_htable = new hash_t [ g_TTable_size ];

                ....

                delete [] pt_htable;

                But the preferred solution would be:

                #include <vector>

                ....

                std::vector< hash_t > HTable;
                HTable.resize( g_TTable_size );

                since you no longer have to deal with freeing the memory.

                Do yourself a favour and get a C++ book and work through it.
                C++ is much to complex to be larned by trial and error.
                See eg. the FAW for recommendations . There are even free ones available.



                http://www.mindview.net/Books (Download a free copy of 'Thinking in C++')

                --
                Karl Heinz Buchegger
                kbuchegg@gascad .at

                Comment

                • JKop

                  #9
                  Re: malloc to new

                  [color=blue]
                  > You don't need to check for NULL because operator 'delete' does that
                  > for you.[/color]


                  .. . .inefficient?



                  -JKop

                  Comment

                  • Howard

                    #10
                    Re: malloc to new


                    "Jeff" <nomail@nospam. com> wrote in message
                    news:416fd979$0 $26683$8fcfb975 @news.wanadoo.f r...[color=blue]
                    > Hi,
                    >
                    > i'm trying to use new/delete in C++ instead of malloc/free.
                    > I have this :
                    >
                    > int g_TTable_size=1 30000; // for example
                    > struct hash_t
                    > {
                    > int depth;
                    > int flags;
                    > int eval;
                    > };
                    >
                    > And this works fine :
                    > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
                    > if(pt_htable!=N ULL) {free(pt_htable );}
                    >
                    > But this not :-((
                    > hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];
                    > neither :
                    > hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];[/color]

                    What didn't work with the above?

                    With new, there's no reason to cast. Just use:

                    hash_t *pt_htable = new hash_t[g_TTable_size];

                    [color=blue]
                    > And this may be ok, i don't know yet :
                    > if(pt_htable!=N ULL) {delete pt_htable;}[/color]

                    To delete when using the form new[], you need to use the matching form
                    delete[]. like this:

                    delete [] pt_htable;

                    (No need to check if it's NULL or not. For one thing, calling delete[] on a
                    NULL pointer is a no-op. Also, if new fails, you should get an exception.
                    Some compilers will return NULL, but as I said, that's safe to call delete[]
                    on.)

                    -Howard


                    Comment

                    • Alf P. Steinbach

                      #11
                      Re: malloc to new

                      * JKop:[color=blue]
                      >[color=green]
                      > > You don't need to check for NULL because operator 'delete' does that
                      > > for you.[/color]
                      >
                      >
                      > . . .inefficient?[/color]

                      In terms of programmer time, yes (in terms of execution time it doesn't
                      matter). And there should be a '[]' in there. Forgot to mention that.

                      --
                      A: Because it messes up the order in which people normally read text.
                      Q: Why is it such a bad thing?
                      A: Top-posting.
                      Q: What is the most annoying thing on usenet and in e-mail?

                      Comment

                      • Jeff

                        #12
                        Re: malloc to new

                        >> Hi,[color=blue][color=green]
                        >>
                        >> i'm trying to use new/delete in C++ instead of malloc/free.
                        >> I have this :
                        >>
                        >> int g_TTable_size=1 30000; // for example
                        >> struct hash_t
                        >> {
                        >> int depth;
                        >> int flags;
                        >> int eval;
                        >> };
                        >>
                        >> And this works fine :
                        >> hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
                        >> if(pt_htable!=N ULL) {free(pt_htable );}
                        >>
                        >> But this not :-((
                        >> hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];
                        >> neither :
                        >> hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
                        >> And this may be ok, i don't know yet :
                        >> if(pt_htable!=N ULL) {delete pt_htable;}
                        >>
                        >> What am i doing wrong ?[/color][/color]
                        [color=blue]
                        > Your not using the available tools:
                        > #include <vector>
                        > std::vector<has h_t> htable( g_TTable_size );
                        > Jeff[/color]

                        Thanks but i'm quite newbie, i don't undersand.
                        Ok about "#include <vector>" but what is "htable" ?
                        How to convert
                        hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
                        With "new" ?
                        Regards.

                        Comment

                        • Gianni Mariani

                          #13
                          Re: malloc to new

                          Alf P. Steinbach wrote:[color=blue]
                          > * Jeff:
                          >[color=green]
                          >>i'm trying to use new/delete in C++ instead of malloc/free.
                          >>
                          >>I have this :
                          >>
                          >>int g_TTable_size=1 30000; // for example[/color]
                          >
                          >
                          > Should be const.[/color]

                          Why ? Isn't it pointless to dynamically allocate const sized arrays ?

                          Comment

                          • Jeff Flinn

                            #14
                            Re: malloc to new


                            "Jeff" <nomail@nospam. com> wrote in message
                            news:416fdeec$0 $7189$8fcfb975@ news.wanadoo.fr ...[color=blue][color=green][color=darkred]
                            > >> Hi,
                            > >>
                            > >> i'm trying to use new/delete in C++ instead of malloc/free.
                            > >> I have this :
                            > >>
                            > >> int g_TTable_size=1 30000; // for example
                            > >> struct hash_t
                            > >> {
                            > >> int depth;
                            > >> int flags;
                            > >> int eval;
                            > >> };
                            > >>
                            > >> And this works fine :
                            > >> hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
                            > >> if(pt_htable!=N ULL) {free(pt_htable );}
                            > >>
                            > >> But this not :-((
                            > >> hash_t *pt_htable = (hash_t *) new char[g_TTable_size*s izeof(hash_t)];
                            > >> neither :
                            > >> hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
                            > >> And this may be ok, i don't know yet :
                            > >> if(pt_htable!=N ULL) {delete pt_htable;}
                            > >>
                            > >> What am i doing wrong ?[/color][/color]
                            >[color=green]
                            > > Your not using the available tools:
                            > > #include <vector>
                            > > std::vector<has h_t> htable( g_TTable_size );
                            > > Jeff[/color]
                            >
                            > Thanks but i'm quite newbie, i don't undersand.
                            > Ok about "#include <vector>" but what is "htable" ?[/color]

                            You'll achieve post-newbie-ness much quicker if you read a book on C++
                            standard containers, and forego manual memory management.

                            htable is an instance of a vector of 130000 hash_t struct instances
                            effectively initialized to zero(IIRC). std::vector manages all of the memory
                            in an exception safe manner.

                            void somefunc()
                            {
                            std::vector<int > someints( 3 ); // someints equivalent to { 0, 0, 0 }

                            someints[0] = 1; // assign values
                            someints[1] = 3;
                            someints[2] = 5;

                            someints.push_b ack( 7 ); // size is now 4

                            legacyfunction( &someints[0] ); // passes address of 'array'

                            } // someints memory now 'freed'
                            [color=blue]
                            > How to convert
                            > hash_t *pt_htable = (hash_t *) malloc (g_TTable_size* sizeof(hash_t)) ;
                            > With "new" ?[/color]

                            Patient: My head hurts after I bang it against the wall.
                            Doctor: Then don't bang it against the wall.

                            Jeff F


                            Comment

                            • Jeff

                              #15
                              Re: malloc to new

                              It's all right.
                              Thanks you all.

                              Comment

                              Working...