为什么要使用malloc?(Why exactly malloc is used? [duplicate])

编程入门 行业动态 更新时间:2024-10-25 08:16:52
为什么要使用malloc?(Why exactly malloc is used? [duplicate])

这个问题在这里已有答案:

声明的字符串和分配的字符串 3答案 之间的差

我一直试图理解什么是malloc()以及为什么使用它。 我知道malloc是用于动态分配内存的,如果你不知道你要创建多少内存就需要它。 我一直在做一些动手。

下面的代码声明了一个字符指针数组,第一个字符指针用“hello”初始化。 这很好用。

int main() { char *strarray[5]; strarray[0]="hello"; printf("%s\n",strarray[0]); return 0; }

但是,如果我尝试使用strcpy()函数将“hello”字符串复制到strarray [0](没有malloc()),则会出现问题。 它进入一些循环并且不复制字符串。 如果我使用malloc来分配内存,它工作正常。

int main() { char *strarray[5]; //strarray[0]=(char *)malloc(sizeof(char)*10); strcpy(strarray[0],"hello"); printf("%s\n",strarray[0]); return 0; }

我想知道是什么产生了影响? 如果我可以将“hello”初始化为未使用malloc的char指针,为什么我不能对strcpy()执行相同的操作。

This question already has an answer here:

Difference between declared string and allocated string 3 answers

I have been trying to understand what is malloc() and why is it used. I understand that malloc is for dynamic allocation of memory, it is needed if you don't know how much memory you wan't to create. I have been doing some hands-on on it.

The following code declares an array of character pointers, and 1st character pointer is initialized with "hello". This works fine.

int main() { char *strarray[5]; strarray[0]="hello"; printf("%s\n",strarray[0]); return 0; }

But if i try to use strcpy() function to copy "hello" string into strarray[0] (without malloc()) it gives a problem. And it goes into some loop and does not copy the string. And it works fine if i use malloc to allocate memory.

int main() { char *strarray[5]; //strarray[0]=(char *)malloc(sizeof(char)*10); strcpy(strarray[0],"hello"); printf("%s\n",strarray[0]); return 0; }

I want to know what makes the difference? If i can initialize "hello" to a char pointer for which malloc is not used, why can't i do the same with strcpy().

最满意答案

char *strarray[5];

将为字符分配五个指针的数组(可以解释为字符串)。

不会分配的是放置这五个字符串的内存空间。 所以,如果你想让所有指针指向不同的字符串,你需要自己分配它们。

但是,由于这些是指针,如果你不想,你根本不需要分配任何东西。 这就是为什么你的第一个例子正在运作。 strarray[0]="hello"; 将使数组的第一个元素指向存储"hello"字符串文字的位置(编译器会将此字面值放在内存中,因此指针将指向有效的内存位置)。

总而言之,在第一个例子中,你的指针指向已经分配并用"hello"初始化的内存(编译器为你做了这个) - 你没有复制字符串,你只是为指针赋值。 在第二个示例中,指针指向某个未定义的位置,您没有明确保留它,但是您正在尝试写入它。

char *strarray[5];

will allocate array of five pointers to the characters (which can be interpreted as strings).

What this will not allocate is the memory space to place these five strings to. So, if you want to make all pointers to point to the different strings, you need to allocate them yourself.

However, as these are pointers, you don't really need to allocate anything, if you don't want to. That is why your first example is working. strarray[0]="hello"; will make the first element of the array to point to the location where "hello" string literal is stored (the compiler will put this literal somewhere in the memory for you, so your pointer will point to the valid memory location).

To sum up, in the first example, your pointer is pointing to the memory already allocated and initialized with "hello" (compiler did this for you) - you're not copying the string, you're just assigning value to the pointer. In the second example your pointer is pointing to some undefined location, which you didn't explicitly reserved, but you're trying to write into it.

更多推荐

本文发布于:2023-08-01 00:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要使   malloc   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!