Sign in to follow this  
BaneTrapper

Reference& question

Recommended Posts

Hello.
I was insane addict user of pointer, but since i handle allot of them i want to use references since they are faster for me to type out.
When i put something like this
[CODE]
class 10MbClass
{
public:
//its stuff
};

10MbClass* 10MbClass::get_class(int IntForPointer)
{
return this;
}
10MbClass& 10MbClass::get_class(bool BoolForReference)
{
return *this;
}

int main()
{
10MbClass obj_10MbClass;

10MbClass* Pobj = obj_10MbClass.get_class(int);
10MbClass& Robj = obj_10MbClass.get_class(true);
}
[/CODE]

At end of code here, "10MbClass* Pobj" is 8bit in memory storage?
and "10MbClass& Robj" is how big? is reference taking same amount of space as pointer or? Edited by BaneTrapper

Share this post


Link to post
Share on other sites
[quote name='BaneTrapper' timestamp='1347815674' post='4980661']
At end of code here, "10MbClass* Pobj" is 8bit in memory storage?
and "10MbClass& Robj" is how big? is reference taking same amount of space as pointer or?
[/quote]
You probably meant 8 bytes.
And yes, internally references take the same amount of memory as pointers. Except of course in cases where compiler is able to optimize these out.

Share this post


Link to post
Share on other sites
The object Pobj is a pointer and is much larger than 8 bits. More likely 32 or 64 bits. The reference, for the purpose of this discussion, is more or less a pointer with different syntax; you can treat its size, overhead and benefits just like if it was a pointer.

Share this post


Link to post
Share on other sites
That was helpful.
Could not find out whats its size it, but i am satisfied if its not as creating another copy class.


Quote ^^
You probably meant 8 bytes.
I meant 8bits, which is not accurate as one may see. Edited by BaneTrapper

Share this post


Link to post
Share on other sites
[quote name='BaneTrapper' timestamp='1347824725' post='4980697']
[quote]You probably meant 8 bytes.[/quote]
I meant 8bits, which is not accurate as one may see.
[/quote]
For the record, pointers aren't 8 bits either. They are (probably) 8 [i]bytes[/i] (64 bits) on a 64 bit computer.
On my 32 bit Windows 7, they are 4 bytes (32 bits).

Get this: A char is 1 byte, but a pointer to char is 4 bytes... (on my machine).
[code]#include <iostream>
#include <limits.h>
int main()
{
std::cout << "sizeof(char) = " << sizeof(char) << " bytes, and " << (sizeof(char) * CHAR_BIT) << " bits.\n";
std::cout << "sizeof(char*) = " << sizeof(char*) << " bytes, and " << (sizeof(char*) * CHAR_BIT) << " bits.\n";

std::cout << "Note: In this situation, a 'byte' is considered " << CHAR_BIT << " bits." << std::endl;

return 0;
}[/code]

Outputs:
[html]sizeof(char) = 1 bytes, and 8 bits.
sizeof(char*) = 4 bytes, and 32 bits.
Note: In this situation, a 'byte' is considered 8 bits.[/html]

Try it [url="http://ideone.com/e4BzL"]online[/url], or execute the code yourself.

[url="http://en.wikipedia.org/wiki/Sizeof"]sizeof[/url]() returns the size of the variable or variable type, measured in whatever the number of bits in an unsigned char is, which is [i]usually [/i]8 bits, and garunteed to be [i]at least[/i] 8 bits. You can get the actual size of an unsigned char with CHAR_BIT (from [i]limits.h[/i]). But I just mostly assume they are 8 bits - you have to make those assumptions sometimes, or you go crazy. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] I bet you think a [url="http://www.cplusplus.com/reference/clibrary/ctime/tm/"]minute[/url] has 60 seconds in it, too. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img] Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1347833400' post='4980723']
I bet you think a [url="http://www.cplusplus.com/reference/clibrary/ctime/tm/"]minute[/url] has 60 seconds in it, too. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]
[/quote]
I will confirm that statement, depends the start point of second counting.
Else i am not scientist and approx value of 60 seconds in minute suffices for my needs.

And much thanks for explaining that sizeof function works i will do homework Edited by BaneTrapper

Share this post


Link to post
Share on other sites
[quote name='BaneTrapper' timestamp='1347889979' post='4980872']
[quote name='Servant of the Lord' timestamp='1347833400' post='4980723']
I bet you think a [url="http://www.cplusplus.com/reference/clibrary/ctime/tm/"]minute[/url] has 60 seconds in it, too. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]
[/quote]
I will confirm that statement, depends the start point of second counting.
Else i am not scientist and approx value of 60 seconds in minute suffices for my needs.

And much thanks for explaining that sizeof function works i will do homework
[/quote]
It's not about being scientific and that a minute has approximately 60 seconds, or that it depends on how you count. A scientific minute does have exactly 60 seconds by definition. But the problem here is the effect of having a time that is bound to the rotation of the earth, like GMT or UTC. Leap seconds are added or removed from the time to keep the clock in sync with the rotation of the earth to ensure that the length a day is 24 hours +/- 0.9 seconds. On some days, the very last minute of the very last hour of that day has either 61 or 59 seconds, not 60 as a normal minute. If you have a time-stamp critical application, you may be in trouble when some systems are suddenly off by a second and you have times stamps that are not in sync anymore.

There are other time systems that don't have leap seconds, in which a minute is always 60 seconds, for example atomic time. At the introduction of atomic time, it was set to GMT. The difference is that atomic time ticks according to the definition of a second, with 60 seconds a minute and so on, but without leap seconds. Today, the atomic time is 30 or so seconds ahead of GMT due to added leap seconds.

Is this necessary to know? In everyday life, probably not. You're most likely fine assuming a minute always has 60 seconds. Just like you can assume that in your everyday development you can assume that a byte has eight bits. Some platforms, just like some application are time-stamp critical, your assumptions may be wrong and a byte isn't eight bits anymore. There are often more to it to trivial and well known facts, and it is not always as easy and trivial as it may sound :)

Share this post


Link to post
Share on other sites
PUFfff.
My brain just blew up...
i had no idea that 0.9sec is skipped each day :D

I like this education.

[quote]
you can assume that a byte has eight bits
byte isn't eight bits anymore
[/quote]
I understand the point you are making, using bytes and bits comparison was not the best idea,
at least in my knowledge since , byte = 8 bits. byte does not exist as far as i know, we only use it to reference 8 bits
as we use minute to reference 60 seconds etc...

Share this post


Link to post
Share on other sites
[quote name='BaneTrapper' timestamp='1347902653' post='4980954']
PUFfff.
My brain just blew up...
i had no idea that 0.9sec is skipped each day [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

I like this education.
[/quote]
No, we don't skip 0.9 seconds every day. The length of a day is varying on the order of milliseconds, and a leap second is introduced once these milliseconds have accumulated to more than 0.9 seconds. A leap second is currently introduced roughly once every three years or so, but it varies. So it's just a second on a relatively large time scale.

This relatively small adjustment over a long period is what can make it difficult; it doesn't happen often so you can practically try and observe it as it happens. But it does happen, and if you depend on it you have to make sure that you can handle it.
[quote name='BaneTrapper' timestamp='1347902653' post='4980954']
[quote]
you can assume that a byte has eight bits
byte isn't eight bits anymore
[/quote]
I understand the point you are making, using bytes and bits comparison was not the best idea,
at least in my knowledge since , byte = 8 bits. byte does not exist as far as i know, we only use it to reference 8 bits
as we use minute to reference 60 seconds etc...
[/quote]
The time and the byte comparison are on different scales practically, yes, but sort of demonstrates the same idea about assumptions. The definition of a byte depends on context, and within the context of C and C++ it is the smallest unit of storage which is the storage of the type [i]char[/i] and its variants. If a [i]char[/i] is 32 bits, then so is a byte in that particular context. If you are explicitly talking about groups of 8-bit of information, then the term [i]octet[/i] is more accurate and less ambiguous.

But as I said, it is most likely not necessary for you to know this; especially about the time, but you should at least be aware of the byte issue even if you choose to ignore it for the moment.

Personally, I am at trying to not depend on these kind of assumptions as far as practically reasonable. I don't write my code to be aware of leap seconds (I just assume they don't exist; they simply don't affect anything I do), but I try my best to not assume that a byte is eight bits, and if I do assume that, I will also isolate that code as much as possible. But then again, I am more picky on these kind of details than most are ;)

Share this post


Link to post
Share on other sites
Wise words,
I will probably learn it by hard way, doing the mistake.
[quote]quote[/quote]
Being ignorant i am not.
I am being smart and trying to leach information as much as i can ^^, easy way to learn the good stuff that people tend to hide.
Reading books, looking on forums will not lead you to such good intelligence

Anyway STOP POSTING. CLOSE DIS PAGE!
CLOSED CLOSED CLOSED CLOSED! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by BaneTrapper

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this