Passing dereferenced new pointer to reference paramter

S

shanemh

I'm starting out with c++ and for some reason I cant get my brain
around this one:

If I have the following:

void Foo (someClass& x)
{}

int Main (void)
{
Foo(*(new someClass));
}


Is the memory allocated for the 'new someClass' that is passed to Foo,
automatically deleted, or must it be deleted using 'delete'? If so, how
given that the new someClass is never actually assigned to a pointer?
Or, is it assigned to x... i'm not sure....i think i might be missing
something here in the way c++ treats this kind of thing.

Thanks
 
A

Alf P. Steinbach

* (e-mail address removed):
I'm starting out with c++ and for some reason I cant get my brain
around this one:

If I have the following:

void Foo (someClass& x)
{}

int Main (void)

Note 1: C++ is a case-sensitive language; that should be 'main'.
Note 2: C++ is not C, using 'void' there is a C'ism best a-voided.
Note 3: When posting code, copy and paste code that compiles.
{
Foo(*(new someClass));
}


Is the memory allocated for the 'new someClass' that is passed to Foo,
automatically deleted

Not during program execution.

, or must it be deleted using 'delete'? If so, how
given that the new someClass is never actually assigned to a pointer?

Do this:

int main()
{
someClass o;
Foo( o );
}

Forget about pointers and 'new' until you have mastered local objects
and standard library container classes.

Or, is it assigned to x... i'm not sure....i think i might be missing
something here in the way c++ treats this kind of thing.

You do, yes (see above).
 
S

shanemh

Alf said:
* (e-mail address removed):

Note 1: C++ is a case-sensitive language; that should be 'main'.
Note 2: C++ is not C, using 'void' there is a C'ism best a-voided.
Note 3: When posting code, copy and paste code that compiles.

Oops. yeah i'll b more careful next time, notes noted.

Not during program execution.


So, you're saying it will be deleted after program execution?

My understanding is that 'new' will return a pointer to a new instance
of someClass. The
pointer is then dereferenced and the someClass instance is passed by
reference to Foo. So, x then references this instance of someClass.
When Foo returns, x is deallocated, but the instance of someClass is
not.

The confusing part is that from what i've read, anything created with
'new' must be deallocated with 'delete'. But how can the instance of
someClass be deleted when it was never assigned to a 'pointer' that can
be deleted?


Do this:

int main()
{
someClass o;
Foo( o );
}

Forget about pointers and 'new' until you have mastered local objects
and standard library container classes.

yeah i agree, but this was just one of those problems that confused me
when i saw it and my curiosity won. Also, i'd never actually code like
this, it's just a curly one i want to understand.
 
B

Bo Persson

Oops. yeah i'll b more careful next time, notes noted.




So, you're saying it will be deleted after program execution?

No, but the OS will recover all the memory you program has been
allocating.
My understanding is that 'new' will return a pointer to a new
instance
of someClass. The
pointer is then dereferenced and the someClass instance is passed by
reference to Foo. So, x then references this instance of someClass.
When Foo returns, x is deallocated, but the instance of someClass is
not.
Correct.


The confusing part is that from what i've read, anything created
with
'new' must be deallocated with 'delete'.
Yes.

But how can the instance of
someClass be deleted when it was never assigned to a 'pointer' that
can be deleted?

That's the problem with the code. :)

In this case, the function Foo just might end with

delete &x; // NOT recommended

to recover the pointer, and delete the memory it points to. If you
pass Foo a reference to something that is not dynamically allocated,
this will absolutely not work. "Don't try this at home!".



Bo Persson
 
F

Frederick Gotham

Shane posted:
Is the memory allocated for the 'new someClass' that is passed to Foo,
automatically deleted, or must it be deleted using 'delete'? If so, how
given that the new someClass is never actually assigned to a pointer?


"new" is an operator. It takes an operand and evaluates to a value. Take
the "unary negation" operator for example; it takes a number as a operand
and evaluates to a value, e.g.:

-5

The operand is "5" and the entire expression evaluates to minus five.

The kind of operand that "new" wants is a type. It evaluates to a memory
address (i.e. the memory address of the allocated memory), e.g.:

new int

Dynamically allocated memory (e.g. memory allocated via "new", or via
"malloc") is under the programmer's sole control. The memory is only
deallocated when the programmer passes the memory address to "delete".

Take a look at the following code:

int main()
{
new int;
}

The memory address which "new int" evaluates to is discarded -- it isn't
saved or stored in any way. Therefore, we have no way to pass this memory
address to "delete", so we can't deallocate the memory. It's true that some
systems deallocate all of a program's lingering memory after the program
exits, but that's outside the scope of this newsgroup.

The kind of variable we use for storing a memory address is a pointer
variable. Furthermore, we pick a particular kind of pointer variable
depending on what kind of object resides at the memory address in question.
For our own sample, we do the following:

int main()
{
int *const p = new int;
}

Now we have stored the memory address, and can use it in the future to free
the memory. The storing of the memory address in a pointer variable has no
effect whatsoever on the workings of the "new" operator. All "new" does is
create an object and evaluate to a memory address. Here's a few different
ways of doing it:

int main()
{
int *const p1 = new int;
delete p1;

int &r1 = *new int;
delete &r1;


delete new int;
}
 
J

Jack Klein

No, but the OS will recover all the memory you program has been
allocating.

Can you cite a reference from the C++ standard that guarantees this?
What if the OP is not using "the OS", but some other OS? What is "the
OS", anyway?

Exactly what makes you think that the C++, or any other language
standard, for that matter, can impose requirements on any operating
system?

Or perhaps you have tried this on every single operating system which
can run C++ programs, so you can speak for all of them?
 
B

Bo Persson

Jack said:
Can you cite a reference from the C++ standard that guarantees this?

The point was actually that the system will not 'delete' the object (meaning
no destructor called) at the end of execution. That's guaranteed by the C++
standard.
What if the OP is not using "the OS", but some other OS? What is
"the OS", anyway?

"The OS" is any operating system run on the OP's machine.

If you go really basic, like a computerized toaster for example, you have to
define what "after program execution" means. If it means "pull the plug",
the memory resources will surely be reclaimed at the next restart.
Exactly what makes you think that the C++, or any other language
standard, for that matter, can impose requirements on any operating
system?

It can, in the sense that if the operating system doesn't supply the
resources needed, you cannot have a conforming C++ implementation on that
system.
Or perhaps you have tried this on every single operating system
which can run C++ programs, so you can speak for all of them?

Yes.


Bo Persson
 

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,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top