Reputation: 79
so I'm quite new in this, sorry if it sound like a dumb question
I'm trying to understand malloc, and create a very simple program which will print "ABC" using ASCII code
here is my code (what our professor taught us) so far
char *i;
i = malloc(sizeof(char)*4);
*i = 65;
*(i+1) = 66;
*(i+2) = 67;
*(i+3) = '\0';
what I don't understand is, why do I have to put malloc there? the professor told us the program won't run without the malloc, but when I tried and run it without the malloc, the program run just fine. so what's the function of malloc there? am I even using it right?
any help and or explanation would be really appreciated
Upvotes: 0
Views: 528
Reputation: 117
You have to allocate space for memory. In the example below, I did not allocate for memory for i, which resulted in a segmentation fault (you are trying to access memory that you don't have access to)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *i;
strcpy(i, "hello");
printf("%s\n", i);
return (0);
}
Output: Segmentation fault (core dumped)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *i;
/*Allocated 6 spots with sizeof char +1 for \0 character*/
i = malloc(sizeof(char) * 6);
strcpy(i, "hello");
printf("%s\n", i);
return (0);
}
Result: hello
Malloc allows you to create space, so you can write to a spot in memory. In the first example, "It won't work without malloc" because i is pointing to a spot in memory that doesn't have space allocated yet.
Upvotes: 1
Reputation:
the professor told us the program won't run without the malloc
This is not quite true, the correct wording would be: "The program's behavior is undefined without malloc()
".
The reason for this is that
char *i;
just declares a pointer to a char
, but there's no initialization -- this pointer points to some indeterminate location. You could be just lucky in that writing values to this "random" location works and won't result in a crash. I'd personally call it unlucky because this hides a bug in your program. undefined behavior just means anything can happen, including a "correct" program execution.
malloc()
will dynamically request some usable memory and return a pointer to that memory, so after the malloc()
, you know i
points to 4 bytes of memory you can use. If malloc()
fails for some reason (no more memory available), it returns NULL
-- your program should test for it before writing to *i
.
All that said, of course the program CAN work without malloc()
. You could just write
char i[4];
and i
would be a local variable with room for 4 characters.
Final side note: sizeof(char)
is defined to be 1
, so you can just write i = malloc(4);
.
Upvotes: 4
Reputation: 726479
Unfortunately, "runs fine" criterion proves nothing about a C program. Great deal of C programs that run to completion have undefined behavior, which does not happen to manifest itself on your particular platform.
You need special tools to see this error. For example, you can run your code through valgrind, and see it access uninitialized pointer.
As for the malloc, you do not have to use dynamic buffer in your code. It would be perfectly fine to allocate the buffer in automatic memory, like this:
char buf[4], *i = buf;
Upvotes: 3