传递char未初始化的指针地址以及成员变量的分配和提取 [英] Passing char uninitialiezed pointer address and allocation and extraction of member variables

查看:90
本文介绍了传递char未初始化的指针地址以及成员变量的分配和提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 char * 指针从main传递到函数 abc ,该函数是未初始化的指针.我正在组成一个网络数据包,就像在功能中分配的那样,我希望它是数据包的表示形式.所以这是我在 main

I am passing char * pointer from main to a function abc which is uninitialized pointer. And I am composing a network packet so like what allocated in the function I want it to be the representation of a packet. So This is my passing of uninitialized pointer in function call inside main

            char *pay;
            abc(&pay);

在我具有的功能中,如下所示的功能

in the function I have as signature of a function like following

            void abc(char **c)

这是全部功能

        void abc(char **c)
        {
            *c=(char *) malloc(sizeof(struct ethhdr)+sizeof(struct iphdr)+sizeof(struct tcphdr)+1000/*1000=payload*/);
            struct ethhdr *eth=(struct ethhdr *)&*c;
            struct iphdr *ip=(struct iphdr *)(&*eth+sizeof(struct ethhdr));
            struct tcphdr *tcp=(struct tcphdr *)(&*ip+sizeof(struct iphdr));
            //populate eth
            //...
            //populate ip
            //...
            //populate tcp
            tcp->source=80;
            printf("%d\n",tcp->source);
            int *x=(int *)*c;
            //x=10;

        }

我想知道如何在 abc 函数中转换第二行我就是这样

I like to know how can I cast second line in abc function I am doing like this

        struct ethhdr *eth=(struct ethhdr *)&*c;

abc 函数内的第二行.

但在包含此指令 tcp-> source = 80; 的行中,它会导致segFault.

But at line containing this instruction tcp->source=80; its causing segFault.

所以我的问题是如何在函数中强制转换 c 双指针并将其分配给 struct ethhdr * struct iphd * struct tcphdr * 指针

So my question is how can I cast the c double pointer in function and to assign it tostruct ethhdr *,struct iphd * and struct tcphdr * pointers

以及如何将值分配给 struct ethhdr struct iphdr struct tcphdr

And how to assign the values to members of struct ethhdr , struct iphdr and struct tcphdr

我尝试过

tcp-> source = 80;

tcp->source=80;

但是会导致segFault

But causing segFault

推荐答案

您有两个问题.

第一个是在以下位置同时使用address-of和dereference运算符:

The first is the use of both the address-of and dereference operators in:

struct ethhdr *eth=(struct ethhdr *)&*c;

表达式& * c 与普通的 c 相同,这是不正确的.您需要在这里 * c :

The expression &*c is the same as plain c, which isn't correct. You need *c here:

struct ethhdr *eth = (struct ethhdr *) *c;

您对其他指针(例如& * eth )也有类似的问题,但是它不会因此引起任何问题.例如,仅使用普通的 eth .

You have similar problem with the other pointers (e.g. &*eth) but there it doesn't cause any problems because of this. Use just plain eth (for example).

第二个问题是双重的,是所有其他分配.首先,您使用的指针不是字节指针,这意味着强制转换为另一个结构指针将破坏严格别名.

The second problem is two-fold, and is all the other assignments. First of all the pointers you use are not byte pointers which means casting to another structure pointer will break strict aliasing.

第二,您必须记住,对于任何指针或数组 p 和索引/偏移量 i ,表达式 *(p + i)完全等于 p [i] .对于类似 eth + sizeof(struct ethhdr)的表达式,这意味着它与&)eth [sizeof(struct ethhdr)])相同.显然这是错误的( eth 可以看作是一个元素的数组,而不是至少包含 sizeof(struct ethaddr)+ 1 个元素的数组.

Secondly you have to remember that for any pointer or array p and index/offset i, the expression *(p + i) is exactly equal to p[i]. For an expression like eth+sizeof(struct ethhdr) that means it's the same as &)eth[sizeof(struct ethhdr)]). Which is clearly wrong (eth could be seen as an array of one element, not an array of at least sizeof(struct ethaddr) + 1 elements.

要解决这两个问题,包括严格的混叠和偏移量,您需要使用原始的字节指针 * c 并将偏移量(以字节为单位)添加到其中:

To fix both these problems, strict aliasing and the offsets, you need to use the original byte-pointer *c and add the offsets in bytes to that:

struct iphdr *ip = (struct iphdr *) (*c + sizeof(struct ethhdr));
struct tcphdr *tcp = (struct tcphdr *) (*c + sizeof(struct ethhdr) + sizeof(struct iphdr));


从上面开始,现在应该相对容易地找出如何获取有效负载数据的指针,因为它不是您的代码中使用的普通 * c .

这篇关于传递char未初始化的指针地址以及成员变量的分配和提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆