If you receive a pointer pointing to the beginning of a char array as an argument as follows, I would like to know the number of elements of the char array using string_ptr.
void hoge(char*string_ptr){
// For char string [100]
// size of (string) / size of (char)
}
Not possible (so buffer overrun vulnerability still exists)
void hoge (char*string_ptr, size_t buffsize);
hoge()
side should take memory dynamically
char*hoge(){
char*p = NULL;
for {
char*q = realoc(p,newsize);
if(q==NULL) {Error handling and escape;}
// Continue only if no errors have occurred
p=q;
take some action on p;
}
It is the responsibility of the caller to return p;//free().
}
© 2024 OneMinuteCode. All rights reserved.