Memory

Discussion in 'C++' started by Michael, Aug 2, 2006.

  1. Michael

    Michael Guest

    Hi,

    After I execute program 1, will the memory be release as if I use
    program 2?

    #include <iostream>
    using namespace std;

    1.
    int main()
    {
    int x=1;
    return 0;
    }

    2.
    int main()
    {
    int* x=1;
    delete x;
    return 0;
    }

    Thanks in advance,
    Michael
     
    Michael, Aug 2, 2006
    #1
    1. Advertising

  2. Michael wrote:
    > After I execute program 1, will the memory be release as if I use
    > program 2?
    >
    > #include <iostream>
    > using namespace std;


    You don't need this, get rid of it.

    > 1.
    > int main()
    > {
    > int x=1;
    > return 0;


    There is no memory allocated here. All local objects cease to exist
    when the function returns; where they reside and how that is maintained
    is not specified in the language.

    > }
    >
    > 2.
    > int main()
    > {
    > int* x=1;


    This isn't going to compile. You cannot initialise a pointer from
    an integer literal except from 0. If you meant to write

    int a = 1;
    int *x = &a;

    then read on...

    > delete x;


    You cannot delete a pointer whose value you didn't obtain from 'new'.

    > return 0;
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 2, 2006
    #2
    1. Advertising

  3. Michael

    Michael Guest

    Victor Bazarov wrote:
    > Michael wrote:
    > > After I execute program 1, will the memory be release as if I use
    > > program 2?
    > >
    > > #include <iostream>
    > > using namespace std;

    >
    > You don't need this, get rid of it.
    >
    > > 1.
    > > int main()
    > > {
    > > int x=1;
    > > return 0;

    >
    > There is no memory allocated here. All local objects cease to exist
    > when the function returns; where they reside and how that is maintained
    > is not specified in the language.
    >
    > > }
    > >
    > > 2.
    > > int main()
    > > {
    > > int* x=1;

    >
    > This isn't going to compile. You cannot initialise a pointer from
    > an integer literal except from 0. If you meant to write
    >
    > int a = 1;
    > int *x = &a;
    >
    > then read on...
    >
    > > delete x;

    >
    > You cannot delete a pointer whose value you didn't obtain from 'new'.
    >
    > > return 0;
    > > }



    Yes.

    int main()
    {
    int a=1;
    int* x;
    x=new int;
    x=&a;
    delete x;

    return 0;
    }

    What are local objects ?
     
    Michael, Aug 2, 2006
    #3
  4. Michael wrote:
    > [..]
    > Yes.
    >
    > int main()
    > {
    > int a=1;
    > int* x;
    > x=new int;


    OK, here you allocate some memory and store the pointer to that memory
    in 'x'.

    > x=&a;


    Here you _lose_ the pointer to the allocated memory because you now
    store the address of 'a' in 'x'.

    > delete x;


    Here you attempt to delete a pointer you didn't obtain from 'new'.
    The program hence has undefined behaviour.

    >
    > return 0;
    > }
    >
    > What are local objects ?


    Objects whose scope is limited to a function.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 2, 2006
    #4
  5. Michael

    Michael Guest

    Victor Bazarov wrote:
    > Michael wrote:
    > > [..]
    > > Yes.
    > >
    > > int main()
    > > {
    > > int a=1;
    > > int* x;
    > > x=new int;

    >
    > OK, here you allocate some memory and store the pointer to that memory
    > in 'x'.
    >
    > > x=&a;

    >
    > Here you _lose_ the pointer to the allocated memory because you now
    > store the address of 'a' in 'x'.
    >
    > > delete x;

    >
    > Here you attempt to delete a pointer you didn't obtain from 'new'.
    > The program hence has undefined behaviour.
    >
    > >
    > > return 0;
    > > }
    > >


    if I want to use x=new int, is there a way to assign the address of a
    to x, and then delete x?
     
    Michael, Aug 2, 2006
    #5
  6. Michael wrote:
    > [..]
    > if I want to use x=new int, is there a way to assign the address of a
    > to x, and then delete x?


    I do not understand what you're trying to do.

    x = new int; // given that 'x' is a pointer to 'int'

    allocates memory for an object of type 'int' and assigns the address
    of that object to 'x'. If you need a dynamically allocated object
    of type 'int', that's what you need to do. After you don't need that
    dynamically allocated object any longer, you do 'delete x'.

    If you need to somehow use the address of 'a' for whatever reason,
    then you take the address of 'a' ('&a') and assign it to 'x'. You
    must not use 'delete x' in that case since you didn't obtain the
    value from 'new'.

    There is no scenario where you need to do both *at the same time*.
    You only may need to do either of the two, never together. The
    sentence you wrote ("If I want to use ...") just makes no sense
    whatsoever.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 2, 2006
    #6
  7. Michael

    Michael Guest


    >
    > There is no scenario where you need to do both *at the same time*.
    > You only may need to do either of the two, never together. The
    > sentence you wrote ("If I want to use ...") just makes no sense
    > whatsoever.
    >


    So :

    1.
    #include <iostream>
    using namespace std;
    int main()
    {

    int* x;
    x=new int;
    *x=1;
    cout<<*x;
    delete x;

    return 0;

    }

    2.
    #include <iostream>
    using namespace std;
    int main()
    {
    int a=1;
    int* x;
    x=&a;
    cout<<*x;

    return 0;

    }

    The first one is to allocate memory dynamically, which need delete
    statement
    The second one is to create a point points to type 'int' variables
    address, which is a local object so it does not need delete at all.

    Is that right?
     
    Michael, Aug 2, 2006
    #7
  8. Michael

    Ian Collins Guest

    Michael wrote:
    >>There is no scenario where you need to do both *at the same time*.
    >>You only may need to do either of the two, never together. The
    >>sentence you wrote ("If I want to use ...") just makes no sense
    >>whatsoever.
    >>

    >
    >
    > So :
    >
    > 1.
    > #include <iostream>
    > using namespace std;


    You don't need this.

    > int main()
    > {
    >
    > int* x;
    > x=new int;
    > *x=1;
    > cout<<*x;
    > delete x;
    >
    > return 0;
    >
    > }
    >
    > 2.
    > #include <iostream>
    > using namespace std;
    > int main()
    > {
    > int a=1;
    > int* x;
    > x=&a;
    > cout<<*x;
    >
    > return 0;
    >
    > }
    >
    > The first one is to allocate memory dynamically, which need delete
    > statement

    Yes.

    > The second one is to create a point points to type 'int' variables
    > address, which is a local object so it does not need delete at all.
    >
    > Is that right?
    >

    Yes.

    --
    Ian Collins.
     
    Ian Collins, Aug 2, 2006
    #8
    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. Mahesh Prasad
    Replies:
    1
    Views:
    711
    Tom Wells
    Feb 22, 2004
  2. Cy Huckaba
    Replies:
    1
    Views:
    1,153
    Xie Xiao
    Jun 26, 2003
  3. Julián Sanz García

    RAM Memory or virual memory

    Julián Sanz García, Nov 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    901
    Julián Sanz García
    Nov 12, 2004
  4. humbleaptience
    Replies:
    0
    Views:
    5,491
    humbleaptience
    Feb 22, 2006
  5. Lee
    Replies:
    8
    Views:
    1,183
    Chris Uppal
    Jan 24, 2004
Loading...

Share This Page