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
我正在尝试理解malloc,并创建一个非常简单的程序,它将使用ASCII代码打印“ABC”
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?
我不明白的是,为什么我要将malloc放在那里?教授告诉我们程序不会在没有malloc的情况下运行,但是当我尝试在没有malloc的情况下运行它时,程序运行得很好。那么malloc的功能是什么?我甚至使用它吗?
any help and or explanation would be really appreciated
任何帮助和/或解释将非常感激
3 个解决方案
#1
3
the professor told us the program won't run without the malloc
教授告诉我们,如果没有malloc,程序将无法运行
This is not quite true, the correct wording would be: "The program's behavior is undefined without malloc()
".
这不完全正确,正确的措辞是:“没有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.
只是声明一个指向char的指针,但是没有初始化 - 这个指针指向一些不确定的位置。你可能只是幸运的是,写这个“随机”位置的值会起作用并且不会导致崩溃。我个人称之为不吉利,因为这会隐藏你程序中的错误。未定义的行为只意味着任何事情都可能发生,包括“正确的”程序执行。
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
.
malloc()将动态请求一些可用内存并返回指向该内存的指针,因此在malloc()之后,你知道我指向你可以使用的4个字节的内存。如果malloc()因某种原因失败(没有更多可用内存),则返回NULL - 您的程序应该在写入* i之前测试它。
All that said, of course the program CAN work without malloc()
. You could just write
所有这一切,当然程序CAN没有malloc()。你可以写
char i[4];
and i
would be a local variable with room for 4 characters.
我将是一个局部变量,可容纳4个字符。
Final side note: sizeof(char)
is defined to be 1
, so you can just write i = malloc(4);
.
最后的注意事项:sizeof(char)被定义为1,所以你可以写i = malloc(4);.
#2
2
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.
不幸的是,“运行正常”标准证明了C程序没有任何意义。大量运行完成的C程序具有未定义的行为,这种行为不会在您的特定平台上体现出来。
You need special tools to see this error. For example, you can run your code through valgrind, and see it access uninitialized pointer.
您需要特殊工具才能看到此错误。例如,您可以通过valgrind运行代码,并看到它访问未初始化的指针。
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:
至于malloc,您不必在代码中使用动态缓冲区。在自动内存中分配缓冲区是完全没问题的,如下所示:
char buf[4], *i = buf;
#3
0
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)
你必须为内存分配空间。在下面的示例中,我没有为i分配内存,导致分段错误(您尝试访问您无权访问的内存)
#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.
Malloc允许您创建空间,因此您可以写入内存中的某个位置。在第一个例子中,“没有malloc就行不通”,因为我指的是内存中没有分配空间的点。
#1
3
the professor told us the program won't run without the malloc
教授告诉我们,如果没有malloc,程序将无法运行
This is not quite true, the correct wording would be: "The program's behavior is undefined without malloc()
".
这不完全正确,正确的措辞是:“没有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.
只是声明一个指向char的指针,但是没有初始化 - 这个指针指向一些不确定的位置。你可能只是幸运的是,写这个“随机”位置的值会起作用并且不会导致崩溃。我个人称之为不吉利,因为这会隐藏你程序中的错误。未定义的行为只意味着任何事情都可能发生,包括“正确的”程序执行。
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
.
malloc()将动态请求一些可用内存并返回指向该内存的指针,因此在malloc()之后,你知道我指向你可以使用的4个字节的内存。如果malloc()因某种原因失败(没有更多可用内存),则返回NULL - 您的程序应该在写入* i之前测试它。
All that said, of course the program CAN work without malloc()
. You could just write
所有这一切,当然程序CAN没有malloc()。你可以写
char i[4];
and i
would be a local variable with room for 4 characters.
我将是一个局部变量,可容纳4个字符。
Final side note: sizeof(char)
is defined to be 1
, so you can just write i = malloc(4);
.
最后的注意事项:sizeof(char)被定义为1,所以你可以写i = malloc(4);.
#2
2
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.
不幸的是,“运行正常”标准证明了C程序没有任何意义。大量运行完成的C程序具有未定义的行为,这种行为不会在您的特定平台上体现出来。
You need special tools to see this error. For example, you can run your code through valgrind, and see it access uninitialized pointer.
您需要特殊工具才能看到此错误。例如,您可以通过valgrind运行代码,并看到它访问未初始化的指针。
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:
至于malloc,您不必在代码中使用动态缓冲区。在自动内存中分配缓冲区是完全没问题的,如下所示:
char buf[4], *i = buf;
#3
0
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)
你必须为内存分配空间。在下面的示例中,我没有为i分配内存,导致分段错误(您尝试访问您无权访问的内存)
#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.
Malloc允许您创建空间,因此您可以写入内存中的某个位置。在第一个例子中,“没有malloc就行不通”,因为我指的是内存中没有分配空间的点。