473,387 Members | 1,859 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.

why sizeof is different ??

Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don't understand ???

The idea is that I would like to calculate size of array.

Thanks in advance,
pujo

Nov 30 '05 #1
11 1438
aj****@gmail.com wrote:
Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don't understand ???

The idea is that I would like to calculate size of array.

Thanks in advance,
pujo


The problem is that your array in sizeArr() does not have a defined
limit -- in other words it's just a pointer. Try Boost.Typetraits'
extent:

http://boost.org/doc/html/boost_type...etraits.extent

Cheers! --M

Nov 30 '05 #2
aj****@gmail.com <aj****@gmail.com> wrote:
Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don't understand ???

The idea is that I would like to calculate size of array.


You cannot pass an array directly to a function, as the array will decay
into a pointer to the array's first element. Therefore, in your
sizeArr() function, it is really just seeing a pointer to float, so your
cout statement prints the size of a pointer to float. If you want the
function to know the size of the array, you must pass it in as an extra
parameter.

Also, if you want the number of elements instead of just the size, you
can do

sizeof(val) / sizeof(val[0]);

but remember that this only works in the scope that sees val as an array
instead of as a pointer.

Of course, it is preferred to use std::vector<> as it keeps track of its
size.

--
Marcus Kwok
Nov 30 '05 #3
aj****@gmail.com wrote:
Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don't understand ???
This is due to the fact that in function parameters, 'float a[]' is the
syntax for a pointer to float. So within the function, sizeof(a) will
return the size of a pointer to float.
This is a legacy from C, where it was considered convenient, but IMHO, it's
only confusing.

Btw: sizeof always returns a compile-time constant.
The idea is that I would like to calculate size of array.


You can't. Your function only gets a pointer to the array's first element,
which does not give you any way to find the size of the array. There are
several solutions.
You could do:

void sizeArr(float (&a)[3])

This passes an array reference to the function. You can use sizeof(a) to get
the array's size, but you can only pass arrays with exactly three elements
to the function.

An alternative solution is to simply pass the array's size as second
argument. Another way is to reserve a special element value as end marker.
The C style strings are handled this way, with '\0' being the end marker.
Or you could - instead of a raw array - use std::vector, which knows its
size.

Nov 30 '05 #4
aj****@gmail.com wrote:
Hello I have this code:

void sizeArr(float a[]){ float a[] has no bearing on the size of caller's parameter. cout << sizeof(a) << endl;
}
Array type parameters are handled very differently to regular types.
The array parameter is demoted to a pointer to the first element.

You can pass a size parameter - like so:

void sizeArr(float a[], unsigned nelements)
{
cout << sizeof(a[0])*nelements << endl;
}

Or you can use a template to do that if you wish.

template <typename T, unsigned N>
void sizeArr(float (&a)[N])
{
sizeArr(a,N);
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don't understand ???

The idea is that I would like to calculate size of array.

Thanks in advance,
pujo

Nov 30 '05 #5
Marcus Kwok wrote:
aj****@gmail.com <aj****@gmail.com> wrote:
Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don't understand ???

The idea is that I would like to calculate size of array.

You cannot pass an array directly to a function, as the array will decay
into a pointer to the array's first element. Therefore, in your
sizeArr() function, it is really just seeing a pointer to float, so your
cout statement prints the size of a pointer to float. If you want the
function to know the size of the array, you must pass it in as an extra
parameter.

Also, if you want the number of elements instead of just the size, you
can do

sizeof(val) / sizeof(val[0]);


For c++ code, I suggest you never do that as it can lead to problems.

Define a helper:
template <typename T, unsigned N>
char ( & ArrayExtent( T (&a)[N] ) )[N];

And then you can write:

sizeof(ArrayExtent(a))

This will only work for array types (which is good since this is what
you expect).

sizeof(val) / sizeof(val[0]) can give really bogus answers if val
happens to be a pointer or even an object that has an operator[].

but remember that this only works in the scope that sees val as an array
instead of as a pointer.

Of course, it is preferred to use std::vector<> as it keeps track of its
size.


std::vector is often better. Remember to pass them by reference (when
you intend to).

Nov 30 '05 #6
The problem I use vector is that it doesn't support explicit
initialization list as do arrays.

float a[] = {1.1 , 2.2, 3};

I can only do this things:
vector<float> v1;
v1.push_back(1.1);
v1.push_back(2.2);
v1.push_back(3);

or if I use:
float a[] = {1.1 , 2.2, 3};
vector<float> v1(a, a+3)

in this case I have to know 3 (the n element of a).

If a is big than I have a problem.

Any idea ?

pujo

Nov 30 '05 #7

aj****@gmail.com wrote:
The problem I use vector is that it doesn't support explicit
initialization list as do arrays.

float a[] = {1.1 , 2.2, 3};

I can only do this things:
vector<float> v1;
v1.push_back(1.1);
v1.push_back(2.2);
v1.push_back(3);

or if I use:
float a[] = {1.1 , 2.2, 3};
vector<float> v1(a, a+3)

in this case I have to know 3 (the n element of a).

If a is big than I have a problem.

Any idea ?


See this post for some ways to initialize vectors more like arrays:

http://groups.google.com/group/comp....fe5982913d4414

Cheers! --M

Nov 30 '05 #8
Gianni Mariani <gi*******@mariani.ws> wrote:
sizeof(val) / sizeof(val[0]) can give really bogus answers if val
happens to be a pointer or even an object that has an operator[].


Yes, very true in the general case, but I was restricting my answer to
an array of floats, in which case it should work.

--
Marcus Kwok
Nov 30 '05 #9
On 2005-11-30 09:57:49 -0500, "aj****@gmail.com" <aj****@gmail.com> said:
The problem I use vector is that it doesn't support explicit
initialization list as do arrays.

float a[] = {1.1 , 2.2, 3};

I can only do this things:
vector<float> v1;
v1.push_back(1.1);
v1.push_back(2.2);
v1.push_back(3);

or if I use:
float a[] = {1.1 , 2.2, 3};
vector<float> v1(a, a+3)

in this case I have to know 3 (the n element of a).

If a is big than I have a problem.

Any idea ?


template<typename T, unsigned N> T *array_begin(T (&arr)[N]) { return arr; }
template<typename T, unsigned N> T *array_end(T (&arr)[N]) { return arr + N; }

float a[] = {1.1 , 2.2, 3};
vector<float> v1(array_begin(a), array_end(a));
--
Clark S. Cox, III
cl*******@gmail.com

Nov 30 '05 #10
thanks...
that's great.

pujo

Nov 30 '05 #11
aj****@gmail.com wrote:
thanks...
that's great.

What is?
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 30 '05 #12

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

Similar topics

12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
43
by: ark | last post by:
Risking to invoke flames from one Tom St Denis of Ottawa :) Is there any guarantee that, say, sizeof(int) == sizeof(unsigned int) sizeof(long) > sizeof(char) ? Thanks, Ark
16
by: Martin Roos | last post by:
hy ppl, i'm trying to create some multiplatformed software here and i'm very curious about the sizes of common variables on different machines. if you are running something different than a 32-bit...
12
by: news.fe.internet.bosch.com | last post by:
Hi All , I u find out size of struct , does it considers paddding chars into consideration struct A { char c; int i; };
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
72
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
3
by: Andreas Eibach | last post by:
Hi, when I had in mind to turn this code (which worked fine in main()) to a separate function, I stumbled upon this...(of course, it's way bigger than that, but I hope this short snippet can...
8
by: c.lang.myself | last post by:
whenever we pass an array to function, we have to also pass its size.e.g if i am making a soritng function then void sort(int b,int size); Now I thought about using sizeof operator(macro?)...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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
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...

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.