473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing dereferenced new pointer to reference paramter

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

Oct 28 '06 #1
7 1742
* sh*****@gmail.com:
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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 28 '06 #2
Alf P. Steinbach wrote:
* sh*****@gmail.com:
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.
Oops. yeah i'll b more careful next time, notes noted.

>
{
Foo(*(new someClass));
}
Is the memory allocated for the 'new someClass' that is passed to Foo,
automatically deleted

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?
>
, 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.
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.
>
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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 28 '06 #3
sh*****@gmail.com wrote:
Alf P. Steinbach wrote:
>* sh*****@gmail.com:
>>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.

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

>>
>>{
Foo(*(new someClass));
}
Is the memory allocated for the 'new someClass' that is passed to
Foo, automatically deleted

Not during program execution.


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
Oct 28 '06 #4
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;
}

--

Frederick Gotham
Oct 28 '06 #5
Thanks for your help everyone. It makes sense now :)

Oct 28 '06 #6
On Sat, 28 Oct 2006 12:08:08 +0200, "Bo Persson" <bo*@gmb.dkwrote in
comp.lang.c++:
sh*****@gmail.com wrote:
Alf P. Steinbach wrote:
* sh*****@gmail.com:
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.
Oops. yeah i'll b more careful next time, notes noted.

>
{
Foo(*(new someClass));
}
Is the memory allocated for the 'new someClass' that is passed to
Foo, automatically deleted

Not during program execution.

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 30 '06 #7
Jack Klein wrote:
On Sat, 28 Oct 2006 12:08:08 +0200, "Bo Persson" <bo*@gmb.dkwrote
in comp.lang.c++:
>sh*****@gmail.com wrote:
>>Alf P. Steinbach wrote:
* sh*****@gmail.com:

{
Foo(*(new someClass));
}
>
>
Is the memory allocated for the 'new someClass' that is passed
to Foo, automatically deleted

Not during program execution.
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.

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
Oct 30 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
20
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
5
by: Matthew Thompson | last post by:
I have as issue I am finding hard to research. I use a stored proecdure in SQL 2000 to provide search capability for our database of news stories and articles. Being an international magazine...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
8
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.