Size of integer array

D

desktop

I have this code:

void test(int* array)
{
int pp = sizeof(array);

cout << pp << endl;

}

int main(){

int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;

}

In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).

But when I pass nums to test() and call sizeof again I only get 4.

Why do I get 12 in main and 4 in test()?
 
I

Ian Collins

desktop said:
I have this code:

void test(int* array)
{
int pp = sizeof(array);

cout << pp << endl;

}

int main(){

int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;

}

In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).

But when I pass nums to test() and call sizeof again I only get 4.

Why do I get 12 in main and 4 in test()?

Because the sizeof(int*), which is the function parameter, is 4 on your
system. If you want the keep track of the size, you have to pass it, or
just use std::vector.
 
L

Lionel B

I have this code:

void test(int* array)
{
int pp = sizeof(array);

cout << pp << endl;

}

int main(){

int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;

}

In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).
Yes.

But when I pass nums to test() and call sizeof again I only get 4.

Yes, because although you've called your parameter to test() "array",
it's *not* an array - it's a pointer to int; and the size (in bytes) of a
pointer to int on your system is evidently 4.
Why do I get 12 in main and 4 in test()?

Because pointers are not the same as arrays in C++. Note that when you
pass in the array nums as an argument to test(), you are actually passing
in a pointer to the first element of nums (this conversion happens
implicitly); in fact the size of the array nums is simply not available
to the function test(). You would have to pass it in as another parameter.
 
S

Sylvester Hesp

Ian Collins said:
desktop said:
I have this code:

void test(int* array)
{
int pp = sizeof(array);

cout << pp << endl;

}

int main(){

int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;

}

In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).

But when I pass nums to test() and call sizeof again I only get 4.

Why do I get 12 in main and 4 in test()?

Because the sizeof(int*), which is the function parameter, is 4 on your
system. If you want the keep track of the size, you have to pass it, or
just use std::vector.

Or use a template function:

template<size_t N>
void test(int (&array)[N])
{
cout << N << endl;
}

Of course, this limits the use of test() to only complete array types (no
incomplete array types, e.g., extern int bla[], and no pointers)

- Sylvester
 
P

Pete Becker

Sylvester said:
Or use a template function:

template<size_t N>
void test(int (&array)[N])
{
cout << N << endl;
}

Of course, this limits the use of test() to only complete array types (no
incomplete array types, e.g., extern int bla[], and no pointers)

And it generates a different function for each array size that you call
it on.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
S

Salt_Peter

I have this code:

void test(int* array)
{
int pp = sizeof(array);

cout << pp << endl;

}

int main(){

int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;

}

In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).

But when I pass nums to test() and call sizeof again I only get 4.

Why do I get 12 in main and 4 in test()?

The function test() takes a pointer to a single integer.
Why should it do anything else?
Follow this example and pay attention to the types detected.

#include <iostream>
#include <typeinfo>

void test(int* p_n)
{
std::cout << "type of p_n is ";
std::cout << typeid(p_n).name();
std::cout << std::endl;
}

template< typename T, const size_t Size >
void pass_by_ref(T (& array)[Size])
{
std::cout << "type of array is ";
std::cout << typeid(array).name();
std::cout << std::endl;
}

int main()
{
int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

int sz = sizeof(nums)/sizeof(int);
std::cout << "sz = " << sz;
std::cout << std::endl;

std::cout << "type of sz is ";
std::cout << typeid(sz).name() << std::endl;

std::cout << "type of nums is ";
std::cout << typeid(nums).name();
std::cout << std::endl;

test(nums);
pass_by_ref(nums);

return 0;
}

/*
sz = 12
type of sz is i
type of nums is A12_i
type of p_n is Pi
type of array is A12_i
*/
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top