关于C结构的问题 [英] A question on C structures

查看:86
本文介绍了关于C结构的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构:

struct msghdr

{

//有些人来这里


struct iovec * msg_iov;


//更多---

}


struct msghdr msg;

struct iovec的主体如下所示:

struct iovec

{

void * iov_base ; //指向(char *)缓冲区

int iov_len;

}

我声明了一个struct iovec数组:

struct iovec iov [10];


然后在适当的内存分配后用值填充它:


for i:0 to 9

strcpy(iov [i] .iov_base,buf);

iov [i] .iov_len = length;


然后我让* msg_iov指向这个数组:

msg.msg_iov = iov;


我的问题是当我尝试访问iov_base时:

msg.msg_iov [i] .iov_base

它仅适用于i = 0,其余部分显示为NULL。


现在有趣的部分,当我将''msg''的

地址传递给显示功能(显示(struct msghdr * msg))

然后在该功能中我'能够毫无问题地访问缓冲区

使用,

msg-> msg_iov [i] .iov_base

不是这很奇怪吗?

为什么''访问方法'会有所不同

任何建议/澄清是受欢迎的。

解决方案

nishantabedafésursonécran:

struct msghdr
{<结构iovec * msg_iov;
}

结构msghdr msg;

结构iovec
{
void * iov_base; //指向(char *)缓冲区
int iov_len;
}
我的问题是当我尝试访问iov_base时:
msg.msg_iov [ i] .iov_base
它仅适用于i = 0,其余部分显示为NULL。

现在有趣的部分,当我通过''msg'的
地址时'到显示功能(显示(struct msghdr * msg))
然后在该功能中我能够毫无问题地访问缓冲区
使用,
msg-> msg_iov [我] .iov_base

这不奇怪吗?


不,它不是。

为什么''访问方法'会有所不同
欢迎任何建议/澄清。




因为你通过指针传递了结构的地址(这是好方法)

。访问现在是间接的。为了澄清,我建议你改变你的定义


display(struct msghdr * p_msg)

{

< ...>

p_msg-> msg_iov [i] .iov_base

}


或(可能更好,最后给我)


显示(struct msghdr * this)

{

< ;. ..>

this-> msg_iov [i] .iov_base

}


-

Emmanuel


2004年7月19日21:54:56 -0700, ni ******* @ gmail.com (nishant)写道:


snip脱节代码片段

我的问题是当我尝试通过以下方式访问iov_base时:
msg.msg_iov [i] .iov_base
它仅适用于i = 0,其余部分显示为NULL。

现在有趣的部分,当我将''msg''的
地址传递给显示功能(显示(struct msghdr * msg))
然后在那个功能中我就是能够 毫无问题地访问缓冲区
使用,
msg-> msg_iov [i] .iov_base

这不是很奇怪吗?
为什么会这样?访问方法''有所不同
欢迎任何建议/澄清。




您需要发布一个可编辑的示例来说明行为

您描述。

<<删除电子邮件的del>>





nishant写道:

我有一个结构:
struct msghdr
{
//有些人在这里

结构iovec * msg_iov;

//更多---
}

struct msghdr msg;

struct iovec的主体看起来像这样:
struct iovec
{
void * iov_base; //指向(char *)缓冲区
int iov_len;
}

我声明了一个struct iovec数组:
struct iovec iov [10];

然后在适当的内存分配后用值填充它:

对于i:0到9
strcpy(iov [i] .iov_base,buf);
iov [i] .iov_len = length;

然后我让* msg_iov指向这个数组:
msg.msg_iov = iov;

我的问题是当我尝试通过以下方式访问iov_base时:
msg.msg_iov [i] .iov_base
它仅适用于i = 0,其余部分显示为NULL。


这看起来不对。你的语法,msg.msg_iov [i] .iov_base,

看起来很好。我假设所有10个动态分配

都是成功的,因为strcpy在循环中工作。必须

其他相关内容。


查看示例

现在有趣的部分,当我通过
'msg''到显示功能的地址(显示(struct msghdr * msg))
然后在该功能中我能够毫无问题地访问缓冲区
使用,
msg-> msg_iov [i] .iov_base

这不奇怪吗?
您使用 - >运算符是正确的。我想你的''怪异''评论

是因为。运营商不工作,但 - >按预期工作

。在我看来,你正在使用运营商

更正。

为什么访问方法会有所不同
欢迎提出任何建议/澄清。




例子:


#include< stdio.h>

#include < string.h>

#include< stdlib.h>


#define SZ 10

struct iovec

{

void * iov_base; //指向(char *)缓冲区

size_t iov_len;

};


struct msghdr

{

//有些人来这里


struct iovec * msg_iov;


//一些更多---

};


void FreeMsghdr(struct msghdr * msg)

{

int i;


for(i = 0; i< SZ; i ++)

{

free(msg- > msg_iov [i] .iov_base);

msg-> msg_iov [i] .iov_base = NULL;

msg-> msg_iov [i] .iov_len = 0;

}

msg-> msg_iov = NULL;

返回;

}


int main(无效)

{

struct msghdr msg;

struct iovec iov [SZ];

int i;

char buf [32];


for(i = 0; i< SZ; i ++)

{

iov [i] .iov_base = malloc(32);

if(iov [i] .iov_base)

{

sprintf(buf,HelloWorld_%d,i);

strcpy(iov [i] .iov_base,buf);

iov [i]。 iov_len = strlen(buf);

}

}

msg.msg_iov = iov;

for(i = 0; i< SZ; i ++)

printf(" msg.msg_iov [%d] .iov_base = \"%s \" \ n",i,

msg.msg_iov [i] .iov_base?msg.msg_iov [i] .iov_base:" NULL");

FreeMsghdr(& msg);

puts( " \\\
After Calling FreeMsghdr:");

for(i = 0; i< SZ; i ++)

printf(" iov [%d] .iov_base =%s \ t \ t"

" iov [%d] .iov_len =%u \ n",i,

iov [i ] .iov_base?iov [i] .iov_base:" NULL",

i,iov [i] .iov_len);

返回0;

}


-

Al Bowers

美国佛罗里达州坦帕市

mailto:< a href =mailto:xa ****** @ myrapidsys.com> xa ****** @ myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822/


I''ve a structure:
struct msghdr
{
//some goes here

struct iovec *msg_iov;

//some more ---
}

struct msghdr msg;
The body of struct iovec looks like this:
struct iovec
{
void *iov_base; // to point to a (char *) buffer
int iov_len;
}
I declare a struct iovec array:
struct iovec iov[10];

Then fill it with values after proper memory allocation:

for i: 0 to 9
strcpy(iov[i].iov_base, buf);
iov[i].iov_len = length;

Then i make *msg_iov to point to this array:
msg.msg_iov = iov;

my question is when i try to access iov_base by:
msg.msg_iov[i].iov_base
it works only for i=0, for the rest it shows NULL.

Now the interesting part, when i pass the
address of ''msg'' to a display function (display(struct msghdr *msg))
then in that function i''m able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base
Isn''t this weird?
Why will a ''access method'' make any difference
Any suggestion/clarifications are welcome.

解决方案

nishant a couché sur son écran :

struct msghdr
{
struct iovec *msg_iov;
}

struct msghdr msg;

struct iovec
{
void *iov_base; // to point to a (char *) buffer
int iov_len;
}

my question is when i try to access iov_base by:
msg.msg_iov[i].iov_base
it works only for i=0, for the rest it shows NULL.

Now the interesting part, when i pass the
address of ''msg'' to a display function (display(struct msghdr *msg))
then in that function i''m able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base

Isn''t this weird?
No, it''s not.
Why will a ''access method'' make any difference
Any suggestion/clarifications are welcome.



Because you passed the address of the structure (Which is The Good Way)
via a pointer. The access is now ''indirect''. For clarification, I
suggest that you change your definition for

display (struct msghdr *p_msg)
{
<...>
p_msg->msg_iov[i].iov_base
}

or (probably better, at last to me)

display (struct msghdr *this)
{
<...>
this->msg_iov[i].iov_base
}

--
Emmanuel


On 19 Jul 2004 21:54:56 -0700, ni*******@gmail.com (nishant) wrote:

snip disjointed code fragments

my question is when i try to access iov_base by:
msg.msg_iov[i].iov_base
it works only for i=0, for the rest it shows NULL.

Now the interesting part, when i pass the
address of ''msg'' to a display function (display(struct msghdr *msg))
then in that function i''m able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base
Isn''t this weird?
Why will a ''access method'' make any difference
Any suggestion/clarifications are welcome.



You need to post a compilable example that illustrates the behavior
you describe.
<<Remove the del for email>>




nishant wrote:

I''ve a structure:
struct msghdr
{
//some goes here

struct iovec *msg_iov;

//some more ---
}

struct msghdr msg;
The body of struct iovec looks like this:
struct iovec
{
void *iov_base; // to point to a (char *) buffer
int iov_len;
}
I declare a struct iovec array:
struct iovec iov[10];

Then fill it with values after proper memory allocation:

for i: 0 to 9
strcpy(iov[i].iov_base, buf);
iov[i].iov_len = length;

Then i make *msg_iov to point to this array:
msg.msg_iov = iov;

my question is when i try to access iov_base by:
msg.msg_iov[i].iov_base
it works only for i=0, for the rest it shows NULL.
This doen''t seem right. Your syntax, msg.msg_iov[i].iov_base,
looks fine. I am assuming that all 10 of the dynamic allocations
were successful since the strcpy worked in the loop. There must
be something else involved.

See the example
Now the interesting part, when i pass the
address of ''msg'' to a display function (display(struct msghdr *msg))
then in that function i''m able to access the buffers without any problem
using,
msg->msg_iov[i].iov_base

Isn''t this weird? Your use of the -> operator is correct. I guess your ''weird'' comment
is because the . operator is not working but the -> is working
as expected. It appears to me that you are using the operators
corrected.
Why will a ''access method'' make any difference
Any suggestion/clarifications are welcome.



The example:

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

#define SZ 10

struct iovec
{
void *iov_base; // to point to a (char *) buffer
size_t iov_len;
};

struct msghdr
{
//some goes here

struct iovec *msg_iov;

//some more ---
};

void FreeMsghdr(struct msghdr *msg)
{
int i;

for(i = 0; i < SZ; i++)
{
free(msg->msg_iov[i].iov_base);
msg->msg_iov[i].iov_base = NULL;
msg->msg_iov[i].iov_len = 0;
}
msg->msg_iov = NULL;
return;
}

int main(void)
{
struct msghdr msg;
struct iovec iov[SZ];
int i;
char buf[32];

for(i = 0; i < SZ;i++)
{
iov[i].iov_base = malloc(32);
if(iov[i].iov_base)
{
sprintf(buf,"HelloWorld_%d",i);
strcpy(iov[i].iov_base,buf);
iov[i].iov_len = strlen(buf);
}
}
msg.msg_iov = iov;
for(i = 0;i < SZ;i++)
printf("msg.msg_iov[%d].iov_base = \"%s\"\n",i,
msg.msg_iov[i].iov_base?msg.msg_iov[i].iov_base:"NULL");
FreeMsghdr(&msg);
puts("\nAfter Calling FreeMsghdr:");
for(i = 0;i < SZ;i++)
printf("iov[%d].iov_base = %s\t\t"
"iov[%d].iov_len = %u\n",i,
iov[i].iov_base?iov[i].iov_base:"NULL",
i,iov[i].iov_len);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


这篇关于关于C结构的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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