I want to get the number of elements from the pointer in the array.

Asked 1 years ago, Updated 1 years ago, 298 views

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)
}

c

2023-02-17 13:02

1 Answers

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().
}


2023-02-17 13:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.