Is this code free the memory

V

Virtual_X

int x=5;

delete(&x);


is the code above delete the block of memory which have be reserved to
x

or the function delete is only for use with pointers
 
V

Victor Bazarov

Virtual_X said:
int x=5;

delete(&x);


is the code above delete the block of memory which have be reserved to
x

or the function delete is only for use with pointers

The code has undefined behaviour because you're passing the address
of the object that wasn't allocated using 'new' to 'delete'. It is
impossible to say what the code will do. Most likely it will crash
(abnormally terminate) your program.

V
 
J

Jim Langston

Virtual_X said:
int x=5;

delete(&x);


is the code above delete the block of memory which have be reserved to
x

or the function delete is only for use with pointers

That is in error and most likely when run will produce some type of OS error
such as a segmentation fault.

delete should only be used on memory allocated with new, use delete[] with
new[]

new returns a pointer to the memory, and delete is called on a pointer. How
you get access to that pointer is up to you.

For example, the following program runs on my computer (although I'm not
suggesting you code like this, I'm just saying it's possible).

int main()
{
int* Foo = new int;
int& Bar = *Foo;

Bar = 10;

delete &Bar;

}

Although I didn't delete using the original pointer variable Foo, I did
delete using the value of the pointer itself (I.E, the pointer value that
new returned).
 
V

Victor Bazarov

Jim said:
[..]
delete should only be used on memory allocated with new, use delete[]
with new[]

new returns a pointer to the memory, and delete is called on a
pointer. How you get access to that pointer is up to you.

For example, the following program runs on my computer (although I'm
not suggesting you code like this, I'm just saying it's possible).

It should run fine on any computer (provided there is a compiler
that can create a runable program on that computer, and the compiler
is worth the bytes it uses to run).
int main()
{
int* Foo = new int;
int& Bar = *Foo;

Bar = 10;

delete &Bar;

}

Although I didn't delete using the original pointer variable Foo, I
did delete using the value of the pointer itself (I.E, the pointer
value that new returned).

V
 
V

Virtual_X

int x=5;

is the code above delete the block of memory which have be reserved to
x
or the function delete is only for use with pointers

That is in error and most likely when run will produce some type of OS error
such as a segmentation fault.

delete should only be used on memory allocated with new, use delete[] with
new[]

new returns a pointer to the memory, and delete is called on a pointer. How
you get access to that pointer is up to you.

For example, the following program runs on my computer (although I'm not
suggesting you code like this, I'm just saying it's possible).

int main()
{
int* Foo = new int;
int& Bar = *Foo;

Bar = 10;

delete &Bar;

}

Although I didn't delete using the original pointer variable Foo, I did
delete using the value of the pointer itself (I.E, the pointer value that
new returned).

thank's
 

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
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top