当前位置: 技术问答>linux和unix
麻烦大家帮我看一下:谁动了我的指针??
来源: 互联网 发布时间:2015-11-22
本文导语: 代码如下: struct traffic_info{ unsigned int type; unsigned int re_byte, re_pack, re_err, re_drop, re_fifo, re_frame, re_compr, re_mcast; unsigned int se_byte, se_pack, se_err, se_drop, se_fifo, se_coll, se_carr, se_compr; }; struct traffic_info *get_et...
代码如下:
struct traffic_info{
unsigned int type;
unsigned int re_byte, re_pack, re_err, re_drop, re_fifo, re_frame, re_compr, re_mcast;
unsigned int se_byte, se_pack, se_err, se_drop, se_fifo, se_coll, se_carr, se_compr;
};
struct traffic_info *get_eth0_traffic()
{
FILE* fp;
char buffer[1024];
size_t bytes_read;
char* match;
struct traffic_info* traffic;
/* Read the entire contents of /proc/net/dev into the buffer. */
fp = fopen ("/proc/net/dev", "r");
bytes_read = fread (buffer, 1, sizeof (buffer), fp);
fclose (fp);
/* Bail if read failed or if buffer isn’t big enough. */
if (bytes_read == 0 || bytes_read == sizeof (buffer))
return 0;
/* NUL-terminate the text. */
buffer[bytes_read] = '';
/* Locate the line that starts with “cpu MHz”. */
match = strstr (buffer, "eth0");
if (match == NULL)
return NULL;
/* Parse the line to extract the clock speed. */
printf("ready for reading!n");
sscanf (match, "eth0 : %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
&(traffic->re_byte),&(traffic->re_pack),&(traffic->re_err),&(traffic->re_drop),
&(traffic->re_fifo),&(traffic->re_frame),&(traffic->re_compr),&(traffic->re_mcast),
&(traffic->se_byte),&(traffic->se_pack),&(traffic->se_err),&(traffic->se_drop),
&(traffic->se_fifo),&(traffic->se_coll),&(traffic->se_carr),&(traffic->se_compr));
traffic->type=0;
return traffic;
}
/*get the eth0 traffic and send*/
void send_traffic_data(int client_fd)
{
int send_num;
struct traffic_info *p=get_eth0_traffic(); /*get the message to send*/
//struct traffic_info *p2; /*我想声明另外一个指针,但是却运行说"段错误",为什么呢*/
send_num=send(client_fd,p,sizeof(struct traffic_info),0);/*send the message successfully*/
printf("the eth0 data is %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %un",
p->type,
p->re_byte,p->re_pack,p->re_err,p->re_drop,
p->re_fifo,p->re_frame,p->re_compr,p->re_mcast,
p->se_byte,p->se_pack,p->se_err,p->se_drop,
p->se_fifo,p->se_coll,p->se_carr,p->se_compr);
/*here the value is right!*/
printf("the type is:%un",p->re_byte); /*but here the value of type is wrong!*/
}
问题1:主要出在最后一个函数内,当我调用send成功发送数据后(接收端数据正确),而且其后的printf打印的
数据也正确,但是下一个就完全不对了. 当初我把send放在了最后,发送数据全部错误.给指针赋值后多打印后指针的值就变了.过程中到底谁动了指针呢?
问题2:我试图再声明一个指针来保存一下看看,打印后值还是否正确.但是运行到此处就停住了说"段错误".
难道不能声明2个同样指针吗?
这些问题我感到很诡异,百思不得要领,麻烦各位帮我看看.
多谢啦!
struct traffic_info{
unsigned int type;
unsigned int re_byte, re_pack, re_err, re_drop, re_fifo, re_frame, re_compr, re_mcast;
unsigned int se_byte, se_pack, se_err, se_drop, se_fifo, se_coll, se_carr, se_compr;
};
struct traffic_info *get_eth0_traffic()
{
FILE* fp;
char buffer[1024];
size_t bytes_read;
char* match;
struct traffic_info* traffic;
/* Read the entire contents of /proc/net/dev into the buffer. */
fp = fopen ("/proc/net/dev", "r");
bytes_read = fread (buffer, 1, sizeof (buffer), fp);
fclose (fp);
/* Bail if read failed or if buffer isn’t big enough. */
if (bytes_read == 0 || bytes_read == sizeof (buffer))
return 0;
/* NUL-terminate the text. */
buffer[bytes_read] = '';
/* Locate the line that starts with “cpu MHz”. */
match = strstr (buffer, "eth0");
if (match == NULL)
return NULL;
/* Parse the line to extract the clock speed. */
printf("ready for reading!n");
sscanf (match, "eth0 : %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
&(traffic->re_byte),&(traffic->re_pack),&(traffic->re_err),&(traffic->re_drop),
&(traffic->re_fifo),&(traffic->re_frame),&(traffic->re_compr),&(traffic->re_mcast),
&(traffic->se_byte),&(traffic->se_pack),&(traffic->se_err),&(traffic->se_drop),
&(traffic->se_fifo),&(traffic->se_coll),&(traffic->se_carr),&(traffic->se_compr));
traffic->type=0;
return traffic;
}
/*get the eth0 traffic and send*/
void send_traffic_data(int client_fd)
{
int send_num;
struct traffic_info *p=get_eth0_traffic(); /*get the message to send*/
//struct traffic_info *p2; /*我想声明另外一个指针,但是却运行说"段错误",为什么呢*/
send_num=send(client_fd,p,sizeof(struct traffic_info),0);/*send the message successfully*/
printf("the eth0 data is %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %un",
p->type,
p->re_byte,p->re_pack,p->re_err,p->re_drop,
p->re_fifo,p->re_frame,p->re_compr,p->re_mcast,
p->se_byte,p->se_pack,p->se_err,p->se_drop,
p->se_fifo,p->se_coll,p->se_carr,p->se_compr);
/*here the value is right!*/
printf("the type is:%un",p->re_byte); /*but here the value of type is wrong!*/
}
问题1:主要出在最后一个函数内,当我调用send成功发送数据后(接收端数据正确),而且其后的printf打印的
数据也正确,但是下一个就完全不对了. 当初我把send放在了最后,发送数据全部错误.给指针赋值后多打印后指针的值就变了.过程中到底谁动了指针呢?
问题2:我试图再声明一个指针来保存一下看看,打印后值还是否正确.但是运行到此处就停住了说"段错误".
难道不能声明2个同样指针吗?
这些问题我感到很诡异,百思不得要领,麻烦各位帮我看看.
多谢啦!
|
在函数struct traffic_info *get_eth0_traffic()中的struct traffic_info* traffic;没有为traffic分配内存
|
补基础http://publications.gbdirect.co.uk/c_book/
经典:The C Programming Language
经典:The C Programming Language
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。