Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie Question: 'Sizeof' - I'm unclear what it actually measures,Modern equiv of older style reference manual?

Reply
Thread Tools

Newbie Question: 'Sizeof' - I'm unclear what it actually measures,Modern equiv of older style reference manual?

 
 
Laurel Shimer
Guest
Posts: n/a
 
      08-23-2010
Working to develop comfort with the language,I have been typing up
and running the code examples, and thinking through each line in each
example to make sure I know what each command is doing. Then I try to
make up my own very simple examples in the style of the ones in the
book.

(actually I have 3 books I go between, trying things out, and figuring
out my personal study plan. I tend to work within a particular book
for a couple of weeks. I return back once another author has gotten
me over a stumbling block - self-study ain't easy)

I'm currently working in the most basic of the 3 books ("Objective -C"
A Visual Quickstart Guide by Steve Holzner )
I'm using and I got stuck understanding how 'sizeof' works. (See also
my question below about my needing to figure out how to use
documentation better)

......
The author says that sizeof gets "the size of the whole array". I can
see from displaying each piece of the information that the author
uses, that the calculation using sizeof works, but I don't get what
'sizeof' is sizing? Is is the number of digits it allocates for each
instance in the array?

(the example in the book did not cast - is that the right way to say
it?- but I got compiler warnings and so I added the (long) part). Hope
I have gotten that idea right.

I added the second 2 printf statements. The author clearly thinks I
should be able to figure it out from the first one, which I haven't so
far.

............

#include <stdio.h>

int main()
{
int scores[5] = {92, 72, 57, 98, 89};

printf ("The array is %i elements long.\n", (long) sizeof(scores) /
(long) sizeof(int));

printf ("sizeof(scores) is %i \n", (long) sizeof(scores) );

printf ("(long) sizeof(int) is %i\n", (long) sizeof(int));

return 0;

}
Running…
The array is 5 elements long.
sizeof(scores) is 20
(long) sizeof(int) is 4

Debugger stopped.
Program exited with status value:0.

............. Second question Modern Equiv of older style reference
manual?........

ALSO this brings up one of my challenges. In my history I always had a
nice big fat reference manual where I could look up everything. (I
admit that even in unix, I like a reference manual - though I know how
to do man pages) My guess is that now, I'm supposed to be looking more
at online documentation or documentation within the application
(XCODE) that I'm using. I have a feeling I'm missing something big by
not quite knowing where to go.

I did find the folders with the foundation framework in XCODE, so
that when I go back to working on the OOP side (which I have
temporarily put on the back burner - since I realized I was fighting
my procedural programmer mindset), but that doesn't seem to be where I
find documentation on FUNCTIONS like ' sizeof'.

Thanks for steering my footsteps

Laurel
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      08-23-2010
[Please do not mail me a copy of your followup]

Sizeof measures the storage, in bytes, of its argument.

char c;
assert(sizeof(c) == 1);
int i;
assert(sizeof(i) == 4); // for 32-bit ints

--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
 
Reply With Quote
 
 
 
 
Laurel Shimer
Guest
Posts: n/a
 
      08-23-2010
On Aug 23, 3:23*pm, Laurel Shimer <laurelshi...@gmail.com> wrote:
> Working to develop comfort with the language,I have been *typing up
> and running the code examples, and thinking through each line in each
> example to make sure I know what each command is doing. Then I try to
> make up my own very simple examples in the style of the ones in the
> book.
>
> (actually I have 3 books I go between, trying things out, and figuring
> out my personal study plan. I tend to work within a particular book
> for a couple of weeks. I *return back once another author has gotten
> me over a stumbling block - self-study ain't easy)
>
> I'm currently working in the most basic of the 3 books ("Objective -C"
> A Visual Quickstart Guide by Steve Holzner )
> I'm using and I got stuck understanding how 'sizeof' works. (See also
> my question below about my needing to figure out how to use
> documentation better)
>
> .....
> The author says that sizeof gets "the size of the whole array". I can
> see from displaying each piece of the information that the author
> uses, that the calculation using sizeof works, but I don't get what
> 'sizeof' is sizing? Is is the number of digits it allocates for each
> instance in the array?
>
> (the example in the book did not cast - is that the right way to say
> it?- but I got compiler warnings and so I added the (long) part). Hope
> I have gotten that idea right.
>
> I added the second 2 printf statements. The author clearly thinks I
> should be able to figure it out from the first one, which I haven't so
> far.
>
> ...........
>
> #include <stdio.h>
>
> int main()
> {
> * * * * int scores[5] = {92, 72, 57, 98, 89};
>
> * * * * printf ("The array is %i elements long.\n", (long) sizeof(scores) /
> (long) sizeof(int));
>
> * * * * printf ("sizeof(scores) is %i \n", (long) sizeof(scores) *);
>
> * * * * printf ("(long) sizeof(int) is %i\n", *(long) sizeof(int));
>
> * * * * return 0;
>
> }
>
> Running…
> The array is 5 elements long.
> sizeof(scores) is 20
> (long) sizeof(int) is 4
>
> Debugger stopped.
> Program exited with status value:0.
>
> ............ Second question Modern Equiv of older style reference
> manual?........
>
> ALSO this brings up one of my challenges. In my history I always had a
> nice big fat reference manual where I could look up everything. (I
> admit that even in unix, I like a reference manual - though I know how
> to do man pages) My guess is that now, I'm supposed to be looking more
> at online documentation or documentation within the application
> (XCODE) that I'm using. I have a feeling I'm missing something big by
> not quite knowing where to go.
>
> *I did find the folders with the foundation framework in XCODE, so
> that when I go back to working on the OOP side (which I have
> temporarily put on the back burner - since I realized I was fighting
> my procedural programmer mindset), but that doesn't seem to be where I
> find documentation on FUNCTIONS like ' sizeof'.
>
> Thanks for steering my footsteps
>
> Laurel


Sorry for final edit errors. I can't find a way to cleanup my original
post so that it looks like I can actually speak and write in English.

 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      08-23-2010
"Laurel Shimer" <>

>where I find documentation on FUNCTIONS like ' sizeof'.


Did you consider STFW?

google's first hit on 'sizeof' looks damn promising to me...
 
Reply With Quote
 
Laurel Shimer
Guest
Posts: n/a
 
      08-23-2010
On Aug 23, 3:33*pm, "Balog Pal" <p...@lib.hu> wrote:
> "Laurel Shimer" <laurelshi...@gmail.com>
>
> >where I find documentation on FUNCTIONS like ' sizeof'.

>
> Did you consider STFW?
>
> google's first hit on 'sizeof' looks damn promising to me...



Thank you for your kind suggestion. Actually I did search the gosh-
darned web, though clearly I included too many arguments. The
wikipedia you so politely pointed out to me, is useful. I can see I
might have understood sizeoff better. I also understand it now that I
saw Richard's response. The lightbulb went on and I realized I was
actually measuring the size of the datatype 'int', which I hadn't sunk
in. Though it seems extremely clear now - funny how that's always the
case.

What I'm asking is, do I rely on Wikipedia to lookup a basic
explanation of a function in Objective-C? Or is there a reference
source that people who have been using the language longer than I
have, all know about? Something I've no doubt missed as I begin
studying a new language and a different programming environment.

Laurel
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-23-2010
On 08/24/10 10:23 AM, Laurel Shimer wrote:
>
> ALSO this brings up one of my challenges. In my history I always had a
> nice big fat reference manual where I could look up everything. (I
> admit that even in unix, I like a reference manual - though I know how
> to do man pages) My guess is that now, I'm supposed to be looking more
> at online documentation or documentation within the application
> (XCODE) that I'm using. I have a feeling I'm missing something big by
> not quite knowing where to go.


If you want a big fat comprehensive reference manual, get a copy of the
standard.

> I did find the folders with the foundation framework in XCODE, so
> that when I go back to working on the OOP side (which I have
> temporarily put on the back burner - since I realized I was fighting
> my procedural programmer mindset), but that doesn't seem to be where I
> find documentation on FUNCTIONS like ' sizeof'.


Another common newbie mistake - sizeof is *not* a function, it is an
operator.

http://www.cplusplus.com/reference/ is a reasonable on-line reference.

--
Ian Collins
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      08-23-2010
[Please do not mail me a copy of your followup]

Christian Hackl <> spake the secret code
<i4utnk$aq$> thusly:

>In C++, the correct way to create an array of 5 ints and print its size
>is as follows:


std::vector<T> is a dynamic array.

Fixed-size arrays are perfectly fine in C++ and the syntax of
aggregate initialization from C is also perfectly fine.

sizeof() is what you want to tell you the size of a fixed-size array,
i.e. sizeof(array_)/sizeof(array_[0])
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      08-23-2010
[Please do not mail me a copy of your followup]

Laurel Shimer <> spake the secret code
<45d72e9f-2142-47d7-99c5-> thusly:

>What I'm asking is, do I rely on Wikipedia to lookup a basic
>explanation of a function in Objective-C?


Objective C is neither C nor C++. It is its own dialect. Use Apple's
reference documentation to look up stuff on Objective C.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      08-23-2010
On Aug 23, 3:39*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 08/24/10 10:23 AM, Laurel Shimer wrote:
> > ALSO this brings up one of my challenges. In my history I always had a
> > nice big fat reference manual where I could look up everything. (I
> > admit that even in unix, I like a reference manual - though I know how
> > to do man pages) My guess is that now, I'm supposed to be looking more
> > at online documentation or documentation within the application
> > (XCODE) that I'm using. I have a feeling I'm missing something big by
> > not quite knowing where to go.

>
> If you want a big fat comprehensive reference manual, get a copy of the
> standard.


The standard is useful for some more arcane topics (sometimes, maybe),
but I strongly suggest not getting the standard as a beginner.
cplusplus.com works well for a standard library reference. There's
sgi's site too. As for a language reference, I'm not quite sure, but I
would think that there are more readable, understandable, and \maybe
even applicable\ documents out there than the official C++ standard.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-23-2010
On 08/24/10 11:02 AM, Joshua Maurice wrote:
> On Aug 23, 3:39 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>> On 08/24/10 10:23 AM, Laurel Shimer wrote:
>>> ALSO this brings up one of my challenges. In my history I always had a
>>> nice big fat reference manual where I could look up everything. (I
>>> admit that even in unix, I like a reference manual - though I know how
>>> to do man pages) My guess is that now, I'm supposed to be looking more
>>> at online documentation or documentation within the application
>>> (XCODE) that I'm using. I have a feeling I'm missing something big by
>>> not quite knowing where to go.

>>
>> If you want a big fat comprehensive reference manual, get a copy of the
>> standard.

>
> The standard is useful for some more arcane topics (sometimes, maybe),
> but I strongly suggest not getting the standard as a beginner.


Maybe, it is a reference, not a tutorial and a tutorial is more use to a
beginner.

> cplusplus.com works well for a standard library reference. There's
> sgi's site too. As for a language reference, I'm not quite sure, but I
> would think that there are more readable, understandable, and \maybe
> even applicable\ documents out there than the official C++ standard.


"The C++ Programming Language" is a good start for the language and an
introduction to the standard library. "The C++ Standard Library" by
Nicolai Josuttis is an invaluable guide the the Standard Library.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Still unclear Irfaan Wahid MCSD 3 02-24-2005 12:14 PM
Scope of news group? Charter unclear Shmuel (Seymour J.) Metz XML 1 07-09-2004 03:41 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Unclear on the concept... kj XML 2 04-12-2004 02:49 AM
Re: media player 9/windows xp, video&sound unclear Juan Pérez Computer Support 0 08-08-2003 01:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57