Reputation:
I'm following this tutorial on learning C, and it says the following:
Let's assume we want to dynamically allocate a person structure. The person is defined like this:
typedef struct { char * name; char age; } person;
To allocate a new person in the myperson argument, we use the following syntax:
person * myperson = malloc(sizeof(person));
Why do they do it that way? What benefit does malloc
bring?
The following code compiles and seemingly works totally fine:
typedef struct {
char * name;
char age;
} person;
person p;
p.name = "Henry";
p.age = 9;
printf("%s is %d", p.name, p.age);
I'm confused why I would want to use malloc
.
Upvotes: 2
Views: 266
Reputation: 398
Malloc is used to Allocate memory at run time. If you are not sureof exact count of persons, then this can be determined at run time and suitable memory can be allocated for it.
malloc allocates memory contiguously... ie., you can do pointer arithmetic
When you want to deallocate the memory, you can give free() and this deallocates the entire block of memory irrespective of the size being too large or small.
also note that malloc doesn't have any default value and so by default it contains garbage value... if you want to set default value to zero, then use calloc instead of malloc.
If there is no contiguous space available then malloc returns null value and so memory can't be allocated in such a case
Upvotes: 0
Reputation: 6831
Another way to look at this is, why use a pointer to my struct rather than just the struct
, because when you think malloc()
, you think pointers. One benefit of using pointers, and thus malloc()
, is overhead when calling functions. When your data structures become more complex, their overhead will increase when you pass them to functions as arguments. A way to counteract this is to just pass a pointer to your struct and then have it de-refereced in the function.
void printfStruct(struct myStruct)
{
printf("%s", myStruct.name);
// no pointer passed, overhead created on larger data structures
}
void printfStruct(struct *myStruct)
{
printf("%s", myStruct->name);
// pointer passed, then dereferenced. Decreased overhead on larger data structures.
}
Upvotes: 0
Reputation: 33046
malloc
is used in 3 scenarios at least:
char
strings.But it is by no means necessary for programming.
Granted, many things will be more difficult to achieve without it, but nevertheless it is not strictly necessary. In fact, some coding styles for special applications (like DO-178B for avionics) actually forbid its usage.
Upvotes: 3
Reputation: 749
The example provided doesn't really show the full capabilities of 'malloc', simple as that.
Think of an application where you don't know the maximum amount of something, lets say a Bank Account...(for this example, money is only integers)
Now, you are a programmer, so the balance can probably be stored in a 16 bit number, in two accounts:
unsigned int * array = malloc(2 * sizeof(unsigned int)); //This allows the array
//to have two indexes.
Now, lets say we now need to add another account...
If we hadn't have been clever and used malloc, we would be stuffed.
However, we can now say:
realloc(3 * sizeof(unsigned int));
and add more bank accounts :)
Upvotes: 0
Reputation: 2066
Malloc give you the flexibility of dynamically allocating memory and using it when you need and freeing it up when you do not need. In the above case the memory allocated for person p will remain till the end of the stack. You may not find the use of it in your current example. But there are many cases where it makes sense to dynamically allocate memory.
Upvotes: 0