简体   繁体   English

动态内存分配和指针相关概念疑惑

[英]Dynamic memory allocation and pointers related concept doubts

On the first note: it is a new concept to me!!首先说明:这对我来说是一个新概念!! I studied pointers and dynamic memory allocations and executed some program recently and was wondering in statement char*p="Computers" the string is stored in some memory location and the base address, ie the starting address of the string is stored in p, now I noticed I can perform any desired operations on the string, now my doubt is why do we use a special statement like malloc and calloc when we can just declare a string like this of the desired length.我最近研究了指针和动态内存分配并执行了一些程序,想知道在语句 char*p="Computers" 中字符串存储在某个内存位置和基地址,即字符串的起始地址存储在 p 中,现在我注意到我可以对字符串执行任何所需的操作,现在我的疑问是为什么我们可以使用像 malloc 和 calloc 这样的特殊语句,因为我们可以声明这样一个所需长度的字符串。

If my understanding of the concept Is wrong please explain.如果我对概念的理解有误请解释。

Thanks in advance.提前致谢。

In this declaration在本声明中

char*p="Computers";

the pointer p is initialized by the address of the first character of the string literal "Computers" .指针p由字符串文字"Computers"的第一个字符的地址初始化。

String literals have the static storage duration.字符串文字具有静态存储持续时间。 You may not change a string literal as for example例如,您不能更改字符串文字

p[0] = 'c';

Any attempt to change a string literal results in undefined behavior.任何更改字符串文字的尝试都会导致未定义的行为。

The function malloc is used to allocate memory dynamically. malloc函数用于动态分配内存。 For example if you want to create dynamically a character array that will contain the string "Computers" you should write例如,如果您想动态创建一个包含字符串"Computers"的字符数组,您应该编写

char *p = malloc( 10 ); // the same as `malloc( 10 * sizeof( char ) )`
strcpy( p, "Computers" );

You may change the created character array.您可以更改创建的字符数组。 For example例如

p[0] = 'c';

After the array is not required any more you should free the allocated memory like在不再需要数组之后,您应该释放分配的内存,例如

free( p );

Otherwise the program can have a memory leak.否则程序可能会出现内存泄漏。

A simple answer to that would be by doing char *p = "Computers";一个简单的答案是执行char *p = "Computers"; you are basically declaring a fixed constant string.您基本上是在声明一个固定的常量字符串。 With that means you cannot edit anything inside the string.这意味着您无法编辑字符串内的任何内容。 Trying to do so may result in Segmentation Fault.尝试这样做可能会导致分段错误。 Using malloc and calloc would allow us to edit the string.使用 malloc 和 calloc 将允许我们编辑字符串。

Simply do this on p[0] = 'c' and you will see the result只需在p[0] = 'c'上执行此操作,您就会看到结果

A statement like像这样的声明

char *p = "Computers";

is not an example of dynamic memory allocation.不是动态内存分配的示例。 The memory for the string literal is set aside when the program starts up and held until the program terminates.字符串文字的内存在程序启动时被留出并一直保持到程序终止。 You can't resize that memory, and you're not supposed to modify it (the behavior on doing so is undefined - it may work as expected, it may crash outright, it may do anything in between).您不能调整该内存的大小,也不应该修改它(这样做的行为是未定义的- 它可能按预期工作,它可能会彻底崩溃,它可能会在两者之间做任何事情)。

We use malloc , calloc , and realloc to allocate memory at runtime that needs to be writable, resizable, and doesn't go away until we explicitly release it.我们使用malloccallocrealloc在运行时分配内存,这些内存需要可写、可调整大小,并且在我们明确释放它之前不会消失。

We have to use pointers to reference dynamically-allocated memory because that's just how the language is designed, but pointers play a much larger role in C programming than just tracking dynamic memory.我们必须使用指针来引用动态分配的内存,因为这正是语言的设计方式,但指针在 C 编程中扮演的角色比仅仅跟踪动态内存要大得多。

as a novice, I described below as my own thinking…作为一个新手,我在下面描述了我自己的想法……

Dynamic memory completely depends on the pointer.动态内存完全依赖于指针。 I mean without pointer knowledge you are able to cope up with dynamic memory allocation.我的意思是没有指针知识,您就可以处理动态内存分配。 (stdlib) library function where store calloc , malloc , relalloc and free . (stdlib) 库函数,其中存储callocmallocrelallocfree

malloc initialized no bit mentioned, calloc mainly used for the array. malloc初始化就不多说了, calloc主要用于数组。 realloc used to increase or decrease size. realloc用于增加或减少大小。

To simply say it is not as hard as what you first think.简单地说,它并不像你最初想象的那么难。 if you declare an array[500] initial declare but you used 100 and the rest of 400 bits remove to use dynamic memory.如果您声明一个array[500]初始声明,但您使用了 100 位和其余 400 位删除以使用动态内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM