There is an array of string concatpaths[k]
. What h-file must be connected to use size for this array?
- oneSuch a function is not and can not be. Instead of a simple array, use vector. - skegg
- If it is absolutely right to say - the compiler is not obliged to store information about the size of the array. Another thing is that the compiler must insert the correct code to call free, so it still stores some information. But it very much depends on the compiler and the platform (and on the optimization keys). In addition, the memory for arrays can be allocated a little more to make memory access more efficient. I heard a bike that in some compilers specifically arrays (even in the production code) were made 1 more - this greatly reduced the glitches when they make mistakes in the index and write outside. - KoVadim
- @KoVadim, in glibc, information about dynamically allocated memory is stored in the allocated memory itself (see Kerrisk "The Linux programming interface"). Even for memory allocated on the stack in C ++, the size can be determined dynamically at runtime, so the compiler cannot know this, only in the case of static size determination with a constant value. - skegg
- That's why I wrote that it strongly depends on the compiler and the platform . - KoVadim
- And what, in fact, is the question itself? What does "use size" mean? - Salivan
|
2 answers
In some cases, the size of the array can be found, start by reading this article . But the correct answer is to store it yourself. Or use stl / boost.
|
A small experiment showed:
#include <stdio.h> #include <stdlib.h> main (int ac, char *av[]) { int l = av[1]? atoi(av[1]): 10; int a[l]; printf ("%d\n",sizeof(a)); }
-
c:/Users/avp/src/cc/hashcode $ gcc tsz.cc:/Users/avp/src/cc/hashcode $ ./a 40 c:/Users/avp/src/cc/hashcode $ ./a 100 400 c:/Users/avp/src/cc/hashcode $
that sizeof () works for local dynamics.
Now determine the size in the elements?
- one@mikillskegg, well, is the
int a[l]
size known when compiled? Does gcc have Wolf Messing opportunities? - avp - oneI want to repent: in C99, sizeof can really be applied to arrays with run-time size. - skegg
|