Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl, Yandex, DotBot

Members: 0
Guests: 8
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
612
Jacki72H
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » General Discussions » Sizeof operator
Forum Rules | Mark all | Recent Posts

Sizeof operator
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Mar 7, 2006, 8:50 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Something I thought I'd share, since I didn't realize why it had been done this way until doing research into Remcon's BV posts. Take this code example:

int get_rflag( char *flag )
{
   unsigned int x;

   for( x = 0; x < ( sizeof( r_flags ) / sizeof( r_flags[0] ) ); x++ )
      if( !str_cmp( flag, r_flags[x] ) )
         return x;
   return -1;
}


I always wondered - why did they use sizeof in this manner? I never really understood it, but accepted it because it "just worked". Well after reading http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/expre_25.asp I finally get it.

The r_flags array is a group of text strings for room flags. This function goes through the array and matches up the argument against that array and returns a result based on that. So how did it know how many elements there were?

The sizeof(r_flags) part knows. That operation returns the number of elements in the array. The value it gets back is multiplied by the size of the datatypes used by the array, in this case char*, which are typically 4. It then needs to be divided by the sizeof(r_flags[0]), which is the size of the char* type, typically 4. Once it divides this out, it knows how many times to go over this loop.

All in all, it's actually quite clever and means the text arrays don't need a fixed size value. Adding an element is easy, and the get_rflag function doesn't need to know about it. I of course realize this may have been plainly obvious to some of you, so this post will hopefully explain it for those who were like me and didn't realize what was going on.
       
Post is unread #2 Mar 8, 2006, 3:21 pm
Go to the top of the page
Go to the bottom of the page

Conner
Sorcerer
GroupMembers
Posts870
JoinedMay 8, 2005

This wasn't obvious to me, thanks for the explaination! :)
       
Post is unread #3 Mar 8, 2006, 5:36 pm   Last edited Mar 8, 2006, 5:37 pm by GatewaySysop
Go to the top of the page
Go to the bottom of the page

GatewaySysop
Conjurer
GroupMembers
Posts367
JoinedMar 7, 2005

Samson said:

Something I thought I'd share, since I didn't realize why it had been done this way until doing research into Remcon's BV posts. Take this code example:

<snip!>

I always wondered - why did they use sizeof in this manner? I never really understood it, but accepted it because it "just worked". Well after reading http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/expre_25.asp I finally get it.

The r_flags array is a group of text strings for room flags. This function goes through the array and matches up the argument against that array and returns a result based on that. So how did it know how many elements there were?

The sizeof(r_flags) part knows. That operation returns the number of elements in the array. The value it gets back is multiplied by the size of the datatypes used by the array, in this case char*, which are typically 4. It then needs to be divided by the sizeof(r_flags[0]), which is the size of the char* type, typically 4. Once it divides this out, it knows how many times to go over this loop.

All in all, it's actually quite clever and means the text arrays don't need a fixed size value. Adding an element is easy, and the get_rflag function doesn't need to know about it. I of course realize this may have been plainly obvious to some of you, so this post will hopefully explain it for those who were like me and didn't realize what was going on.


That's pretty slick, actually. And no, it wasn't obvious to me either. Makes perfect sense in hindsight, (doesn't it all, at some point?) but somehow it wouldn't have clicked without the explanation.

Thanks for passing this along. :alien:

       
Post is unread #4 Mar 8, 2006, 6:16 pm
Go to the top of the page
Go to the bottom of the page

Halcyon
Magician
GroupMembers
Posts187
JoinedApr 12, 2005

I actually thought about that last night when it was mentioned that sizeof was returning the size of the datatype, and not the size of what was actually in it, and I too got to thinking about how those types of functions work. I have a macro that does that automatically, as well. After looking at it, I realized the same thing. :P

So yeah, it was plainly obvious to me, but only about as soon as it was to you. :D
       
Pages:<< prev 1 next >>