Hiiiiii....always from Rome....and today the weather is a little
dirt...(like Alice in Chains....);however, i've a little script that
allocate memory for first time when i created the 'agenda':
naturally start is global an it was declared:
struct agenda
{
char acNome[20];
struct agenda *next;
};
typedef struct agenda lista_agenda;
lista_agenda *start=NULL;
void crea_agenda()
{
start=(lista_agenda *)malloc(sizeof(lista_agenda));
invokes clearly undefined behavior when the compiler misses the
prototype of malloc. Then it thinks malloc returns int - and that can
be returned in another was as pinters gets returned - so you converts
something unknown but not a boid* to a pointer to lists_agenda.
casting a return value void* to (all and anything) is ALWAYS and under
any circumstance a bug.
if(start!=NULL)
{
memset((char *)start->acNome,'\0',sizeof(start->acNome));
Using memset to fill anything else as an array of char (or a string)
is undefined behavior too. E.g. filling a pointer with binary 0 bytes
can be an access violation. There is no guarantee that a NULL pointer
has any bits set to 0. It may have padding bits set to nonzer0 to be a
valid pointer. You have NO chance to preset a struct with proper
values as to
a) copy a proper initialised struct (x = y)
or
b) initialise each member of the struct with the proper value.
scanf("%s",start->acNome);
guarantee for hackers to kontaminate your mashine with viruses of any
kind. Don't use scanf or buffer overflow ruins your mashine.
The error i receive when i try to add a new node it depends from the
malloc function i use, in particular:
newrecord=(lista_agenda *)malloc(sizeof(lista_agenda));
clearly undefined behavior - as the compiler has not seen the
prototype of malloc and you assigns something but not the value
returned from malloc to newrecord.
This line above is ok(from my gdb).
No - it's undefined behavior anyway.
Also because, (i think) i must allocate for the entire size of my struct
and not the size of a pointer to the struct.
(i'm not sure but if i execute a sizeof of a pointer a can see always 4
byte;maybe u'm wrong).
The line below give me an strange error from (gdb):
newrecord=(lista_agenda *)malloc(sizeof(lista_agenda*));
It is clearly different! As this will allocate only enough room for a
pointer, but not enough space for a whole struct.
you means clearly
newrecord = malloc(sizeof(*newrecord));
Use the size of the type the target pointer points to is the best way
you can go.
malloc(sizeof lista_agenda) would work too - but lets you change any
occurence of that whenever you changes the type name of that type.
1. #include always the system headers you needs!
2. never cast return values from malloc(), its family or self defined
functions returning void* - to get be warned when you have forgotten
to include the right headers.
3. tell the compiler to serve all, really all possible warnings
4. resolve any warning - but never by simply casting a warning away.
5. mostenly when you things you needs to cast a warning away you are
going on to hide but not resolve the mistake. Double check and recheck
double what you are doing. There is surely no need to cast!
6. whenever you things you needs a cast you are commonly wrong. You've
made another mistake. The simplest would be a missing #include
<something>.