c中线程程序的valgrind printf()错误
valgrind printf() error with thread program in c
所以我用 c 编写了这个程序,其想法是它从 3 个文件中读取并显示其中的内容。它工作正常,但是当我使用 valgrind 运行 时出现错误。
这是程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
struct structFile{
int numFile;
char* fileName;
};
void* fileThread(void* arg){
struct structFile* threadStruct = (struct structFile*) arg;
FILE *file = fopen(threadStruct->fileName, "r");`
//char* charS = (char*) malloc(sizeof(char)*10);`
char *charS = malloc(10 + 1);
int sizeC = fread(charS, 1, 10, file);
charS[sizeC] = '[=10=]';
while (sizeC < 10){
charS[sizeC] = ' ';
sizeC++;
}//while
fclose(file);
printf("%d. nit: %s\n", threadStruct->numFile, charS);
return charS;
}//fileThread
int main(int argc, char **argv){
pthread_t id[3];
char* word[3];
struct structFile* mainStruct = (struct structFile*) malloc (sizeof(struct structFile)*3);
for (size_t i = 0; i < 3; i++){
mainStruct[i].numFile = i;
mainStruct[i].fileName = (char*)argv[i+1];
pthread_create(&id[i], NULL, fileThread, mainStruct+i);
}//for
for (size_t i = 0; i < 3; i++){
pthread_join(id[i], (void**) &word[i]);
}//for
printf("Sporocilo-> %s %s %s\n", word[0], word[1], word[2]);
free(mainStruct);
free(word[0]);
free(word[1]);
free(word[2]);
return 0;
}//main
我真的不知道听到什么问题,有时显示 7 个错误,有时显示 4 个错误。我大约 1 个月前开始使用 c 编程,所以它可能只是一个简单的答案,所以提前感谢您的帮助。
问题是 charS
没有 space 空字节(因为您想使用 %s
作为字符串打印)。
分配一个额外的字节并正确地用 null 终止它。
char *charS = malloc(10 + 1); // sizeof(char) can be omitted as it's always 1
int sizeC = fread(charS, 1, 10, file);
charS[sizeC] = '[=10=]';
...
您还应该为 fopen
、malloc
、pthread_create
等添加错误检查。
fread 读取了10 个字符,因此您需要为'\0' 分配11 个字符。
像这样的 Malloc :
char* charS = malloc(sizeof(char) * 11);`
所以我用 c 编写了这个程序,其想法是它从 3 个文件中读取并显示其中的内容。它工作正常,但是当我使用 valgrind 运行 时出现错误。 这是程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
struct structFile{
int numFile;
char* fileName;
};
void* fileThread(void* arg){
struct structFile* threadStruct = (struct structFile*) arg;
FILE *file = fopen(threadStruct->fileName, "r");`
//char* charS = (char*) malloc(sizeof(char)*10);`
char *charS = malloc(10 + 1);
int sizeC = fread(charS, 1, 10, file);
charS[sizeC] = '[=10=]';
while (sizeC < 10){
charS[sizeC] = ' ';
sizeC++;
}//while
fclose(file);
printf("%d. nit: %s\n", threadStruct->numFile, charS);
return charS;
}//fileThread
int main(int argc, char **argv){
pthread_t id[3];
char* word[3];
struct structFile* mainStruct = (struct structFile*) malloc (sizeof(struct structFile)*3);
for (size_t i = 0; i < 3; i++){
mainStruct[i].numFile = i;
mainStruct[i].fileName = (char*)argv[i+1];
pthread_create(&id[i], NULL, fileThread, mainStruct+i);
}//for
for (size_t i = 0; i < 3; i++){
pthread_join(id[i], (void**) &word[i]);
}//for
printf("Sporocilo-> %s %s %s\n", word[0], word[1], word[2]);
free(mainStruct);
free(word[0]);
free(word[1]);
free(word[2]);
return 0;
}//main
问题是 charS
没有 space 空字节(因为您想使用 %s
作为字符串打印)。
分配一个额外的字节并正确地用 null 终止它。
char *charS = malloc(10 + 1); // sizeof(char) can be omitted as it's always 1
int sizeC = fread(charS, 1, 10, file);
charS[sizeC] = '[=10=]';
...
您还应该为 fopen
、malloc
、pthread_create
等添加错误检查。
fread 读取了10 个字符,因此您需要为'\0' 分配11 个字符。 像这样的 Malloc :
char* charS = malloc(sizeof(char) * 11);`