Memory allocation with void pointer

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

    Memory allocation with void pointer

    Does this code delete right amount of memory:

    struct SAMPLE{
    int a,b,c;
    char zzz;
    };

    void* ptr;
    int main()
    {
    ptr=(void*) new[SAMPLE];
    delete ptr; //delete void pointer without casting it
    }
  • John Harrison

    #2
    Re: Memory allocation with void pointer


    "Maedowan" <Maedowan@gaz.p l> wrote in message
    news:chhnfc$c4c $3@inews.gazeta .pl...[color=blue]
    > Does this code delete right amount of memory:
    >
    > struct SAMPLE{
    > int a,b,c;
    > char zzz;
    > };
    >
    > void* ptr;
    > int main()
    > {
    > ptr=(void*) new[SAMPLE];
    > delete ptr; //delete void pointer without casting it
    > }[/color]

    No, the behaviour of this program is completely undefined by the C++
    standard. It might be OK, it might not.

    john


    Comment

    • Maedowan

      #3
      Re: Memory allocation with void pointer

      John Harrison wrote:
      [color=blue]
      >
      > "Maedowan" <Maedowan@gaz.p l> wrote in message
      > news:chhnfc$c4c $3@inews.gazeta .pl...[color=green]
      >> Does this code delete right amount of memory:
      >>
      >> struct SAMPLE{
      >> int a,b,c;
      >> char zzz;
      >> };
      >>
      >> void* ptr;
      >> int main()
      >> {
      >> ptr=(void*) new[SAMPLE];
      >> delete ptr; //delete void pointer without casting it
      >> }[/color]
      >
      > No, the behaviour of this program is completely undefined by the C++
      > standard. It might be OK, it might not.[/color]

      but what about free and malloc, they use void* pointers and does it right.

      Comment

      • John Harrison

        #4
        Re: Memory allocation with void pointer


        "Maedowan" <Maedowan@gaz.p l> wrote in message
        news:chhqi8$sqk $3@inews.gazeta .pl...[color=blue]
        > John Harrison wrote:
        >[color=green]
        > >
        > > "Maedowan" <Maedowan@gaz.p l> wrote in message
        > > news:chhnfc$c4c $3@inews.gazeta .pl...[color=darkred]
        > >> Does this code delete right amount of memory:
        > >>
        > >> struct SAMPLE{
        > >> int a,b,c;
        > >> char zzz;
        > >> };
        > >>
        > >> void* ptr;
        > >> int main()
        > >> {
        > >> ptr=(void*) new[SAMPLE];
        > >> delete ptr; //delete void pointer without casting it
        > >> }[/color]
        > >
        > > No, the behaviour of this program is completely undefined by the C++
        > > standard. It might be OK, it might not.[/color]
        >
        > but what about free and malloc, they use void* pointers and does it right.[/color]

        free and malloc would be fine. There's no reason you can't use them in a C++
        program.

        john


        Comment

        • Maedowan

          #5
          Re: Memory allocation with void pointer

          John Harrison wrote:
          [color=blue]
          >
          > free and malloc would be fine. There's no reason you can't use them in a
          > C++ program.[/color]

          becouse i don't want to include stdlib, it will incrase size of my
          executable. Is this code proper in c++ :

          void* ptr=new SAMPLE;
          delete (SAMPLE*) ptr; //casting ptr to proper type.

          Comment

          • John Harrison

            #6
            Re: Memory allocation with void pointer


            "Maedowan" <Maedowan@gaz.p l> wrote in message
            news:chhrdh$sqk $6@inews.gazeta .pl...[color=blue]
            > John Harrison wrote:
            >[color=green]
            > >
            > > free and malloc would be fine. There's no reason you can't use them in a
            > > C++ program.[/color]
            >
            > becouse i don't want to include stdlib, it will incrase size of my
            > executable. Is this code proper in c++ :
            >
            > void* ptr=new SAMPLE;
            > delete (SAMPLE*) ptr; //casting ptr to proper type.[/color]

            That's fine.

            Remember that if you allocate an array then you must use delete[] when you
            deallocate (this was the other thing wrong with your original code)

            void* ptr=new SAMPLE[10];
            delete[] (SAMPLE*) ptr; // use delete[] for arrays

            john


            Comment

            • Gianni Mariani

              #7
              Re: Memory allocation with void pointer

              Maedowan wrote:[color=blue]
              > John Harrison wrote:
              >
              >[color=green]
              >>free and malloc would be fine. There's no reason you can't use them in a
              >>C++ program.[/color]
              >
              >
              > becouse i don't want to include stdlib, it will incrase size of my
              > executable. ...[/color]

              It is unlikely that including stdlib will increase the size of your
              executable at all.

              [color=blue]
              > ... Is this code proper in c++ :
              >
              > void* ptr=new SAMPLE;
              > delete (SAMPLE*) ptr; //casting ptr to proper type.[/color]

              The snipped above would work fine.

              You can also use "operator new".

              int main()
              {
              void * x = operator new( 50 );

              operator delete( x );
              }

              Comment

              • Maedowan

                #8
                Re: Memory allocation with void pointer

                John Harrison wrote:

                [color=blue]
                >
                > Remember that if you allocate an array then you must use delete[] when you
                > deallocate (this was the other thing wrong with your original code)
                >
                > void* ptr=new SAMPLE[10];
                > delete[] (SAMPLE*) ptr; // use delete[] for arrays
                >[/color]

                my orginal code wouldn't even compile becouse i wrote like this:
                new[SAMPLE], i don't now why i wrote like this but i did it

                Comment

                • Maedowan

                  #9
                  Re: Memory allocation with void pointer

                  Gianni Mariani wrote:

                  [color=blue][color=green]
                  >> ... Is this code proper in c++ :
                  >>
                  >> void* ptr=new SAMPLE;
                  >> delete (SAMPLE*) ptr; //casting ptr to proper type.[/color]
                  >
                  > The snipped above would work fine.
                  >
                  > You can also use "operator new".
                  >
                  > int main()
                  > {
                  > void * x = operator new( 50 );
                  >
                  > operator delete( x );
                  > }[/color]

                  that is interesting, doesn't delete use operator delete(), if it calls this
                  operator i think it's also possible to use delete sample;

                  Comment

                  • Gianni Mariani

                    #10
                    Re: Memory allocation with void pointer

                    Maedowan wrote:
                    ....[color=blue]
                    >
                    > that is interesting, doesn't delete use operator delete(), if it calls this
                    > operator i think it's also possible to use delete sample;[/color]

                    Be more clear about your question - post a code snippet of what you mean
                    you think you can do.

                    Comment

                    Working...