c语言中的offset_c语言中/和%的区别

c语言中的offset_c语言中/和%的区别今天看libPhenom源代码,看到他们使用的JSON解析库参考的是JanssonJSON解析库。于是就去网上查了这个库,找到了官方网站:http://www.digip.org/jansson/。找了一下发现在Github上能够下载源代码,于是下载了源代码来瞅瞅。    看了一会儿发现有一块代码一直看不明白,就比如说如下的代码:json_t*json_object(void)

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

        今天看libPhenom源代码,看到他们使用的JSON解析库参考的是Jansson JSON解析库。于是就去网上查了这个库,找到了官方网站:http://www.digip.org/jansson/。找了一下发现在Github上能够下载源代码,于是下载了源代码来瞅瞅。

        看了一会儿发现有一块代码一直看不明白,就比如说如下的代码:

json_t *json_object(void)
{
    json_object_t *object = jsonp_malloc(sizeof(json_object_t));
    if(!object)
        return NULL;

    if (!hashtable_seed) {
        /* Autoseed */
        json_object_seed(0);
    }

    json_init(&object->json, JSON_OBJECT);

    if(hashtable_init(&object->hashtable))
    {
        jsonp_free(object);
        return NULL;
    }

    object->serial = 0;
    object->visited = 0;

    return &object->json;
}

        这里在一开始的时候malloc了一块指向struct json_object_t的地址,但是在将指针返回的时候,却并没有将这个分配好内存的指针返回,返回的是内部的一个struct json_t指针。那这样的话,在需要进行回收内存的时候,需要怎么去查找到地址来进行释放呢?

        又看了一会儿突然发现了如下的代码:

#define json_to_object(json_)  container_of(json_, json_object_t, json)

#define container_of(ptr_, type_, member_)  \
    ((type_ *)((char *)ptr_ - offsetof(type_, member_)))

        一下子就明白了,是通过offsetof这个宏来获取到内部成员在结构体内的偏移量,然后进而来获取整个结构体的地址。

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

struct test {
    int a;
    char b;
    long c;
    char d;
};

struct test2 {
    char a;
    char b;
    char c;
};

struct test3 {
    char a;
    struct test b;
    int c;
};

int main(int argc, const char * argv[]) {
    printf("struct test: offset a %d\n", (int) offsetof(struct test, a));
    printf("struct test: offset b %d\n", (int) offsetof(struct test, b));
    printf("struct test: offset c %d\n", (int) offsetof(struct test, c));
    printf("struct test: offset d %d\n", (int) offsetof(struct test, d));
    
    printf("struct test2: offset a %d\n", (int) offsetof(struct test2, a));
    printf("struct test2: offset b %d\n", (int) offsetof(struct test2, b));
    printf("struct test2: offset c %d\n", (int) offsetof(struct test2, c));
    
    printf("struct test3: offset a %d\n", (int) offsetof(struct test3, a));
    printf("struct test3: offset b %d\n", (int) offsetof(struct test3, b));
    printf("struct test3: offset c %d\n", (int) offsetof(struct test3, c));
    
    struct test3 * item = (struct test3 *) malloc(sizeof(struct test3) / sizeof(char));
    item->a = 'a';
    item->c = 10;
    struct test * innerItem = &item->b;
    innerItem->a = 1;
    innerItem->b = 1;
    innerItem->c = 1;
    innerItem->d = 1;
    
    struct test3 * compareItem = (struct test3 *) ((char *) innerItem - offsetof(struct test3, b));
    
    if (compareItem == item) {
        printf("equal\n");
    } else {
        printf("not equal\n");
    }
    
    free(item);
    
    return 0;
}

         打印结果如下:

struct test: offset a 0
struct test: offset b 4
struct test: offset c 8
struct test: offset d 16
struct test2: offset a 0
struct test2: offset b 1
struct test2: offset c 2
struct test3: offset a 0
struct test3: offset b 8
struct test3: offset c 32
equal
Program ended with exit code: 0

         这里struct test里面成员b和c之间偏移量为4是因为结构体将成员的存放地址对齐了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/172086.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号