声明的字符串和分配的字符串之间的区别 [英] Difference between declared string and allocated string

查看:113
本文介绍了声明的字符串和分配的字符串之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I.  char *str = "Stack";

II. char *str = (char*) malloc(6);
    strcpy(str, "Stack");

什么是上述两种方法的区别?

What are the differences between above two approaches?

他们是否相同或东西是幕后有什么不同?

Do they are same or something is different behind the curtains?

推荐答案

在code以上会造成你很大的问题。

The code above will cause you big problems.

第一个实例被称为静态字符串分配和定义。
对于像 INT 正常变量等,以及非字符串数据类型,这样的声明将在栈上分配数据。在字符串的情况下,通过字符串初始化(即:堆栈),它被分配在内存的只读部分

The first instance is known as static string allocation and definition. For normal variables like int, etc, and non-string data types, such a declaration would allocate data on the stack. In the case of strings being initialized via string literals (ie: "stack"), it is allocated in a read-only portion of memory.

的字符串本身不应修改,因为它会被存储在存储器的只读部分。指针本身可以改变为指向新的位置。

The string itself should not be modified, as it will be stored in a read-only portion of memory. The pointer itself can be changed to point to a new location.

例如:

char strGlobal[10] = "Global";

int main(void) {
  char* str = "Stack";
  char* st2 = "NewStack";
  str = str2;  // OK
  strcpy(str, str2); // Will crash
}

为了安全起见,你应该实际分配的指针指向const数据,例如:

To be safe, you should actually allocate as a pointer to const data, ie:

const char* str = "Stack"; // Same effect as char* str, but the compiler
                           // now provides additional warnings against doing something dangerous

第二被称为动态分配,其中在堆,而不是堆栈分配内存。该字符串可以毫不费力修改。在某些时候,你需要释放通过免费()命令这个动态分配的内存。

The second is known as dynamic allocation, which allocates memory on the heap, not the stack. The string can be modified without hassle. At some point, you need to free this dynamically allocated memory via the free() command.

有是分配一个字符串,它是在栈上的静态分配的第三装置。这允许您修改数组的是持有该字符串的内容,并且它是静态分配的。

There is a third means of allocating a string, which is static allocation on the stack. This allows you to modify the contents of the array which is holding the string, and it is statically allocated.

char str[] = "Stack";

在总结:

Example:                       Allocation Type:     Read/Write:    Storage Location:
================================================================================
const char* str = "Stack";     Static               Read-only      Code segment
char* str = "Stack";           Static               Read-only      Code segment
char* str = malloc(...);       Dynamic              Read-write     Heap
char str[] = "Stack";          Static               Read-write     Stack
char strGlobal[10] = "Global"; Static               Read-write     Data Segment (R/W)

您还应该对数据如何分割在现代操作系统的应用程序读取了。它真的会增加你的code是如何被构建的了解。

You should also read up on how data is segmented for applications in modern operating systems. It will really increase your understanding of how your code is being built.

参考


  1. 数据段的,访问的2013年4月15日,<一个href=\"http://en.wikipedia.org/wiki/Data_segment\"><$c$c><http://en.wikipedia.org/wiki/Data_segment>

  2. code段的,访问的2013年4月15日,<一个href=\"http://en.wikipedia.org/wiki/$c$c_segment\"><$c$c><http://en.wikipedia.org/wiki/$c$c_segment>

  1. Data Segment, Accessed 2013-04-15, <http://en.wikipedia.org/wiki/Data_segment>
  2. Code Segment, Accessed 2013-04-15, <http://en.wikipedia.org/wiki/Code_segment>

这篇关于声明的字符串和分配的字符串之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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