0

Here in this program I am using the volatile register object, Am I actually storing my object to a register here?
Why am I getting the address of object as 1? Please share you thought on this.

#include <iostream>
using namespace std;


class a{
    int i,j,k[999];
    long double  arr[9999999];
    public:
        a(){
            i=77; j=89;
            cout<<"\nctor\n";
        }
        void disp()volatile {
            cout<<"\ni = "<<i<<" j = "<<j<<"\n";
        //  delete this;
        }
        ~a(){
            cout<<"\ndtor\n";
        }
};


int main(){
    register volatile a *ao = new a;
    cout<<"address of a = "<<ao; //out puts "1" for me; (My processor is core i3 330M).
    ao->disp();
     delete ao;
}
Renuka
  • 217
  • 1
  • 6
  • 3
    What would call the destructor without a delete? – László Papp Apr 06 '14 at 16:39
  • 1
    C++ is not Java. You need to call "delete" if you allocated with "new". – PaulMcKenzie Apr 06 '14 at 16:39
  • 1
    @PaulMcKenzie: unless you use a smart pointer... – László Papp Apr 06 '14 at 16:41
  • Yes, but at some point, even the smart pointer has to call "delete" (internally of course). – PaulMcKenzie Apr 06 '14 at 16:42
  • @Renuka: Please mark all updates to the question (especially code) clearly. Avoids much confusion when reading the comments. Anyway, not changing the code under discussion is preferred. – Deduplicator Apr 06 '14 at 16:45
  • @Renuka: It sounds like your real question is this, https://gist.github.com/sharth/ea59c1974605c468399a is that correct? – Bill Lynch Apr 06 '14 at 16:46
  • @PaulMcKenzie: yes, internally, sure. – László Papp Apr 06 '14 at 16:50
  • 2
    @Renuka: It's generally not good etiquette to edit your question and completely change it to a new one. If you have a new question, please create a new post. Otherwise, none of the answers that people spent their time writing make sense any more (you can also see evidence of the confusion in the comments below). – Jason C Apr 06 '14 at 16:57
  • @Renuka: the 1 mystery is now also answered, basically from sharth's finding, thanks to him or that, but yes, do not introduce new questions next time. The question is still not duplicate though as it is also asking about delete, however. – László Papp Apr 06 '14 at 16:59

3 Answers3

4

This is obvious because nothing calls delete which means you get a memory leak. You will need to call delete explicitly to avoid that, or even better: use a smart pointer.

Moreover, the register will be ignored in C++. It is basically deprecated. You can read it from the C++ standard:

A register specifier has the same semantics as an auto specifier together with a hint to the implementation that the object so declared will be heavily used. [Note: the hint can be ignored and in most implementations it will be ignored if the address of the object is taken. —end note]

Thereby, the register usage is red herring here.

The 1 printed is because of the following reason:

"When you pass in a volatile pointer, the second overload can't apply because volatile pointers cannot be converted to non-volatile without an explicit cast. However, any pointer can be converted to bool, so the first overload is chosen, and the result you see is 1 or 0."

... from here.

Thereby, if you remove the volatile, it will not print 1 anymore.

Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
1

You are constructing the object in dynamic memory. You will need to manually clean this up by calling "delete ao". This wont be done automatically for you. If somehow ao goes ouut of scope and you have no access to the pointer to this object then you have what is called a "memory leak which is a major pain point for many C++ programmers.

register keyword has been deprecatred in C++ 11 and is not required either. If you are using a compiler confirming to the older standards register storage is just a request to the compiler and the compiler is free to ignore it especially when you require the address of the object.

MS Srikkanth
  • 3,829
  • 3
  • 19
  • 33
1

My thought: Here you might be trying to deny the optimization of compiler for the pointer object ao by using the volatile keyword, Am I right? But this is the only reason why you got '1'! The C++ standard says, even if you specify an object or variable as register it will internally consider only as an auto. For the detaild info about the out put 1 see this link

Community
  • 1
  • 1
uss
  • 1,271
  • 1
  • 13
  • 29