C++ is based on C and inherits many features from it. In relation to this question, it inherits something called "array/pointer equivalence" which is a rule that allows an array to decay to a pointer, especially when being passed as a function argument. It doesn't mean that an array is a pointer, it just means that it can decay to one.
void func(int ptr); int array[5]; int ptr = array; // valid, equivalent to 'ptr = &array[0]' func(array); // equivalent to func(&array[0]);
This last part is the most relevant to your question. You are not passing the array, you are passing the address of the 0th element.
In order for your function to know how big the incoming array is, you will need to send that information as an argument.
static const size_t ArraySize = 5; int array[ArraySize]; func(array, ArraySize);
Because the pointer contains no size information, you can't use sizeof.
void func(int* array) { std::cout << sizeof(array) << "\n"; }
This will output the size of "int*" - which is 4 or 8 bytes depending on 32 vs 64 bits.
Instead you need to accept the size parameters
void func(int* array, size_t arraySize); static const size_t ArraySize = 5; int array[ArraySize]; func(array, ArraySize);
Even if you try to pass a fixed-sized array, it turns out this is syntactic sugar:
void func(int array[5]);
Remember how I said that an array is NOT a pointer, just equivalent?
int array[5]; int* ptr = array; std::cout << "array size " << sizeof(array) << std::endl; std::cout << "ptr size " << sizeof(ptr) << str::endl;
array size will be 5 sizeof(int) = 20 ptr size will be sizeof(int ) which will be either 4 or 8 bytes.
sizeof returns the size of the type being supplied, if you supply an object then it deduces the type and returns the size of that
If you want to know how many elements of an array are in the array, when you have the array and not a pointer, you can write
sizeof(array) / sizeof(array[0])
or sizeof(array) / sizeof(*array)
Illustration: Steven Wilson The winter sun sets in mid-afternoon in Kolobrzeg, Poland, but the early twilight does not deter people from taking their regular outdoor promenade. Bundled up in parkas with fur-trimmed hoods, strolling hand in mittened hand along the edge of the Baltic Sea, off-season tourists from Germany stop openmouthed when they see a tall, […]
Comment utiliser visualVM à travers un pont SSH ?
par un proxy socks:
ssh -D 9096 user@host
ensuite tools -> options -> network -> proxy socks -> localhost:9096
Comment est géré la connection en JMX au instances de JVM afin d'avoir des infos sur la jvm, la taille mémoire, les objets, etc... ?
Un bon article pour comprendre ce qu'est une fuite mémoire et comment y remédier ;)
un article parlant du garbage collector en javascript.
peu connu, le javascript utilise comme son cousin java, un garbage collector, c'est à dire un processus régulier qui va vérifier si les variables sont encore utilisées, si ce n'est pas le cas il les suppriment.
et du coup il permet de récupérer de la mémoire.
il va falloir que je lise attentivement cet article, utilisant de manière intensive Javascript dans mon travail
(via http://linuxfr.org/users/crev/journaux/de-tout-de-rien-des-bookmarks-du-bla-bla--5 de CrEv)
Ouvrir les heap dumps trop gros pour la config pas défaut de Memory Analyser ou de windows XP