473,387 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sizeof observation

Hello,

I just thought I would share the following observation with the rest
of the group. The sizeof operator seems to act differently according
to whether the number of elements in the array is known. Hence when
passing arrays to functions the number of elements in the array must
always be passed as an argument. If STL is available then people can
just use vectors of course. Anyways, I guess this stuff is pretty
standard. Well, have a nice day,

Neil

#include <cstdio>

void hello(const char *foo[]) {
printf("%u\n", sizeof(foo)/sizeof(char));
}

int main() {
const char *foo[8];
const char *bar[] = { "aa", "bb" };
printf("%u\n", sizeof(foo)/sizeof(char));
//BTW sizeof(char) is always going to be one
printf("%u\n", sizeof(bar)/sizeof(char));
//BTW sizeof(char) is always going to be one
hello(foo);
}

Output:

$ ./a.out
32
8
4

Aug 2 '05 #1
7 4614
nz******@cs.mun.ca wrote:
I just thought I would share the following observation with the rest
of the group. The sizeof operator seems to act differently according
to whether the number of elements in the array is known.
No, it "acts" differently when you supply different types to it.
Hence when
passing arrays to functions the number of elements in the array must
always be passed as an argument. If STL is available then people can
just use vectors of course. Anyways, I guess this stuff is pretty
standard.
Yes. And pretty basic too.
Well, have a nice day,
Thanks. Same to you.

Neil

#include <cstdio>

void hello(const char *foo[]) {
In this function 'foo' is not an array. It's a pointer. The [] notation
is a left-over from C days (like the "native" arrays, of course), and it
simply declares the argument as a pointer. If you want to pass an array,
you can, by reference. You should do

void hello(const char *(&foo)[8])

In that case, 'foo' is a reference to an array of eight pointers to const
char. And, just like with any other reference, if you use the 'sizeof'
operator on it, you will get the size of the object it refers to.
printf("%u\n", sizeof(foo)/sizeof(char));
BTW, 'sizeof(char)' is _always_ 1. Dividing by it makes no sense. You
probably wanted to divide by 'sizeof(const char*)' which would give you
the _number_ of elements in the array.
}

int main() {
const char *foo[8];
const char *bar[] = { "aa", "bb" };
printf("%u\n", sizeof(foo)/sizeof(char));
//BTW sizeof(char) is always going to be one
Yes, by definition of 'sizeof'. There is no sense in dividing by 1, is
there?
printf("%u\n", sizeof(bar)/sizeof(char));
//BTW sizeof(char) is always going to be one
hello(foo);
}

Output:

$ ./a.out
32
8
4

V
Aug 2 '05 #2

nz******@cs.mun.ca wrote:
Hello,

I just thought I would share the following observation with the rest
of the group. The sizeof operator seems to act differently according
to whether the number of elements in the array is known. Hence when
passing arrays to functions the number of elements in the array must
always be passed as an argument. If STL is available then people can
just use vectors of course. Anyways, I guess this stuff is pretty
standard. Well, have a nice day,

Neil

#include <cstdio>

void hello(const char *foo[]) {
printf("%u\n", sizeof(foo)/sizeof(char));
}

int main() {
const char *foo[8];
const char *bar[] = { "aa", "bb" };
printf("%u\n", sizeof(foo)/sizeof(char));
//BTW sizeof(char) is always going to be one
printf("%u\n", sizeof(bar)/sizeof(char));
//BTW sizeof(char) is always going to be one
hello(foo);
}

Output:

$ ./a.out
32
8
4


Instead of the sizeof idiom for determining the number of elements in
an array, you might want to consider using this function instead:

template<typename T, std::size_t N>
inline std::size_t countof( T (&)[N] ) { return N; }

this function won't compile if you pass a pointer to it (calling
countof(foo) inside hello will error) and you can't make mistakes such
as dividing by sizeof(char) instead of by sizeof(const char*) like you
should be doing.

btw, if you're including <cstdio> (why not <iostream>?) it is
std::printf, etc. all standard identifiers are in the std namespace.

Aug 2 '05 #3
Victor Bazarov wrote:
....
printf("%u\n", sizeof(foo)/sizeof(char));


BTW, 'sizeof(char)' is _always_ 1. Dividing by it makes no sense. You
probably wanted to divide by 'sizeof(const char*)' which would give you
the _number_ of elements in the array.

....

This may not be true if you're trying to write code that is valid as
either C or C++ code. C originally defined sizeof to return the
number of bytes of storage needed, and a byte and a char were not
necessarily the same thing. Architectures with instructions that
operated on units of memory whose size wasn't a power of two
disappeared from widespread use around 1980, and C++ assumed the dead
would not rise again.

Aug 2 '05 #4
wk****@yahoo.com wrote:
Victor Bazarov wrote:
...
printf("%u\n", sizeof(foo)/sizeof(char));
BTW, 'sizeof(char)' is _always_ 1. Dividing by it makes no sense. You
probably wanted to divide by 'sizeof(const char*)' which would give you
the _number_ of elements in the array.


...

This may not be true if you're trying to write code that is valid as
either C or C++ code.


That's nonsense. It's true for both C and C++.
C originally defined sizeof to return the
number of bytes of storage needed, and a byte and a char were not
necessarily the same thing.
Care to give a quote from some document which would define that?
Architectures with instructions that
operated on units of memory whose size wasn't a power of two
disappeared from widespread use around 1980, and C++ assumed the dead
would not rise again.


C defines 'byte' to be "addressable unit of data storage large enough to
hold any member of the basic character set of the execution environment".
And also in 6.5.3.4, it says "When applied to an operand that has type
char, unsigned char, or signed char, (or a qualified version thereof) the
result is 1." Where you see a conflict with some [unusual or now dead]
architectures, I am not sure.

V
Aug 2 '05 #5
Victor Bazarov wrote:
wk****@yahoo.com wrote:
Victor Bazarov wrote:
...
printf("%u\n", sizeof(foo)/sizeof(char));

BTW, 'sizeof(char)' is _always_ 1. Dividing by it makes no sense. You
probably wanted to divide by 'sizeof(const char*)' which would give you
the _number_ of elements in the array.


...

This may not be true if you're trying to write code that is valid as
either C or C++ code.


That's nonsense. It's true for both C and C++.


I'll take your word for it that sizeof(char) == 1 has made it
into the C Standard now as well. But I still see alot of
"#ifdef ANSI_C" as I'm browsing through C code. C is all
about anachronism these days, which could make for a big lag
between compiler versions in wide usage and the current
C Standard.
> C originally defined sizeof to return the
number of bytes of storage needed, and a byte and a char were not
necessarily the same thing.


Care to give a quote from some document which would define that?


Nah, too lazy to try.
> Architectures with instructions that
operated on units of memory whose size wasn't a power of two
disappeared from widespread use around 1980, and C++ assumed the dead
would not rise again.


C defines 'byte' to be "addressable unit of data storage large enough to
hold any member of the basic character set of the execution environment".
And also in 6.5.3.4, it says "When applied to an operand that has type
char, unsigned char, or signed char, (or a qualified version thereof) the
result is 1." Where you see a conflict with some [unusual or now dead]
architectures, I am not sure.


The DEC/PDP 10 had word size of 36 bits. The size of a character could
be set (by loading a special register) to 6, 7, 8 or 9 bits. I never
saw a C implementation for the DEC 10. If char was defined to be
8 bits, then it would probably make sense to define a byte to be 4 bits
rather than 8 (unless sizeof was redefined to return a float).

Aug 2 '05 #6
wk****@yahoo.com wrote:
Victor Bazarov wrote:
That's nonsense. It's true for both C and C++.


I'll take your word for it that sizeof(char) == 1 has made it
into the C Standard now as well.


If made it into there years before C++ even had a standard.
But I still see alot of
"#ifdef ANSI_C" as I'm browsing through C code. C is all
about anachronism these days, which could make for a big lag
between compiler versions in wide usage and the current
C Standard.
What nonsense is this? There's still some old pre-standard C code
around, yeah, but the same is true (probably more so) for C++. C has
been standardized several years longer.
The DEC/PDP 10 had word size of 36 bits. The size of a character
could be set (by loading a special register) to 6, 7, 8 or 9 bits. I
never saw a C implementation for the DEC 10. If char was defined to
be 8 bits, then it would probably make sense to define a byte to be 4
bits rather than 8 (unless sizeof was redefined to return a float).


There's no requirement for char to be eight bits, in either language.
It must be at least eight.

Brian
Aug 2 '05 #7
Default User wrote:
wk****@yahoo.com wrote:
Victor Bazarov wrote:

That's nonsense. It's true for both C and C++.


I'll take your word for it that sizeof(char) == 1 has made it
into the C Standard now as well.


If made it into there years before C++ even had a standard.


Looks like you're right about that, it was in the first ANSI
C Standard. Anyone still got a first ed. of K&R? Be interested
to hear what it says about this issue.
But I still see alot of
"#ifdef ANSI_C" as I'm browsing through C code. C is all
about anachronism these days, which could make for a big lag
between compiler versions in wide usage and the current
C Standard.


What nonsense is this? There's still some old pre-standard C code
around, yeah, but the same is true (probably more so) for C++. C has
been standardized several years longer.


There's a high probability that no C/C++ compiler in use now (or that
was ever used) has sizeof(char) != 1 . But in general, for both C
and C++, if you really want to maximize the portability of your
source code (even for those nonsensical and morally inferior people
who use a compiler that isn't 100% Standard compliant), it's more
complex than just coding to the standard.
The DEC/PDP 10 had word size of 36 bits. The size of a character
could be set (by loading a special register) to 6, 7, 8 or 9 bits. I
never saw a C implementation for the DEC 10. If char was defined to
be 8 bits, then it would probably make sense to define a byte to be 4
bits rather than 8 (unless sizeof was redefined to return a float).


There's no requirement for char to be eight bits, in either language.
It must be at least eight.


Yeah just make it 9 bits and waste a bit. I guess I just need to get
over the traumas of having learned to program on a machine that used
magnetic core memory.

Aug 3 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
7
by: Kieran Simkin | last post by:
I have in my project in main.c a number of arrays defined like this (outside of any functions): char muser, mpass; I'm declaring them in a number of other .c files (inside functions) like this:...
21
by: raghu | last post by:
i'm surprised at the output of the following code. compiled in turbo C void main() { printf("%d",sizeof(printf()); } the output was : 2 how come the output is 2? actually what is the property...
29
by: lnitsu | last post by:
Hello I've got a simple question regarding sizeof operator. why sizeof 'a' return 4? (Note the argument is without parenthesis) If anybody is so kind to answer ... TIA
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
58
by: Nishu | last post by:
Hi All, When I run the below program in MSVC, I get the output as 1 4 Could you tell me why sizeof 'A' is taken as 4? Is it standard defined or compiler specific? Thanks, Nishu...
2
by: Tom | last post by:
My older system: Win2k, VS2005(Academic), .Net 2.0 SP1. Windows.Forms Application: Two splitter panels, a TreeView (named: "tree") in one panel populated with directory name nodes. Logic for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.