分段错误(核心转储)C
Seguementation Fault (Core Dump) C
我遇到了分段错误问题。我知道这与记忆有关。
我做了一些更改,但它仍然存在错误。我正在尝试读取一个 25kb 的大文件。
我需要读取最大的 csv 并将其逐行放入结构中。我是用链表做的。
我也已经尝试过使用列表。
如果我在不拆分字符串的情况下读取文件,我没有错误。
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct gh_user {
char *id;
char *login;
char *type;
char *createdat;
char *followers;
char *followerlist;
char *following;
char *followinglist;
char *public_gists;
char *pub_repos;
struct gh_user *next;
};
typedef struct gh_user *GH_USER;
void separador (char *lines, GH_USER k) {
k->id = strdup(strsep(&lines, ";"));
k->login = strdup(strsep(&lines, ";"));
k->type = strdup(strsep(&lines, ";"));
k->createdat = strdup(strsep(&lines, ";"));
k->followers = strdup(strsep(&lines, ";"));
k->followerlist = strdup(strsep(&lines, ";"));
k->following = strdup(strsep(&lines, ";"));
k->followinglist = strdup(strsep(&lines, ";"));
k->public_gists = strdup(strsep(&lines, ";"));
k->pub_repos = strdup(strsep(&lines, ";"));
free(lines);
}
int conta_bots()
{
FILE *usr;
usr = fopen("../entrada/users.csv","rb");
int contador = 0;
char *lines = malloc(1024);
GH_USER k = malloc(sizeof(struct gh_user));
int fileSize = 0;
while (fgets(lines, 1024,usr)!=NULL)
{
if(lines != NULL)
{
separador (lines,k->next);
printf("%s", k->type);
}
}
free(k);
fclose(usr);
free(lines);
return contador;
}
int main()
{
conta_bots();
return 0;
}
没有数据很难说它到底在哪里崩溃了,但让我们尝试解决我在没有 运行 代码的情况下看到的问题:
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct gh_user {
char *id;
char *login;
char *type;
char *createdat;
char *followers;
char *followerlist;
char *following;
char *followinglist;
char *public_gists;
char *pub_repos;
struct gh_user *next;
};
char *n_strdup(char *in) {
return strdup(in ? in : "");
}
void separador (char *lines, struct gh_user *k) {
k->id = n_strdup(strsep(&lines, ";"));
k->login = n_strdup(strsep(&lines, ";"));
k->type = n_strdup(strsep(&lines, ";"));
k->createdat = n_strdup(strsep(&lines, ";"));
k->followers = n_strdup(strsep(&lines, ";"));
k->followerlist = n_strdup(strsep(&lines, ";"));
k->following = n_strdup(strsep(&lines, ";"));
k->followinglist = n_strdup(strsep(&lines, ";"));
k->public_gists = n_strdup(strsep(&lines, ";"));
k->pub_repos = n_strdup(strsep(&lines, ";"));
}
/*yes, you need to free all that*/
void gh_user_free(struct gh_user *ptr) {
if (!ptr) return;
free(ptr->id);
free(ptr->login);
free(ptr->type);
free(ptr->createdat);
free(ptr->followers);
free(ptr->followerlist);
free(ptr->following);
free(ptr->followinglist);
free(ptr->public_gists);
free(ptr->pub_repos);
free(ptr);
}
int conta_bots()
{
FILE *usr;
usr = fopen("../entrada/users.csv","rb");
int contador = 0;
char lines[1024];
while (fgets(lines, 1024,usr)!=NULL)
{
struct gh_user *k = calloc(1, sizeof(struct gh_user));
separador (lines,k);
printf("%s", k->type);
gh_user_free(k);
}
fclose(usr);
return contador; /*possibly that var was supposed to be updated in some way?*/
}
int main()
{
conta_bots();
return 0;
}
您的代码示例没有使用链表,所以我省略了它。
我遇到了分段错误问题。我知道这与记忆有关。 我做了一些更改,但它仍然存在错误。我正在尝试读取一个 25kb 的大文件。 我需要读取最大的 csv 并将其逐行放入结构中。我是用链表做的。 我也已经尝试过使用列表。 如果我在不拆分字符串的情况下读取文件,我没有错误。
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct gh_user {
char *id;
char *login;
char *type;
char *createdat;
char *followers;
char *followerlist;
char *following;
char *followinglist;
char *public_gists;
char *pub_repos;
struct gh_user *next;
};
typedef struct gh_user *GH_USER;
void separador (char *lines, GH_USER k) {
k->id = strdup(strsep(&lines, ";"));
k->login = strdup(strsep(&lines, ";"));
k->type = strdup(strsep(&lines, ";"));
k->createdat = strdup(strsep(&lines, ";"));
k->followers = strdup(strsep(&lines, ";"));
k->followerlist = strdup(strsep(&lines, ";"));
k->following = strdup(strsep(&lines, ";"));
k->followinglist = strdup(strsep(&lines, ";"));
k->public_gists = strdup(strsep(&lines, ";"));
k->pub_repos = strdup(strsep(&lines, ";"));
free(lines);
}
int conta_bots()
{
FILE *usr;
usr = fopen("../entrada/users.csv","rb");
int contador = 0;
char *lines = malloc(1024);
GH_USER k = malloc(sizeof(struct gh_user));
int fileSize = 0;
while (fgets(lines, 1024,usr)!=NULL)
{
if(lines != NULL)
{
separador (lines,k->next);
printf("%s", k->type);
}
}
free(k);
fclose(usr);
free(lines);
return contador;
}
int main()
{
conta_bots();
return 0;
}
没有数据很难说它到底在哪里崩溃了,但让我们尝试解决我在没有 运行 代码的情况下看到的问题:
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct gh_user {
char *id;
char *login;
char *type;
char *createdat;
char *followers;
char *followerlist;
char *following;
char *followinglist;
char *public_gists;
char *pub_repos;
struct gh_user *next;
};
char *n_strdup(char *in) {
return strdup(in ? in : "");
}
void separador (char *lines, struct gh_user *k) {
k->id = n_strdup(strsep(&lines, ";"));
k->login = n_strdup(strsep(&lines, ";"));
k->type = n_strdup(strsep(&lines, ";"));
k->createdat = n_strdup(strsep(&lines, ";"));
k->followers = n_strdup(strsep(&lines, ";"));
k->followerlist = n_strdup(strsep(&lines, ";"));
k->following = n_strdup(strsep(&lines, ";"));
k->followinglist = n_strdup(strsep(&lines, ";"));
k->public_gists = n_strdup(strsep(&lines, ";"));
k->pub_repos = n_strdup(strsep(&lines, ";"));
}
/*yes, you need to free all that*/
void gh_user_free(struct gh_user *ptr) {
if (!ptr) return;
free(ptr->id);
free(ptr->login);
free(ptr->type);
free(ptr->createdat);
free(ptr->followers);
free(ptr->followerlist);
free(ptr->following);
free(ptr->followinglist);
free(ptr->public_gists);
free(ptr->pub_repos);
free(ptr);
}
int conta_bots()
{
FILE *usr;
usr = fopen("../entrada/users.csv","rb");
int contador = 0;
char lines[1024];
while (fgets(lines, 1024,usr)!=NULL)
{
struct gh_user *k = calloc(1, sizeof(struct gh_user));
separador (lines,k);
printf("%s", k->type);
gh_user_free(k);
}
fclose(usr);
return contador; /*possibly that var was supposed to be updated in some way?*/
}
int main()
{
conta_bots();
return 0;
}
您的代码示例没有使用链表,所以我省略了它。