3

I want to create and use something like :

struct mystruct { 
    mystruct * AnOtherMyStruct; //mystruct must have a pointer to an other mystruct inside
    ...some stuffs...  
};

Should I use "typedef struct" or not? Which are the pros and cons ?

When I allocate memory some told me that calloc is better than malloc because it does the same job but better...

My results will be something like :

struct (or typedef struct) mystruct  {
    mystruct * AnOtherMyStruct;
    ...
};
int main () {

malloc or calloc =...
free(...);

What do you think, which is the best choice to allocate and deallocate structs considering that these operations will be done quite frequently?

feded
  • 109
  • 2
  • 13
  • 1
    *"it does the same job but better."* - did you ask that someone *why* they claim this? I only ask because it would seem easier for them to justify their conclusion than for us to assume we successfully climbed into their head. Each function has merit. One (`calloc`) is better suited syntactically for sequence allocation with the added feature of zero-filling the allocated block. Whether that is a *beneficial* feature for your use was is something only you can decide. – WhozCraig Nov 27 '19 at 11:17
  • 4
    Please don't ask 2 totally unrelated questions at once. Please [edit] your question and focus only on one thing. – user694733 Nov 27 '19 at 11:23
  • user694733 I would like to know which is the best way to create and define structs considering that I'll create and free these structs very often. That's why I'm asking pros and cons... – feded Nov 27 '19 at 11:36
  • `Should I use "typedef struct" or not?` a duplicate could be https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c but for `What do you think, which is the best choice` a duplicate could be https://stackoverflow.com/questions/1538420/difference-between-malloc-and-calloc . The `which is the best choice` is opinion based - the best choice using what measure to compare? Speed of execution? Writing speed? Writer sanity? – KamilCuk Nov 27 '19 at 11:42
  • Thank you KamilCuk, I found good stuffs I your links. – feded Nov 27 '19 at 11:45

2 Answers2

3

calloc() and malloc() are almost the same.

calloc() initializes the claimed memory with 0s.

when you call malloc() and try to access members of the allocated type, you will see garbage values (for int for example you will see values like 347289472) while with calloc() you will see 0s as initial values

Raildex
  • 3,406
  • 1
  • 18
  • 42
3

Should I use "typedef struct" or not? Which are the pros and cons ?

It's just subjective coding style. The most common style is to use a typedef. One advantage is that it makes the code behave just like C++. Typing out struct name explicitly is ok too, a style favoured by Linux. Neither style is obviously right or wrong.

However, for self-referencing structs then regardless of style you must always write the pointer member with struct notation.

typedef struct foo
{
  struct foo* next;
} foo_t;

foo_t* next won't work because the typedef is not complete at that point. foo however is a "struct tag", not a type name, so it follows different rules. If you wish to use the typedef name internally, then you have to forward-declare the struct:

typedef struct foo foo_t;  // forward declaration
struct foo
{
  foo_t* next;
};

When I allocate memory some told me that calloc is better than malloc because it does the same job but better...

When someone tells you that, ask them how exactly is calloc any better. When programming, critical thinking is very important.

  • malloc allocates a chunk of memory but does not initialize it to known values. Since there is no initialization, malloc is faster than calloc.
  • calloc allocates a chunk of memory, or optionally an array of chunks placed in adjacent memory cells. It zero-initializes all memory, meaning that it has known default values. Because of this, it is slower than malloc.

So which is "better", faster or pre-initialized? Depends on what you want to do.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    @feded Why would you want to use the very same name for the type and the variable? That's just confusing - don't write confusing code. – Lundin Nov 27 '19 at 12:57
  • ok I got it : typedef struct node MyVar is type node name myvar right? – feded Nov 27 '19 at 13:00