我在内存分配/释放方面遇到了一些麻烦。
在“64 位机器”上,我运行了一些没有出现错误的 C 代码。 如果我在 32 位机器上运行相同的代码(在重新编译之后,很明显。)我会遇到一些段错误问题。
这是我的 glib-c 版本:
Package: libc6
New: yes State: installed Automatically installed: no Version: 2.13-20ubuntu5 Priority: required Section: libs Maintainer: Ubuntu Developers Uncompressed Size: 10,7 M Depends: libc-bin (= 2.13-20ubuntu5), libgcc1, tzdata Suggests: glibc-doc, debconf | debconf-2.0, locales Conflicts: 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 Breaks: nscd (< 2.13), nscd (< 2.13), libc6 (!= 2.13-20ubuntu5) Replaces: belocs-locales-bin, belocs-locales-bin, libc6-amd64, libc6-amd64, libc6 (< 2.13-20ubuntu5) Provides: glibc-2.13-1 Description: libreria C GNU embedded: 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);
}
有人有想法吗?
我的第一个是关于 glib-c 版本,但现在我可以了'
t 到达 32 位机器并“更新”该库。那么,有人可以称呼我吗?
编辑: 明确一点,段错误出现在 free(ptr_buf) 之前
最佳答案
libroLetto->titolo = (char*) malloc (lung*sizeof(char));
memset (libroLetto->titolo, 0, sizeof(libroLetto->titolo));
sizeof(libroLetto->titolo)
是指针成员的大小,而不是分配的对象。
你想要的是:memset(libroLetto->titolo, 0, lung)
您可能还想在每次调用 getline
函数时检查它的返回值,否则 strlen
可能会给您带来意想不到的结果。
最后但同样重要的是:
lung = strlen (ptr_buf);
libroLetto->autore = (char*) malloc (lung*sizeof(char));
您没有为尾随的空字符分配空间,您想要的是 malloc(lung + 1)
关于c - 段错误 - 不同的 glib-c 版本可以做类似的事情吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9453398/