Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

How does C++ know the length of an array when allocated?

fawnha
Beginner
709 Views
Hi,
When I allocate an array in a function I can use sizeof to get the length og the array in bytes but when I pass this to another function I can no longer do this. How does C++ know the length of an array when it just stores a pointer to the allocated memory? Why doesn't this work in other functions?

Thanks for any help,
Hamish
0 Kudos
5 Replies
Maximillia_D_Intel
709 Views
Can you provide a testcase? It is unclear what you mean by array in C++?

Thanks,

Max
0 Kudos
fawnha
Beginner
709 Views
say you have

int main()
{
char foo[100];
printf("main array size: %d", sizeof(foo));
bar(foo);
return 0;
}

void bar(int foo[])
{
printf("foo array size: %d", sizeof(foo));
}

This will print:

main array size: 100
foo array size: 4

My question is why doesn't sizeof(foo) in foo() evaluate to 100 where as sizeof(foo) in main does evaluate to 100?

Thanks,
Hamish
0 Kudos
Anna_N_
Beginner
709 Views
/*
sizeof: "(...) When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays. (...)"
*/
// foo in bar1 is a pointer, so sizeof returns size of a pointer.
// Any corrections welcome.

void bar1(short foo[])
{
printf("bar1 foo's size: %u ", sizeof(foo));
printf("bar1 elem. size: %u ", sizeof(foo[0]));
printf("bar1 foo[0]: %x ", foo[0]);
}
void bar2(char foo[])
{
printf("bar2 foo's size: %u ", sizeof(foo));
printf("bar2 elem. size: %u ", sizeof(foo[0]));
printf("bar2 foo[0]: %x ", foo[0]);
}
void bar3(char foo[100])
{
printf("bar3 foo's size: %u ", sizeof(foo)); // guess this :)
printf("bar3 elem. size: %u ", sizeof(foo[0]));
printf("bar3 foo[0]: %x ", foo[0]);
}
void bar4(char (&foo)[100])
{
printf("bar4 foo's size: %u ", sizeof(foo));
printf("bar4 elem. size: %u ", sizeof(foo[0]));
printf("bar4 foo[0]: %x ", foo[0]);
}
int main()
{
char foo[100] = {0x77, 0x77, 0x77, 0x77};
printf("main foo's size: %u ", sizeof(foo));
printf("main elem. size: %u ", sizeof(foo[0]));
printf("main foo[0]: %x ", foo[0]);
bar1(foo);
bar2(foo);
bar3(foo);
bar4(foo);
return 0;
}
// Regards, Anna
0 Kudos
bronx
Beginner
709 Views
unfortunately there is no native C++ capability to retrieve the array size, though you can do something along these lines :

class CharArray
{
char *elts;
int size;
public:
CharArray (int vSize) : size(vSize)
{elts = new char[size];}
~CharArray {delete [] elts;}
int getSize () const {return size;}
// TODO add getElt,setElt fcts and/or
// operator [], operator [] const
};

typical client code will use CharArray & or const CharArray & as parameter like in :

void Func (const CharArray &a)
{
cout << "a has " << a.getSize() << " elements ";
}


0 Kudos
Telnov__Alex
Beginner
709 Views
To main(), foo is head of a static array (i.e., an array that exists at compile time). According to the C++ standard, sizeof(foo) is resolved at compile time to be the size of this array.

To bar(), foo is just a pointer. sizeof(pointer) is also resolved at compile time and is 4 bytes in the 32-bit architecture.

In general, beware of possible inconsistencies in the behavior of sizeof() on arrays depending on implementation:

http://en.wikipedia.org/wiki/Sizeof#Using_sizeof_with_arrays
0 Kudos
Reply