why sizeof is different ??

A

ajikoe

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
 
M

mlimber

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_typetraits/reference.html#boost_typetraits.extent

Cheers! --M
 
M

Marcus Kwok

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.
 
G

Gianni Mariani

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
 
R

Rolf Magnus

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.
 
G

Gianni Mariani

Marcus said:
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).
 
A

ajikoe

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
 
M

mlimber

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.lang.c++/msg/e2fe5982913d4414

Cheers! --M
 
M

Marcus Kwok

Gianni Mariani said:
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.
 
C

Clark S. Cox III

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));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top