为什么不需要声明字符串时分配内存 [英] Why dont need to allocate memory when declaring string

查看:170
本文介绍了为什么不需要声明字符串时分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C,目前我想了解如何工作的指针。

下面是一个问题困惑我:

据我所知,值分配给指针之前,您应该为指针分配一定的内存(如果我错了,请大家指正:)),如后续code:

  INT主要(无效){
    INT I;
    为int * PTR =(INT *)malloc的(的sizeof(INT));    我= 2;
    * PTR = 5;
    printfn(%D,* PTR);
    免费(PTR);
}

然而,在声明C字符串的时候,就像:

 的char * p =Hello World的;

而不需要分配内存。

是什么原因,它是如何工作的?如果我失去了一些东西,善意地提醒我。非常感谢!


解决方案

  

...你应该分配一定的内存为指针...


没有,你似乎误解了关于指针...

指针是持有型某种类型的的地址的。看下面这个例子。

  int类型的= 1;
为int * p =&放大器;一个;

类型的 A INT ,其中包含的整数

的类型 P 为int * ,其中包含的整型变量的地址

让我们假设内存是这样的:

  --------------------------------------
变量:| A | p |
--------------------------------------
地址:| 0x12341234 | 0x12341238 |
--------------------------------------

&安培; 运营商获得的操作的地址。因此,&放大器;一个等于 0x12341234

所以变量初始化是这样的:

  --------------------------------------
变量:| A | p |
--------------------------------------
地址:| 0x12341234 | 0x12341238 |
--------------------------------------
值:| 1 | 0x12341234 |
--------------------------------------

现在,看看这个code, * P 。在 * 运营商的引用操作的,得到的该指针指向的变量的值。在这种情况下, P 包含 0x12341234 - 在 0x12341234 ?在 A !因此, * P 等于 1 ,其中 A 包含


现在看看这个例子:

 的#include<&stdlib.h中GT;炭C1 ='1';诠释的main()
{
    炭C2 ='2';    字符* P1 =&放大器; C1;
    字符* P2 =&放大器; C2;    CHAR AR [13] =世界,你好! / *不要忘记'\\ 0':12 + 1 * /    字符* P3 =&放大器; AR [0];
    为const char * P4 =世界,你好! / *注意的类型,`为const char *`* /    字符* P5 =的malloc(13 * sizeof的(炭));
}

C1 是全局变量,因此的编译器将它放在数据部分节目直接。 C2 ,所以它被放置在的的由<$ C的局部变量$ C>主。无论如何,他们放在内存中。和 P1 P2 包含他们的地址的,所以 * P1 1 C1 )和 * P2 '2' C2 )。

AR 字符 13长度的阵列的。它被放置在内存中是这样的:

  -------------------------------------- ----------------
|'H'|'E'|'L'|'L'|'O'|' '|'W'|'O'|'R'|'L'|'D'|'!'|'\\ 0'|
-------------------------------------------------- ----

&放大器; AR [0] 的第一个元素的AR 的地址,因此 P3 包含的地址'H'

现在看看 P4 。它是由初始化世界,你好!。你的问题是它被分配 - 这只是(串)的的,如 1234 2.71 'A'。常数被放置在程序直接由编译器。像 C1 字符串常量的放在 RO 数据程序段的直接。对比的数据部分 rodata节的只读(只读数据),因为字符串常量是恒定的。 (如你所知,不变是只读的。)所以类型 P4 为const char *

P5 是由的malloc ,也就是从动态分配的返回值初始化。在这种情况下,的malloc 地方分配内存和 P5 由该initialied。

I am new to C, and currently I am trying to understand how to pointer work.

Here is one problem confusing me:

As far as I know, before assigning a value to pointer, you should allocate certain memory for that pointer (if I am wrong, pls correct me :) ), like the follow code:

int main(void) {
    int i;
    int * ptr = (int *)malloc(sizeof(int));

    i = 2;
    *ptr = 5;     
    printfn("%d",*ptr); 
    free(ptr);
}

However, when declaring string in C, just like:

char *p = "Hello world"; 

without need to allocate memory.

What's the reason and how it work? If I am missing something, kindly remind me. many thanks!

解决方案

...you should allocate certain memory for that pointer ...

No, you seems to misunderstand about pointers...

Pointer is the type which hold some type's address. Look at this example.

int a = 1;
int *p = &a;

The type of a is int, which contains integer.

The type of p is int *, which contains address of integer variables.

Let's suppose memory like this:

--------------------------------------
variables: | a          | p          |
--------------------------------------
address  : | 0x12341234 | 0x12341238 |
--------------------------------------

the & operator gets the address of operand. So, &a is equal to 0x12341234.

So the variables are initialized like this:

--------------------------------------
variables: | a          | p          |
--------------------------------------
address  : | 0x12341234 | 0x12341238 |
--------------------------------------
value    : | 1          | 0x12341234 |
--------------------------------------

Now, look this code, *p. the * operator, dereference operator, gets the value of the variables which the pointer is pointing. In this case, p contains 0x12341234 - What variables is in 0x12341234? the a! So *p is equal to 1, which a contains.


Now look this example:

#include <stdlib.h>

char c1 = '1';

int main()
{
    char c2 = '2';

    char *p1 = &c1;
    char *p2 = &c2;

    char ar[13] = "hello world!"; /* don't forget '\0' : 12 + 1. */

    char *p3 = &ar[0];
    const char *p4 = "hello world!"; /* notice the type, `const char *` */

    char *p5 = malloc(13 * sizeof(char));
}

c1 is global variables, so compiler place it on the data section of program directly. c2 is local variables of main, so it is placed on stack by main. Anyway, they're placed on memory. And p1 and p2 contains their address, so *p1 is '1' (c1) and *p2 is '2' (c2).

ar is 13-length array of char. It is placed on memory like this:

------------------------------------------------------
|'h'|'e'|'l'|'l'|'o'|' '|'w'|'o'|'r'|'l'|'d'|'!'|'\0'|
------------------------------------------------------

And &ar[0] is the address of the first element of ar, so p3 contains the address of 'h'.

Now look at p4. It's initialized by "hello world!". Your question is where it is allocated - it's just (string) constant, like 1234, 2.71 or 'a'. Constant is placed on the program directly, by compiler. Like c1, the string constant is placed on the rodata section of program directly. Contrast to data section, rodata section is read-only (Read Only DATA) because string constant is constant. (As you know, constant is read-only.) So the type of p4 is const char *.

p5 is initialized by return value of malloc, that is from dynamic allocation. In this case, malloc allocates the memory somewhere, and p5 is initialied by this.

这篇关于为什么不需要声明字符串时分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆