473,472 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sizeof arrary

Please look at the code and run below. My question is why the compiler
cannot figure out the size of the array in function foo? Is this a bug
or expected?
Thanks a lot.
#include <iostream>

using namespace std;

int a[] = { 1, 2 };

void foo( int a[2] )
{

cout << sizeof( a ) << endl;
}

int main()
{
cout << sizeof( a ) << endl;
foo ( a );
}

[nan@xxx xxx]$ g++ test.cxx && ./a.out
8
4

Oct 3 '05 #1
3 1997
The prototype for the function foo actually takes a pointer to an int,
not an array. In the main routine you are getting the size of the
array, local variable 'a' but in the function foo you are actually
passing in a pointer. So when you get the sizeof 'a' in foo you are
getting the size of an int pointer.

David Barkol
www.neudesic.com

Oct 3 '05 #2
Nan Li wrote:
Please look at the code and run below. My question is why the compiler
cannot figure out the size of the array in function foo? Is this a bug
or expected?
It is expected. Passing arrays is a little strange in C and by default
in C++. This is one of the reasons that passing std::vector<> is
preferred since it is more intuitive.

In C (and C++), arrays get degraded to a pointer to the first element.
However in C++ you can pass by reference which can be used to find the
size of the array, in fact, using templates, you can write generic code
that will determine the array size for you.
Thanks a lot.
#include <iostream>

using namespace std;

int a[] = { 1, 2 };

void foo( int a[2] )
replace with this:

void foo( int (&a)[2] )
{

cout << sizeof( a ) << endl;
}

int main()
{
cout << sizeof( a ) << endl;
foo ( a );
}

[nan@xxx xxx]$ g++ test.cxx && ./a.out
8
4


If you want it to work for arrays of any size use:
template <unsigned N>
void foo( int (&a)[N] )
{
cout << sizeof( a ) << endl;
cout << N * sizeof( *a ) << endl;
}
Oct 3 '05 #3
Nan Li wrote:
Please look at the code and run below. My question is why the compiler
cannot figure out the size of the array in function foo?
Because you don't pass the array to foo. You actually pass a pointer to it's
first element.

void foo( int a[2] )

is 100% equivalent to:

void foo( int* a )

Therefore, sizeof(a) will give you the size of a pointer to int, not the
size of your array.

What you can do is make the function take a reference to an array:

void foo( int (&a)[2])
Is this a bug or expected?


It's how C++ is defined. So it's not a bug in the compiler, but you might
argue that it's a design flaw in C++ itself.

Oct 3 '05 #4

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...
21
by: sugaray | last post by:
hi, it just came up my mind that since we can get the length of any given string literal S with 'sizeof S-1', so, what's the merit of library function strlen()'s existence ? thanx in advance for...
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
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
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
0
by: alanwo | last post by:
Hi Experts, Interesting finding, when comparing two dictionary of byte(), KeyNotFoundException throwed but, that byte() key is present in another dictionary. Is that the limitation of comparison...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.