我正在创建一个双指针并在地址中发送以分配内存,这需要一个三指针。此外,我正在创建单指针(目标和助手)并发送他们的地址来分配内存,这需要双指针。我认为问题在于内存分配,但我无法弄明白。每当我运行readLinesFromFile函数时,我都会出现seg faulting。当我尝试单独运行allocateMemory函数时,它不会出现段错误。问题也可能出在readLinesFromFile函数
中int main (int argc, char **argv)
{
int numPlayers = 0;
if (argc != 3)
{
printf("Missing text file");
return 0;
}
char **playerNames;
int *goals, *assists;
FILE *filePtr = fopen(argv[1],"r");
if(filePtr == NULL)
{
printf("\nFile is empty");
return 0;
}
numPlayers = countLinesInFile(filePtr);
allocateMemory(&goals,&assists,&playerNames,numPlayers);
readLinesFromFile(filePtr,goals,assists,playerNames,numPlayers);
}
void allocateMemory(int **goals, int **assists, char *** names, int size)
{
int i = 0;
*goals = malloc(MAX_NAME * sizeof(int));
*assists = malloc(MAX_NAME * sizeof(int));
*names = malloc(MAX_NAME * sizeof(char*));
for (i = 0; i < size; i++)
{
(names[i]) = malloc(MAX_NAME * sizeof(char*));
}
}
void readLinesFromFile(FILE *fptr, int *goals, int *assists, char **names, int numLines)
{
int i = 0, j = 0, x = 0;
char players[MAX_LINE];
char *tokenPtr;
fptr = fopen(INPUT,"r");
for(i = 0; i < numLines; i++)
{
fgets(players,MAX_LINE, fptr);
tokenPtr = strtok(players," ");
strcpy((*(names+i)), tokenPtr);
while (tokenPtr != NULL)
{
tokenPtr = strtok(NULL," ");
if (x = 0)
{
goals[i] = atoi(tokenPtr);
x = 1;
}
else
{
assists[i] = atoi(tokenPtr);
x = 0;
}
}
}
}
答案 0 :(得分:0)
假设MAX_NAME
是玩家姓名的最大长度,这应该有效:
void AllocateMemory(char *** pppNames, int ** ppGoals, int ** ppAssists, size_t sizePlayersMax)
{
*pppNames = malloc(sizePlayersMax * sizeof **pppNames);
*ppGoals = malloc(sizePlayersMax * sizeof **ppGoals);9
*ppAssists = malloc(sizePlayersMax * sizeof **ppAssists);
{
size_t sizePlayersCount = 0;0
for (; sizePlayersCount < sizePlayersMax; ++sizePlayersCount)
{
(*pppNames)[sizePlayersCount] = calloc(MAX_NAME + 1, sizeof *((*pppNames)[sizePlayersCount]));
}
}
}
为了防止应用程序在真正长的玩家名称的情况下覆盖内存,您可能想要更改此行:
strcpy((*(names+i)), tokenPtr);
成为:
if (tokenPtr)
strncpy((*(names+i)), tokenPtr, MAX_NAME);
这会截断存储的玩家名称,最多为MAX_NAME
个字符。后者的大小AllocateMemory()
使用减去1
。 0
/ NUL
需要这一个备用字符来终止字符数组,以便它可以用作“字符串”。
最后这也是危险的:
while (tokenPtr != NULL)
{
tokenPtr = strtok(NULL," ");
if (x = 0)
{
最好这样做:
while (NULL != (tokenPtr = strtok(NULL," ")))
{
if (x = 0)
{