[英]Segmentation fault using strsep
我正在嘗試使用strsep
刪除 CSV 文件中的額外字符。 問題是當我運行它時,它給了我分段錯誤,我不知道為什么。 這是代碼:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
typedef struct {
int id, followers, following, public_gists;
int public_repos;
char *login;
char *type;
char *created_at;
int *follower_list;
int *following_list;
} *User;
void checkUsersFile();
FILE *createCSV();
void corrigirFicheiro();
User criarUser();
int count = 0;
void checkUsersFile() {
//Ficheiro "users.csv"
FILE *file = fopen("ficheirosG1/users-set2.csv", "r");
//Verifica se o ficheiro "users.csv" existe
if(!file) {
printf("Ficheiro não encontrado");
return;
}
//Cria ficheiro "users-ok.csv"
FILE *newFile = createCSV("users-ok.csv");
corrigirFicheiro(file, newFile);
printf("%d\n", count);
}
//Cria e retorna ficheiro "users-ok.csv"
FILE *createCSV(char *nome) {
FILE *file = fopen(nome, "w");
return file;
}
//Função responsável por intrepretar o ficheiro "users.csv" e colocar os dados corretos no ficheiro "users-ok.csv"
void corrigirFicheiro(FILE *file, FILE *newFile) {
//imprimirPrimeiraLinha(file, newFile);
char string[200000];
//Uma linha do ficheiro com, no máximo, 200.000 caracteres
while ((fgets(string, 200000, file))) {
if (string[0] != '\0') {
//1. Criar user
//2. Print user
User user = criarUser(&string);
if (user != NULL) {
printf("ok\n");
}
free(user);
}
}
//free(string);
}
//Cria um User a partir de uma linha do ficheiro
User criarUser(char *str) {
User novoUser;
novoUser = (User) malloc(sizeof(User));
for(int i = 0; i<10; i++) {
//char *a = strdup(strsep(&str, ";"));
//char *b = strdup(strsep(&a, "\n"));
char *p = strsep(&str, ";\n\r");
if (strlen(p) == 0) {
count++;
free(novoUser);
return NULL;
}
}
return novoUser;
}
int main(){
checkUsersFile();
return 0;
}
使用gdb調試代碼,它說它出現在if(strlen(p) == 0 {
所以它甚至沒有進入switch case。我不知道為什么會發生這種情況。
謝謝
我認為沒有理由認為strsep()
調用對您遇到的錯誤負責。
然而這是錯誤的:
User novoUser = (User) malloc(sizeof(User));
它很可能要為您的錯誤負責。
User
是指針類型,所以sizeof(User)
是指針的大小,對於User
指向的那種結構來說不夠大。 當您稍后嘗試分配給它指向的結構成員(省略)或在printUser()
訪問它們(也省略)時,您將超出分配對象的邊界。 這正是可能導致段錯誤的那種事情。
表達分配的一個很好的習慣用法,例如使用接收變量來確定要分配的空間量:
User novoUser = malloc(sizeof(*novoUser));
請注意,我還刪除了不需要的演員表。
然而,正如我在評論中所表達的那樣,將指針性質隱藏在typedef
后面是一種糟糕的風格,就像您的User
那樣,而且就我個人而言,即使對於大多數避免該陷阱的typedef
s,我也不太在意。
以下是使用更好的 typedef 的方法:
typedef struct {
int id, followers, following, public_gists;
int public_repos;
char *login;
char *type;
char *created_at;
int *follower_list;
int *following_list;
} User; // not a pointer
// ...
User *criarUser(char *str) {
// ...
User *novoUser = malloc(sizeof(*novoUser)); // Note: no change on the right-hand side
// ...
return novoUser;
}
但這就是我要做的,沒有 typedef:
struct user {
int id, followers, following, public_gists;
int public_repos;
char *login;
char *type;
char *created_at;
int *follower_list;
int *following_list;
};
// ...
struct user *criarUser(char *str) {
// ...
struct user *novoUser = malloc(sizeof(*novoUser)); // still no change on the right-hand side
// ...
return novoUser;
}
為了回答你的問題,我寫了一個簡短的例子來說明需要什么。 從本質上講,你需要為字符串分配存儲和地址傳遞string
到criarUser()
您不能使用數組,因為傳遞給criarUser()
的類型是指向數組的指針而不是指向指針的指針。 (注意:您可以使用數組,只要它允許衰減為指針,因此取地址不會導致指向數組的指針 - 下面的示例)
corrigirFicheiro()
的(關閉)示例需要更改以用於分配的存儲:
void corrigirFicheiro(FILE *file, FILE *newFile)
{
char *string = malloc (200000); /* allocate for string */
/* valdiate allocation here */
char *original_ptr = string; /* save original pointer */
while ((fgets(string, 200000, file))) {
if (string[0] != '\0') {
User *user = criarUser(&string); /* pass address of string */
if (user != NULL) {
printUser(user, newFile);
}
free(user);
}
}
free (original_ptr); /* free original pointer */
}
以下示例中使用了縮寫結構,但主體相同。 對於示例輸入,您可以簡單地從printf
管道幾行並在stdin
讀取並寫入stdout
。 我用了:
$ printf "one;two;3\nfour;five;6\n" | ./bin/strsep_example
一個簡短的 MCVE(我已經冒昧刪除了 typedef'ed 指針)將是
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXC 128
#define DELIM ";\r\n"
#define NFIELDS 3
typedef struct {
char login[MAXC];
char type[MAXC];
int public_repos;
} User;
void printUser (User *user, FILE *newFile)
{
fprintf (newFile, "\nlogin : %s\ntype : %s\nrepo : %d\n",
user->login, user->type, user->public_repos);
}
User *criarUser(char **str)
{
User *novoUser = malloc(sizeof *novoUser);
/* validate allocation here */
for(int i = 0; i<NFIELDS; i++) {
char *p = strsep(str, DELIM);
switch (i) {
case 0: strcpy (novoUser->login, p);
break;
case 1: strcpy (novoUser->type, p);
break;
case 2: novoUser->public_repos = atoi(p);
break;
}
if (strlen(p) == 0) {
free(novoUser);
return NULL;
}
}
return novoUser;
}
void corrigirFicheiro(FILE *file, FILE *newFile)
{
char *string = malloc (200000); /* allocate for string */
/* valdiate allocation here */
char *original_ptr = string; /* save original pointer */
while ((fgets(string, 200000, file))) {
if (string[0] != '\0') {
User *user = criarUser(&string); /* pass address of string */
if (user != NULL) {
printUser(user, newFile);
}
free(user);
}
}
free (original_ptr); /* free original pointer */
}
int main (int argc, char **argv) {
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
corrigirFicheiro(fp, stdout);
if (fp != stdin) /* close file if not stdin */
fclose (fp);
}
示例使用/輸出
$ printf "one;two;3\nfour;five;6\n" | ./bin/strsep_example
login : one
type : two
repo : 3
login : four
type : five
repo : 6
內存使用/錯誤檢查
在你寫的,可動態分配內存的任何代碼,您有任何關於分配的內存任何塊2個職責:(1)始終保持一個指針的起始地址的存儲器中,以便塊,(2),當它是沒有它可以被釋放不再需要。
您必須使用內存錯誤檢查程序來確保您不會嘗試訪問內存或寫入超出/超出分配塊的范圍,嘗試讀取或基於未初始化值的條件跳轉,最后確認你釋放了你分配的所有內存。
對於 Linux valgrind
是正常的選擇。 每個平台都有類似的內存檢查器。 它們都易於使用,只需通過它運行您的程序即可。
$ printf "one;two;3\nfour;five;6\n" | valgrind ./bin/strsep_example
==6411== Memcheck, a memory error detector
==6411== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6411== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6411== Command: ./bin/strsep_example
==6411==
login : one
type : two
repo : 3
login : four
type : five
repo : 6
==6411==
==6411== HEAP SUMMARY:
==6411== in use at exit: 0 bytes in 0 blocks
==6411== total heap usage: 5 allocs, 5 frees, 205,640 bytes allocated
==6411==
==6411== All heap blocks were freed -- no leaks are possible
==6411==
==6411== For counts of detected and suppressed errors, rerun with: -v
==6411== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
始終確認您已釋放所有分配的內存並且沒有內存錯誤。
仔細檢查一下,如果您有任何問題,請告訴我。
使用數組
當您將數組作為參數傳遞時,它會通過數組/指針轉換衰減為指針。 如果這樣做並且“數組”類型不再與指針相關聯,那么您可以獲取地址並使用strsep()
指出的帶有strsep()
自動存儲。
對上述程序的更改將是:
User *criarUser(char *str)
{
User *novoUser = malloc(sizeof *novoUser);
/* validate allocation here */
for(int i = 0; i<NFIELDS; i++) {
char *p = strsep(&str, DELIM);
switch (i) {
case 0: strcpy (novoUser->login, p);
break;
case 1: strcpy (novoUser->type, p);
break;
case 2: novoUser->public_repos = atoi(p);
break;
}
if (strlen(p) == 0) {
free(novoUser);
return NULL;
}
}
return novoUser;
}
void corrigirFicheiro(FILE *file, FILE *newFile)
{
char string[200000];
while ((fgets(string, 200000, file))) {
if (string[0] != '\0') {
User *user = criarUser(string); /* pass string, as pointer */
if (user != NULL) {
printUser(user, newFile);
}
free(user);
}
}
}
但請注意,在將string
作為參數傳遞時,如果沒有數組/指針轉換的好處,則必須使用已分配的存儲空間。 由你決定,但要知道警告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.