【发布时间】:2014-04-17 09:29:54
【问题描述】:
我有一个输入文件 .txt,其中有序列:
NAMEOFSEQUENCE1/SEQUENCE1
NAMEOFSEQUENCE2/SEQUENCE2
NAMEOFSEQUENCE3/SEQUENCE3
我做了一个结构:
typedef struct lane{
char *name;
char *sequence;
}lane;
并写了这段代码:
int i=0;
lane* toSend = malloc(sizeof(*toSend)*3);
while (fgets(line,strlen(line),fileinput) != NULL){
//GETTING NAME AND SEQUENCE, LINE PER LINE
char *tempName = malloc(strlen(line)-strlen(strstr(line,"\\"))+1);
strncpy(tempName,line,strlen(line)-strlen(strstr(line,"\\")));
tempName[strlen(line)-strlen(strstr(line,"\\"))] = '\0';
char *tempSequence = malloc(strlen(strstr(line,"\\")));
strncpy(tempSequence,strstr(line,"\\")+1,strlen(strstr(line,"\\")));
tempSequence[strlen(strstr(line,"\\"))-1] = '\0';
//FILLING TOSEND
toSend[i].name = malloc(strlen(line)-strlen(strstr(line,"\\"))+1);
toSend[i].sequence = malloc(strlen(strstr(line,"\\")));
howmuchbyte += strlen(line)+1;
strcpy(toSend[i].name,tempName);
strcpy(toSend[i].sequence,tempSequence);
i++;
}
我一次将文件的一行放入“line”变量中,并将每个序列的NAMEOFSEQUENCEX 放入tempName,并将SEQUENCEX 放入tempSequence。
此时一切正常。如果我打印“toSend”向量,我会得到正确的值! 所以我写了这个:
MPI_Send(toSend, 3, MPI_BYTE, 1, tag, MPI_COMM_WORLD);
所以我从排名为 0 的进程发送到排名为 1 的进程(我有 2 个进程)。我将 3 作为计数参数,因为我在数组中有 3 个元素。
排名为 1 的进程执行以下操作:
lane* received = malloc(sizeof(*received)*3);
MPI_Recv(received, 3, MPI_BYTE, 0, tag, MPI_COMM_WORLD, &status);
如果我在排名为 1 的进程上执行此操作:
printf("%s",received[0].name);
我遇到了分段错误。我怎么了?
【问题讨论】:
-
sizeof(lane) * 3 ?而不仅仅是 3 个?
-
刚试过。另一个分段错误