分段错误 - 不同的glib-c版本可以做类似的事吗?

Don*_*sto 0 c glibc segmentation-fault

我在内存分配/释放方面遇到了一些麻烦.

在"64位机器"上,我运行了一些没有错误的c代码.如果我在32位机器上运行相同的代码(重新编译后,obv.)我有一些分段故障的麻烦.

这是我的glib-c版本:

包:libc6
新增:是状态:已安装自动安装:无版本:2.13-20ubuntu5优先级:必需部分:libs维护者:Ubuntu开发人员未压缩大小:10,7 M取决于:libc-bin(= 2.13-20ubuntu5),libgcc1,tzdata建议:glibc-doc,debconf | debconf-2.0,locales冲突:belocs-locales-bin,belocs-locales-bin,libc6-amd64,libc6-amd64,prelink(<0.0.20090925),prelink(<0.0.20090925),tzdata(<2007k-1) ,tzdata(<2007k-1),tzdata-etch,tzdata-etch中断:nscd(<2.13),nscd(<2.13),libc6(!= 2.13-20ubuntu5)替换:belocs-locales-bin,belocs-locales- bin,libc6-amd64,libc6-amd64,libc6(<2.13-20ubuntu5)提供:glibc-2.13-1描述:libreria C GNU嵌入式:librerie condivise

在32位机器上,版本是:

2.06-01

更重要的是,这里有一些令我疯狂的代码片段:

void estrai_libro (FILE* fileDescriptor, Libro* libroLetto) {

char* ptr_buf;
size_t n;
size_t lung;
ssize_t nread;

/* ---- questo blocco di istruzioni verrà utilizzato per tutti
    i campi della struttura Libro passata in input */
/* inizializzo ptr_buf e n rispettivamente a NULL e a 0 in moda da
   da sfruttare al meglio la getline(...)*/     
ptr_buf = NULL;
n = 0;
/* copio all'interno del buffer ptr_buf il contenuto di una riga
       del file; la funzione getline legge dal file fino a quando non
   incontra uno \n (ecco perchè io formatto gli input) */
nread = getline (&ptr_buf, &n, fileDescriptor);
/* calcolo la lunghezza della stringa letta */
lung = strlen (ptr_buf);
/* istanzio una zona di memoria della stessa dimensione della stringa
   letta e fatta puntare dal campo titolo della struttura Libro */
libroLetto->titolo = (char*) malloc (lung*sizeof(char));
/* inizializzo la zona di memoria istanziata con degli 0 */
memset (libroLetto->titolo, 0, sizeof(libroLetto->titolo));
/* copio la stringa letta e contenuta in ptr_buf nel campo titolo
   della struttura Libro passata in input (libroLetto). */
strcpy (libroLetto->titolo,ptr_buf);
free (ptr_buf);
/* ---- fine blocco*/
ptr_buf = NULL;
n = 0;
nread = getline (&ptr_buf, &n, fileDescriptor);
lung = strlen (ptr_buf);
libroLetto->autore = (char*) malloc (lung*sizeof(char));
memset (libroLetto->autore, 0, sizeof(libroLetto->autore));
strcpy (libroLetto->autore,ptr_buf);
free (ptr_buf);
ptr_buf = NULL;
n = 0;
nread = getline (&ptr_buf, &n, fileDescriptor);
lung = strlen (ptr_buf);
libroLetto->editore = (char*) malloc (lung*sizeof(char));
memset (libroLetto->editore, 0, sizeof(libroLetto->editore));
strcpy (libroLetto->editore,ptr_buf);
free (ptr_buf);
ptr_buf = NULL;
n = 0;
nread = getline (&ptr_buf, &n, fileDescriptor);
lung = strlen (ptr_buf);
libroLetto->data_pubblicazione = (char*) malloc (lung*sizeof(char));
memset (libroLetto->data_pubblicazione, 0, sizeof(libroLetto->data_pubblicazione));
strcpy (libroLetto->data_pubblicazione,ptr_buf);
free (ptr_buf);
ptr_buf = NULL;
n = 0;
nread = getline (&ptr_buf, &n, fileDescriptor);
lung = strlen (ptr_buf);
libroLetto->num_pagine = (char*) malloc (lung*sizeof(char));
memset (libroLetto->num_pagine, 0, sizeof(libroLetto->num_pagine));
strcpy (libroLetto->num_pagine,ptr_buf);
free (ptr_buf); 
ptr_buf = NULL;
n = 0;
nread = getline (&ptr_buf, &n, fileDescriptor);
lung = strlen (ptr_buf);
libroLetto->num_copie = (char*) malloc (lung*sizeof(char));
memset (libroLetto->num_copie, 0, sizeof(libroLetto->num_copie));
strcpy (libroLetto->num_copie,ptr_buf);
free (ptr_buf);
ptr_buf = NULL;
n = 0;
nread = getline (&ptr_buf, &n, fileDescriptor);
free (ptr_buf);
}
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

我的第一个是关于glib-c版本,但现在我可以'

达到32位机器并"更新"该lib.那么,有人可以解答我吗?

编辑:为了清楚,分段错误在free之前出现(ptr_buf)

oua*_*uah 6

 libroLetto->titolo = (char*) malloc (lung*sizeof(char));
 memset (libroLetto->titolo, 0, sizeof(libroLetto->titolo));
Run Code Online (Sandbox Code Playgroud)

sizeof(libroLetto->titolo)是指针成员的大小,而不是分配的对象.你想要的是:memset(libroLetto->titolo, 0, lung)

您可能还想在getline每次调用时检查函数的返回值,否则strlen可能会得到意外的结果.

最后但并非最不重要的:

lung = strlen (ptr_buf);
libroLetto->autore = (char*) malloc (lung*sizeof(char));
Run Code Online (Sandbox Code Playgroud)

你没有为尾随空字符分配空间,你想要的是什么 malloc(lung + 1)