3
I’m learning C from the book "Use Your Head! C" (damn me).
Given a lesson, I need to create a struct
calling for "island" with the following code:
typedef struct island {
const char *name;
const char *opens;
const char *closes;
struct island *next;
} island;
After a series of changes I follow in the book, it’s time to write a method to create a island
, allocating memory dynamically with malloc
to then return the pointer of the created object. Below the code of this function:
island* create(char *name)
{
island *i = malloc(sizeof(island));
i->name = strdup(name);
i->opens = "09:00";
i->closes = "17:00";
i->next = NULL;
return i;
}
Finally, and where is my problem, the book guides you to free up the memory you have allocated so as not to leave it with "trash", using a method release
:
void release(island *start)
{
island *i = start;
island *next = NULL;
for(; i != NULL; i = next) {
next = i->next;
free(i->name);
free(i);
}
}
While trying to pass a struct
for this function, I get the following errors:
memoriaDinamica.c: In function ‘release’:
memoriaDinamica.c:83:14: warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
free(i->name);
^
In file included from memoriaDinamica.c:2:0:
/usr/include/stdlib.h:483:13: note: expected ‘void *’ but argument is of type ‘const char *’
extern void free (void *__ptr) __THROW;
^
*** Error in `./memoriaDinamica': free(): invalid pointer: 0x0000000000400940 ***
Name: Amity open: 09:00-17:00
Name: Craggy open: 09:00-17:00
Name: Isla Nublar open: 09:00-17:00
Name: Skull open: 09:00-17:00
The newly created island:
Name: Hawaii open: 09:00-17:00
Name: Skull open: 09:00-17:00
======= Backtrace: =========
/lib64/libc.so.6(+0x77a8d)[0x7fee806c1a8d]
/lib64/libc.so.6(cfree+0x5cd)[0x7fee806cdd2d]
./memoriaDinamica[0x400886]
./memoriaDinamica[0x400791]
/lib64/libc.so.6(__libc_start_main+0xf0)[0x7fee8066a700]
./memoriaDinamica[0x400589]
======= Memory map: ========
00400000-00401000 r-xp 00000000 fd:02 2097225 memoria-dinamica/memoriaDinamica
00600000-00601000 r--p 00000000 fd:02 2097225 memoria-dinamica/memoriaDinamica
00601000-00602000 rw-p 00001000 fd:02 2097225 memoria-dinamica/memoriaDinamica
02248000-02269000 rw-p 00000000 00:00 0 [heap]
7fee80433000-7fee80449000 r-xp 00000000 fd:01 1049963 /usr/lib64/libgcc_s-5.1.1-20150618.so.1
7fee80449000-7fee80648000 ---p 00016000 fd:01 1049963 /usr/lib64/libgcc_s-5.1.1-20150618.so.1
7fee80648000-7fee80649000 r--p 00015000 fd:01 1049963 /usr/lib64/libgcc_s-5.1.1-20150618.so.1
7fee80649000-7fee8064a000 rw-p 00016000 fd:01 1049963 /usr/lib64/libgcc_s-5.1.1-20150618.so.1
7fee8064a000-7fee80801000 r-xp 00000000 fd:01 1054187 /usr/lib64/libc-2.21.so
7fee80801000-7fee80a00000 ---p 001b7000 fd:01 1054187 /usr/lib64/libc-2.21.so
7fee80a00000-7fee80a04000 r--p 001b6000 fd:01 1054187 /usr/lib64/libc-2.21.so
7fee80a04000-7fee80a06000 rw-p 001ba000 fd:01 1054187 /usr/lib64/libc-2.21.so
7fee80a06000-7fee80a0a000 rw-p 00000000 00:00 0
7fee80a0a000-7fee80a2b000 r-xp 00000000 fd:01 1054235 /usr/lib64/ld-2.21.so
7fee80c05000-7fee80c08000 rw-p 00000000 00:00 0
7fee80c27000-7fee80c2a000 rw-p 00000000 00:00 0
7fee80c2a000-7fee80c2b000 r--p 00020000 fd:01 1054235 /usr/lib64/ld-2.21.so
7fee80c2b000-7fee80c2c000 rw-p 00021000 fd:01 1054235 /usr/lib64/ld-2.21.so
7fee80c2c000-7fee80c2d000 rw-p 00000000 00:00 0
7ffc4d52b000-7ffc4d54c000 rw-p 00000000 00:00 0 [stack]
7ffc4d550000-7ffc4d552000 r--p 00000000 00:00 0 [vvar]
7ffc4d552000-7ffc4d554000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
/bin/sh: line 1: 23871 Abortado (imagem do núcleo gravada)./memoriaDinamica
I understand that a reference to a constant cannot be released for obvious reasons, so I should really worry about cleaning up this allocated memory in cases like this example?
You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Something needs to be improved?
– Maniero