Memory allocation with void pointer

Discussion in 'C++' started by Maedowan, Sep 6, 2004.

  1. Maedowan

    Maedowan Guest

    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
    }
     
    Maedowan, Sep 6, 2004
    #1
    1. Advertising

  2. "Maedowan" <> wrote in message
    news:chhnfc$c4c$...
    > 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
    > }


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

    john
     
    John Harrison, Sep 6, 2004
    #2
    1. Advertising

  3. Maedowan

    Maedowan Guest

    John Harrison wrote:

    >
    > "Maedowan" <> wrote in message
    > news:chhnfc$c4c$...
    >> 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
    >> }

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


    but what about free and malloc, they use void* pointers and does it right.
     
    Maedowan, Sep 6, 2004
    #3
  4. Maedowan

    Maedowan Guest

    John Harrison wrote:

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


    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.
     
    Maedowan, Sep 6, 2004
    #4
  5. "Maedowan" <> wrote in message
    news:chhqi8$sqk$...
    > John Harrison wrote:
    >
    > >
    > > "Maedowan" <> wrote in message
    > > news:chhnfc$c4c$...
    > >> 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
    > >> }

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

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


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

    john
     
    John Harrison, Sep 6, 2004
    #5
  6. Maedowan wrote:
    > John Harrison wrote:
    >
    >
    >>free and malloc would be fine. There's no reason you can't use them in a
    >>C++ program.

    >
    >
    > becouse i don't want to include stdlib, it will incrase size of my
    > executable. ...


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


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


    The snipped above would work fine.

    You can also use "operator new".

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

    operator delete( x );
    }
     
    Gianni Mariani, Sep 6, 2004
    #6
  7. "Maedowan" <> wrote in message
    news:chhrdh$sqk$...
    > John Harrison wrote:
    >
    > >
    > > free and malloc would be fine. There's no reason you can't use them in a
    > > C++ program.

    >
    > 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.


    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
     
    John Harrison, Sep 6, 2004
    #7
  8. Maedowan

    Maedowan Guest

    John Harrison wrote:


    >
    > 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
    >


    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
     
    Maedowan, Sep 6, 2004
    #8
  9. Maedowan

    Maedowan Guest

    Gianni Mariani wrote:


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

    >
    > The snipped above would work fine.
    >
    > You can also use "operator new".
    >
    > int main()
    > {
    > void * x = operator new( 50 );
    >
    > operator delete( x );
    > }


    that is interesting, doesn't delete use operator delete(), if it calls this
    operator i think it's also possible to use delete sample;
     
    Maedowan, Sep 6, 2004
    #9
  10. Maedowan wrote:
    ....
    >
    > that is interesting, doesn't delete use operator delete(), if it calls this
    > operator i think it's also possible to use delete sample;


    Be more clear about your question - post a code snippet of what you mean
    you think you can do.
     
    Gianni Mariani, Sep 6, 2004
    #10
    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. Ollej Reemt
    Replies:
    7
    Views:
    605
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    839
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    885
    S.Tobias
    Jul 22, 2005
  4. Ken
    Replies:
    24
    Views:
    3,950
    Ben Bacarisse
    Nov 30, 2006
  5. Replies:
    1
    Views:
    438
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page