Board index » cppbuilder » Reference or Pointer ?

Reference or Pointer ?


2005-04-19 12:02:46 PM
cppbuilder93
Hi,
I'm wondering what's the difference in the computer's memory between a
reference and a pointer to an object like the following declarations:
TMyClass MyClass();
MyClass.Foo();
// and
TMyClass * MyClass = new TMyClass();
MyClass->Foo();
Is one of the two techniques takes more or less memory, takes more or less
processor's cycles when used? What's the big picture between those two
techniques?
Thanks.
--
/* Stéphane Lambert */
 
 

Re:Reference or Pointer ?

Stéphane Lambert wrote:
Quote
Hi,

I'm wondering what's the difference in the computer's memory between a
reference and a pointer to an object like the following declarations:
There is no difference.

TMyClass MyClass();
MyClass.Foo();
This is NOT a reference, its a normal variable
TMyClass MyClass;
TMyClass &ref = MyClass; // This is a reference
ref.Foo();
Quote
// and

TMyClass * MyClass = new TMyClass();
MyClass->Foo();

Is one of the two techniques takes more or less memory, takes more or less
processor's cycles when used? What's the big picture between those two
techniques?
A reference has some conceptual differences:
A reference must be initialized (a pointer can be uninitialized)
unfortunatly its still possible for the referenced object to go out of
scope before the reference.
A reference cannot be NULL.
A reference cannot be changed, it allways points to the same object.
Ofcourse the contents can be changed.
Eelke
 

Re:Reference or Pointer ?

JD < XXXX@XXXXX.COM >wrote:
Quote
"Stéphane Lambert" < XXXX@XXXXX.COM >wrote:
>
>[...] what's the difference in the computer's memory [...]

One method allocates from the stack while the other (new)
allocates from the heap. [...]
You can store references to objects
in pointers or references independent
of how they were allocated. (Not that
every possible combination would result
in good programming practice...)
Quote
~ JD
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
 

{smallsort}

Re:Reference or Pointer ?

"Stéphane Lambert" < XXXX@XXXXX.COM >wrote:
Quote

[...] what's the difference in the computer's memory [...]
One method allocates from the stack while the other (new)
allocates from the heap. The only real difference in using
one method v/s the other has to do with how objects get
destroyed.
When an object is allocated from the stack, it's guaranteed to
be destroyed because of how the stack gets unwound. If an object
is allocated using 'new' (from the heap), you must explicitly
destroy the object using 'delete' and if an exception is thrown,
the stack gets unwound and execution passes to the closest
catch statement which can cause your delete statement to
remain unexecuted - causing a leak.
If you're wondering why use the heap at all ... it's because
the stack is very limited in size while the heap is virtually
unlimited. Smart pointers such as auto_ptr combine the safety
of using the stack with the flexibility of using the heap by
allocating a small class from the stack that takes ownership
of the memory allocated from the heap. When the stack gets
unwound, the smart pointers destructor is automatically
executed which deallocates the memory from the heap.
~ JD
 

Re:Reference or Pointer ?

"Stéphane Lambert" wrote:
Quote
I'm wondering what's the difference in the computer's memory between a
reference and a pointer to an object like the following declarations:

TMyClass MyClass();
This is created on the stack (internal memory of the program, limited,
do not use for creating classes that need a lot of memory, advantage:
the garbage collector will release the memory automatically when you
program leaves the segment where this variable is declared).
Quote
TMyClass * MyClass = new TMyClass();
MyClass->Foo();
This is created on the heap. There virtually no limit (except the
memory of your system). Disadvantage: You have to release the memory
with "delete MyClass;" when it is no more needed.
Greetings, Gérôme
 

Re:Reference or Pointer ?

Stiphane Lambert wrote:
Quote
Hi,

I'm wondering what's the difference in the computer's memory between a
reference and a pointer to an object like the following declarations:

TMyClass MyClass();
MyClass.Foo();

// and

TMyClass * MyClass = new TMyClass();
MyClass->Foo();

Is one of the two techniques takes more or less memory, takes more or
less processor's cycles when used? What's the big picture between
those two techniques?

Thanks.
Any difference in "computers memory" between the two usages above is
compiler dependent. In practice, there is nothing to stop the compiler
(behind the scenes) implementing the first approach in terms of the
second. Similarly comments are true when discussing execution speed,
machine instructions, or "processor cycles".
 

Re:Reference or Pointer ?

"Stéphane Lambert" < XXXX@XXXXX.COM >writes:
Quote
I'm wondering what's the difference in the computer's memory between a
reference and a pointer to an object like the following declarations:

TMyClass MyClass();
This is the declaration of a function named MyClass that returns a
TMyClass object. It doesn't produce any executable code.
Quote
MyClass.Foo();
This shouldn't compile.
Did you mean to compare
TMyClass().Foo();
and ...
Quote
TMyClass * MyClass = new TMyClass();
MyClass->Foo();
...
std::auto_ptr<TMyClass>(new TMyClass)->Foo();
If yes, then the second version is normally just unnecessary
complexity; for the human reader, for the compiler and runtime-wise.
"Normally" because it is possible to write TMyClass in a way that only
allows one of the two ways. E.g. for all classes derived from the
VCL's TObject, only the second way works.
 

Re:Reference or Pointer ?

"Stéphane Lambert" < XXXX@XXXXX.COM >wrote in message
Quote
I'm wondering what's the difference in the computer's memory
between a reference and a pointer to an object like the following
declarations:
You are not using references in your code example. But there is a
difference. In the first case, the object is created on the stack. In the
second case, it is created on the heap instead.
Gambit
 

Re:Reference or Pointer ?

"Stéphane Lambert" < XXXX@XXXXX.COM >writes:
Quote
Hi,

I'm wondering what's the difference in the computer's memory between a
reference and a pointer to an object like the following declarations:
The biggest difference is at the C++ level, in the way the compiler
enforces the rules of C++. References and pointers are different
things in the language, however the generated code is quite likely the
same.
--
Chris (TeamB);
 

Re:Reference or Pointer ?

"JD" < XXXX@XXXXX.COM >writes:
Quote
"Stéphane Lambert" < XXXX@XXXXX.COM >wrote:
>
>[...] what's the difference in the computer's memory [...]

One method allocates from the stack while the other (new)
allocates from the heap. The only real difference in using
one method v/s the other has to do with how objects get
destroyed.
This is memory management, which is unrelated to pointer vs. reference
issues.
int main()
{
int stack;
int * ptr_to_heap = new int; // pointer to something on the heap
int * ptr_to_stack = &stack; // pointer to something on the stack
int & ref_to_stack = stack; // reference to something on the stack
int & ref_to_heap = *heap; // reference to something from the heap
}
--
Chris (TeamB);
 

Re:Reference or Pointer ?

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote

This is memory management, which is unrelated to pointer vs.
reference issues. [...]
Perhaps, but not as I understood his post and I wasn't the
only one to infer it's relevance. Strange that you would
feel compelled to point this out to me. Need I point out
that he asked "What's the big picture between those two
techniques?"?
~ JD
 

Re:Reference or Pointer ?

Hi,
First I want to thank all of you for your help and knowledge in this very
interesting discussion. I really appreciate.
Now that I know that the main reason is heap vs. stack and the
implementation depends on the compiler, here's why I asked this question.
Every time I create a class (sometimes with static members) and I already
know that I will need to use only one copy (or instance) of that class
throughout the whole program, I'm always not sure if I should use a pointer
to that class or use a simple declaration (as I showed in my original post).
: TMyClass MyClass();
: MyClass.Foo();
:
: // and
:
: TMyClass * MyClass = new TMyClass();
: MyClass->Foo();
Is one way is a better programming practice then the other? Or is a "real"
reference declaration could be better? Like:
TMyClass & MyClass = TMyClass();
MyClass.Foo();
My goal is to understand what I write and writing good code. Since I learned
programming by myself reading books, I apologize if my way of seeing things
right now is non-standard. That's what I try to find out.
--
/* Stéphane Lambert */
 

Re:Reference or Pointer ?

"Stéphane Lambert" < XXXX@XXXXX.COM >writes:
Quote
Every time I create a class (sometimes with static members) and I already
know that I will need to use only one copy (or instance) of that class
throughout the whole program, I'm always not sure if I should use a pointer
to that class or use a simple declaration (as I showed in my original post).

: TMyClass MyClass();
: MyClass.Foo();
Why do you keep posting this completely wrong code?
Quote
:
: // and
:
: TMyClass * MyClass = new TMyClass();
: MyClass->Foo();

Is one way is a better programming practice then the other? Or is a "real"
reference declaration could be better? Like:

TMyClass & MyClass = TMyClass();
MyClass.Foo();
Sigh, I'm repeating my self here ...
Unless there are any boundary conditions indicating otherwise, use the
simplest way. Which is none of the above, but
TMyClass().Foo();
or, if you want,
TMyClass myObject;
myObject.Foo();
 

Re:Reference or Pointer ?

Hi Thomas, thanks for you help.
:>: TMyClass MyClass();
:>: MyClass.Foo();
:
: Why do you keep posting this completely wrong code?
Sorry, you're right, I forgot to make the correction. But this is a thing I
don't understand, sorry for my ignorance. Why the following declaration for
a class that need to initialize a member within it's constructor is legal:
TMyClass MyClass( "John Smith" );
MyClass.getName();
But, it's not legal to use the following declaration for a class with a
constructor that don't have to initialize any class member:
TMyClass MyClass() // <- illegal.
MyClass.setName( "John Smith" );
Thanks.
--
/* Stéphane Lambert */
 

Re:Reference or Pointer ?

TMyClass MyClass();
This is the declaration of a function called MyClass which takes no
parameters and returns a TMyClass object.
Eelke