Avoid incompatible pointer warning when dealing wi

2019-07-06 17:22发布

Assuming this program:

#include <stdio.h>
#include <string.h>

static void ring_pool_alloc(void **p, size_t n) {
    static unsigned char pool[256], i = 0;
    *p = &pool[i];
    i += n;
}

int main(void) {
    char *str;
    ring_pool_alloc(&str, 7);
    strcpy(str, "foobar");
    printf("%s\n", str);
    return 0;   
}

... is it possible to somehow avoid the GCC warning

test.c:12: warning: passing argument 1 of ‘ring_pool_alloc’ from incompatible pointer type
test.c:4: note: expected ‘void **’ but argument is of type ‘char **’

... without casting to (void**) (or simply disabling the compatibility checks)? Because I would very much like to keep compatibility warnings regarding indirection-level...

3条回答
爷、活的狠高调
2楼-- · 2019-07-06 17:34

Why don’t you change the method signature such that it returns the new pointer instead of passing it by pointer? In fact, just like regular malloc does:

static void * ring_pool_alloc(size_t n) {
    static unsigned char pool[256], i = 0;
    void *p = &pool[i];
    i += n;
    return p;
}

int main(void) {
    char *str = ring_pool_alloc(7);
    strcpy(str, "foobar");
    printf("%s\n", str);
    return 0;   
}
查看更多
Melony?
3楼-- · 2019-07-06 17:35

Simply change:

static void ring_pool_alloc(void **p, size_t n) {

to:

static void ring_pool_alloc(char **p, size_t n) {
查看更多
等我变得足够好
4楼-- · 2019-07-06 17:58

Change ring_pool_alloc to receive a void *. You can then recast to void ** in the function, if you wish.

Or in your specific case:

/* You can pass any pointer as a first parameter */
static void ring_pool_alloc(void *r, size_t n) {
    unsigned char **p = r; /* no cast necessary */
    static unsigned char pool[256], i = 0;
    *p = &pool[i];
    i += n;
}

Note that void ** cannot act as a generic pointer-to-pointer type. On the other hand, convertions from and to void * with other pointer types are applied automatically.

查看更多
登录 后发表回答