size of char *
-
Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)
-
Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)
baerten wrote:
How can i find the number of allocated chars from this char*
you can use
_msize()
to find the size of buffer allocated in heap. I.e the function will work in the secnarios likechar *pszName = new char[50]; fun( pszName );
but not forchar szName[50]; fun( szName );
So to solve such pblms we usually pass the size of array also to such functions.nave [OpenedFileFinder]
-
Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)
You should prefer strlen which will check the real size of your null-terminated string and not the size of the 'container'. So, if you copy "Hello" in your buffer, strlen will return 5 and not 50. Is that what you are looking for ? If no, then you should maybe elaborate a little bit more.
Cédric Moonen Software developer
Charting control [v1.2] -
Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)
short answer, you can't. When passing arrays around, it is always good practice to also send the number of items, or use one of the STL templates like std::vector
Waldermort
-
Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)
Have you not ever used functions that take a buffer and the size of that buffer as arguments?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)
That's called "array decay" :) Just google for the term and you'll find the explanation.
-
Hi everybody, i have a class which stores char arrays, for example char name[50]; If i write int test = sizeof(inst->name); test has the value 50, which his OK. Now i like to send a the char array to a function, there'in it has the form char* and i sizeof() of this char* returns only the size of the pointer or something like this. How can i find the number of allocated chars from this char* ? Big thanks ! :)