Memory

M

Michael

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
 
V

Victor Bazarov

Michael said:
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
 
M

Michael

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


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.


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


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


Yes.

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

return 0;
}

What are local objects ?
 
V

Victor Bazarov

Michael said:
[..]
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'.

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
 
M

Michael

Victor said:
Michael said:
[..]
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'.

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

Victor Bazarov

Michael said:
[..]
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
 
M

Michael

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

Ian Collins

Michael said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top