关于指向结构数组的指针的问题 [英] Question about a pointer to an array of structs

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

问题描述



我想知道是否有人可以帮助我,我已经抓了几个小时

了。


基本上,我已经定义了一个名为cache_object的结构:

struct cache_object {

char hostname [HOSTSIZE + 1];

char ipaddr [IPSIZE + 1];

};


我计划存储这些包含缓存的
我的代码的解析器部分的主机名和IP地址。

更复杂的是,数组必须通过
$ b可用于子进程$ b shm。现在,我对Shm很新,所以我要把这一点留到最后。无论如何,

为了使用shm我需要一个指向我的结构数组的指针

" cache_object"。我已经定义了这样的指针:


struct cache_object(* cache)[CACHESIZE];


HOSTNAME / IPSIZE / CACHESIZE是所有#defined都带有整数值。


现在我开始研究shm代码之前我正在使用malloc()来测试
,因为malloc会返回指向一块内存的指针与

shmat()相同。 (实际上我写了一堆shm代码,无法让它工作并且

将其全部删除以试图回到问题的根源)。


我遇到的问题,我的shm代码也发生了这个问题

我似乎无法弄清楚我应该允许多少内存to

存储这个结构数组。这是我的代码:


cache = malloc(sizeof(struct cache_object [CACHESIZE]));

作为旁注,我该怎么做呢?我试过了:

cache =(struct * cache_object [CACHESIZE])malloc(sizeof(struct

cache_object [CACHESIZE]));


但我在'''''令牌之前得到'语法错误'来自gcc ..我试过了:

cache =(struct cache_object [CACHESIZE])malloc(sizeof(struct

cache_object [CACHESIZE]));

但我得到cast指定数组类型


无论如何,sizeof()应该返回我需要分配的字节数

为了存储一个CACHESIZE大小的cache_object结构数组,对吗?

嗯,我是这么认为的。


运行以下循环:


for(c = 0; c< CACHESIZE; c ++){

printf(" cache:%i ipaddr:%s hostname:

%s \ n",c,cache [c] - > ipaddr,cache [c] - > hostname);

}


使用这些#defines:

#define HOSTSIZE 255

#define IPSIZE 255

#define CACHESIZE 10


产生以下输出:


缓存:0 ipaddr:主机名:

缓存:1 ipaddr:主机名:

缓存:2 ipaddr:主机名:

缓存:3 ipaddr:主机名:

Seg心理错误(核心倾销)


奇怪的是,将CACHESIZE减少到5可防止段错误和程序

按预期运行:


$ ./caching_logger

foo 127.0.0.1 foo

cache:0 ipaddr:hostname:

cache:1 ipaddr:hostname :

缓存:2 ipaddr:主机名:

缓存:3 ipaddr:主机名:

缓存:4 ipaddr:主机名:


任何对此的帮助都会非常感激,它一整天都在推动我

坚果。


Incase任何人都感兴趣,我正在处理的代码位于日志的末尾
来自apache的
管道分割出每个vhost的日志行并将它们记录到

单独的文件,它还将日志行中的IP地址解析为dns等待apache本身的时间。然而,该程序的主要目的是通过只需要一个管道来支持apache'的打开文件描述符限制这个程序 - 程序本身关闭并重新启动打开

写入之间的每个日志文件;效率较低,但在我运行的情况下是必要的。


下面是我到目前为止的代码(fork注释掉了需要

shm) :


#include< stdlib.h>

#include< stdio.h>

#include< netdb .h>

#include< string.h>

#include< sys / types.h>

#include< netinet /in.h>

#include< arpa / inet.h>

#include< unistd.h>

#include< ; sys / socket.h>

#include< signal.h>


#define HOSTSIZE 255

#define IPSIZE 255

#define CACHESIZE 10

struct cache_object {

char hostname [HOSTSIZE + 1];

char ipaddr [IPSIZE + 1];

};


struct cache_object(* cache)[CACHESIZE];

int * cachecount;


int main(){

char vhost [HOSTSIZE + 1],ip [IPSIZE],line [2046],path [255 ],

finalhost [HOSTSIZE + 1 ];

FILE * outstream;

struct in_addr iph;

struct hostent * hp;

int c;

信号(SIGCHLD,SIG_IGN);


cache = malloc(sizeof(struct cache_object [CACHESIZE]));

cachecount =(int *)malloc(sizeof(int));

* cachecount = 0;

while(!feof(stdin)){

memset(vhost,0,255);

memset(ip,0,255);

memset(line,0,2046);

memset( path,0,255);

memset(finalhost,0,255);

fscanf(stdin,"%s%s",vhost,ip);

fgets(line,2046,stdin);

for(c = 0; c< CACHESIZE; c ++){

printf(" cache:%i) ipaddr:%s主机名:

%s \ n",c,cache [c] - > ipaddr,cache [c] - > hostname);

}

// if(fork()== 0){

sprintf(path," / home / logs / access_logs /%s",vhost); <如果((ht = gethostbyaddr((const char *)& iph,

sizeof(struct in_addr),AF_INET))!= NULL){

sprintf(finalhost,"%s",hp-> h_name);

} else {

sprintf(finalhost,"%s",ip);

}

}其他{

sprintf (finalhost,"%s",ip);

}

sprintf(cache [* cachecount] - > ipaddr,"%s",ip);


sprintf(cache [* cachecount] - > hostname,"%s",finalhost);

if(* cachecount< CACHESIZE){

* cachecount = * cachecount + 1;

}否则{

* cachecount = 0;

}

outstream = fopen(path," a");

if(outstream!= NULL){

fprintf(outstream, "%s%s",finalhost,line);

fclose(outstream);

}

// exit(0);

//}


}

退出(0);

}



-

~Kieran Simkin

Digital Crocus
http://digital-crocus.com/

推荐答案

./caching_logger

foo 127.0.0.1 foo

cache:0 ipaddr:hostname:

cache:1 ipaddr:hostname:

cache:2 ipaddr:hostname:

cache:3 ipaddr:hostname:

cache:4 ipaddr:hostname:


任何对此的帮助将非常感激,它''一直在驾驶我

坚果。


如果有人感兴趣,我正在处理的代码就在日志的末尾

来自apache的管道为每个vhost分割日志行并将它们记录到

单独的文件中,它还将日志行中的IP地址解析为dns等待

apache本身的时间。然而,该程序的主要目的是通过只需要一个管道来支持apache'的打开文件描述符限制这个程序 - 程序本身关闭并重新启动打开

写入之间的每个日志文件;效率较低,但在我运行的情况下是必要的。


下面是我到目前为止的代码(fork注释掉了需要

shm) :


#include< stdlib.h>

#include< stdio.h>

#include< netdb .h>

#include< string.h>

#include< sys / types.h>

#include< netinet /in.h>

#include< arpa / inet.h>

#include< unistd.h>

#include< ; sys / socket.h>

#include< signal.h>


#define HOSTSIZE 255

#define IPSIZE 255

#define CACHESIZE 10

struct cache_object {

char hostname [HOSTSIZE + 1];

char ipaddr [IPSIZE + 1];

};


struct cache_object(* cache)[CACHESIZE];

int * cachecount;


int main(){

char vhost [HOSTSIZE + 1],ip [IPSIZE],line [2046],path [255 ],

finalhost [HOSTSIZE + 1 ];

FILE * outstream;

struct in_addr iph;

struct hostent * hp;

int c;

信号(SIGCHLD,SIG_IGN);


cache = malloc(sizeof(struct cache_object [CACHESIZE]));

cachecount =(int *)malloc(sizeof(int));

* cachecount = 0;

while(!feof(stdin)){

memset(vhost,0,255);

memset(ip,0,255);

memset(line,0,2046);

memset( path,0,255);

memset(finalhost,0,255);

fscanf(stdin,"%s%s",vhost,ip);

fgets(line,2046,stdin);

for(c = 0; c< CACHESIZE; c ++){

printf(" cache:%i) ipaddr:%s主机名:

%s \ n",c,cache [c] - > ipaddr,cache [c] - > hostname);

}

// if(fork()== 0){

sprintf(path," / home / logs / access_logs /%s",vhost); <如果((ht = gethostbyaddr((const char *)& iph,

sizeof(struct in_addr),AF_INET))!= NULL){

sprintf(finalhost,"%s",hp-> h_name);

} else {

sprintf(finalhost,"%s",ip);

}

}其他{

sprintf (finalhost,"%s",ip);

}

sprintf(cache [* cachecount] - > ipaddr,"%s",ip);


sprintf(cache [* cachecount] - > hostname,"%s",finalhost);

if(* cachecount< CACHESIZE){

* cachecount = * cachecount + 1;

}否则{

* cachecount = 0;

}

outstream = fopen(path," a");

if(outstream!= NULL){

fprintf(outstream, "%s%s",finalhost,line);

fclose(outstream);

}

// exit(0);

//}


}

退出(0);

}



-

~Kieran Simkin

Digital Crocus
http://digital-crocus.com/
./caching_logger
foo 127.0.0.1 foo
cache: 0 ipaddr: hostname:
cache: 1 ipaddr: hostname:
cache: 2 ipaddr: hostname:
cache: 3 ipaddr: hostname:
cache: 4 ipaddr: hostname:

Any help with this would be very greatly appreciated, it''s been driving me
nuts all day.

Incase anyone''s interested, the code I''m working on sits on the end of a log
pipe from apache splitting out log lines for each vhost and logging them to
separate files, it also resolves IP addresses in the log lines to dns wait
time in apache itself. The main purpose of the program however is to
overcome apache''s open file descriptor limit by requiring only one pipe to
this program - the program itself closes and re-opens each log file between
writes; less efficient, but necessary in the situation I run.

Below is the code I have so far (fork commented out to remove the need for
shm):

#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>
#include <signal.h>

#define HOSTSIZE 255
#define IPSIZE 255
#define CACHESIZE 10

struct cache_object {
char hostname[HOSTSIZE+1];
char ipaddr[IPSIZE+1];
};

struct cache_object (*cache)[CACHESIZE];
int *cachecount;

int main () {
char vhost[HOSTSIZE+1], ip[IPSIZE], line[2046], path[255],
finalhost[HOSTSIZE+1];
FILE *outstream;
struct in_addr iph;
struct hostent *hp;
int c;
signal(SIGCHLD, SIG_IGN);

cache = malloc(sizeof(struct cache_object[CACHESIZE]));
cachecount = (int *) malloc(sizeof(int));
*cachecount=0;
while (!feof(stdin)) {
memset(vhost,0,255);
memset(ip,0,255);
memset(line,0,2046);
memset(path,0,255);
memset(finalhost,0,255);
fscanf(stdin, "%s %s ", vhost, ip);
fgets(line, 2046, stdin);
for (c=0;c<CACHESIZE;c++) {
printf("cache: %i ipaddr: %s hostname:
%s\n",c,cache[c]->ipaddr,cache[c]->hostname);
}
// if (fork() == 0) {
sprintf(path,"/home/logs/access_logs/%s",vhost);
if (inet_aton(ip, &iph)) {
if ((hp=gethostbyaddr((const char *)&iph,
sizeof(struct in_addr), AF_INET)) != NULL) {
sprintf(finalhost,"%s",hp->h_name);
} else {
sprintf(finalhost,"%s",ip);
}
} else {
sprintf(finalhost,"%s",ip);
}
sprintf(cache[*cachecount]->ipaddr,"%s",ip);

sprintf(cache[*cachecount]->hostname,"%s",finalhost);
if (*cachecount < CACHESIZE) {
*cachecount = *cachecount + 1;
} else {
*cachecount=0;
}
outstream = fopen(path, "a");
if (outstream != NULL) {
fprintf(outstream,"%s %s",finalhost, line);
fclose(outstream);
}
// exit(0);
// }

}
exit(0);
}


--
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/




Kieran Simkin <き**** @ digital-crocus.com>在消息中写道

news:b1pcc.470

"Kieran Simkin" <ki****@digital-crocus.com> wrote in message
news:b1pcc.470


Xi.6@newsfe1-win ...


无论如何,
Xi.6@newsfe1-win...

Anyway,
为了使用shm我需要一个指向我的结构数组的指针
cache_object。我已经定义了这样的指针:

struct cache_object(* cache)[CACHESIZE];


目前为止确定。

HOSTNAME / IPSIZE / CACHESIZE都是#defined并带有整数值。

现在我开始工作之前在shm代码上我正在用malloc()来测试东西,因为malloc将以与$ sh $()相同的方式返回指向一块内存的指针。 (实际上我写了一堆shm代码,无法让它工作并且
将其全部删除以试图回到问题的根源)。

问题我有,并且这发生在我的shm代码以及
,我似乎无法弄清楚我应该允许自己存储这个结构数组的内存量。这是我的代码:

cache = malloc(sizeof(struct cache_object [CACHESIZE]));


cache = malloc(sizeof * cache);


-Mike

作为旁注,我该怎么办?类型转换为?
in order to work with shm I need a pointer to an array of my struct
"cache_object". I''ve defined my pointer like this:

struct cache_object (*cache)[CACHESIZE];
OK so far.
HOSTNAME/IPSIZE/CACHESIZE are all #defined with integer values.

Now before I start work on the shm code I''m testing things with malloc()
because malloc will return a pointer to a piece of memory in the same way as shmat(). (actually I wrote a load of shm code, couldn''t get it to work and
removed it all in an attempt to get back to the root of the problem).

The problem I''m having, and this happened with my shm code as well was that I can''t seem to figure out how much memory I should be allowing myself to
store this array of structs. Here''s the code I''ve got:

cache = malloc(sizeof(struct cache_object[CACHESIZE]));
cache = malloc(sizeof *cache);

-Mike
as a sidenote, what should I typecast this to?




不要从malloc()转换返回值。只需

确保你已经#included< stdlib.h> for malloc()'s
原型。


-Mike



Don''t cast the return value from malloc(). Just
make sure you''ve #included <stdlib.h> for malloc()''s
prototype.

-Mike


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

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