[C++] C String存储方式-英文版

Storage for Strings in C

In C, a string can be referred to either using a character pointer or as a character array.

Strings as character arrays

char str[4] = "GfG"; /*One extra for string terminator*/``
/*  OR  */
char str[4] = {‘G’, ‘f’, ‘G’, '\0'}; /* '\0' is string terminator */

When strings are declared as character arrays, they are stored like other types of arrays in C. For example, if str[] is an auto variable then string is stored in stack segment, if it’s a global or static variable then stored in data segment, etc.

Data segment

In computing, a data segment (often denoted .data) is a portion of an object file or the corresponding address space of a program that contains initialized static variables, that is, global variables and static local variables. The size of this segment is determined by the size of the values in the program’s source code, and does not change at run time.

The data segment is read/write, since the values of variables can be altered at run time. This is in contrast to the read-only data segment (rodata segment or .rodata), which contains static constants rather than variables; it also contrasts to the code segment, also known as the text segment, which is read-only on many architectures. Uninitialized data, both variables and constants, is instead in the BSS segment.

Strings using character pointers
Using character pointer strings can be stored in two ways:

  1. Read only string in a shared segment.
    When a string value is directly assigned to a pointer, in most of the compilers, it’s stored in a read-only block (generally in data segment) that is shared among functions.

char *str = "GfG";

In the above line “GfG” is stored in a shared read-only location, but pointer str is stored in a read-write memory. You can change str to point something else but cannot change value at present str(类似指向常量的指针). So this kind of string should only be used when we don’t want to modify string at a later stage in the program.

  1. Dynamically allocated in heap segment.
    Strings are stored like other dynamically allocated things in C and can be shared among functions(堆内存).
char *str; 
int size = 4; /*one extra for ‘\0’*/
str = (char *)malloc(sizeof(char)*size); 
*(str+0) = 'G';  
*(str+1) = 'f';   
*(str+2) = 'G';   
*(str+3) = '\0';   

Let us see some examples to better understand the above ways to store strings.

Example 1 (Try to modify string)
The below program may crash (gives segmentation fault error) because the line *(str+1) = ‘n’ tries to write a read only memory.

int main() 
{ 
 char *str;  
 str = "GfG";     /* Stored in read only part of data segment */
 *(str+1) = 'n'; /* Problem:  trying to modify read only memory */
 getchar(); 
 return 0; 
} 

Below program works perfectly fine as str[] is stored in writable stack segment(生命周期为一次函数调用).

int main() 
{ 
 char str[] = "GfG";  /* Stored in stack segment like other auto variables */
 *(str+1) = 'n';   /* No problem: String is now GnG */
 getchar(); 
 return 0; 
} 

Below program also works perfectly fine as data at str is stored in writable heap segment(生命周期为整个程序运行期,但是需要手动释放内存).

int main() 
{ 
  int size = 4; 
  
  /* Stored in heap segment like other dynamically allocated things */
  char *str = (char *)malloc(sizeof(char)*size); 
  *(str+0) = 'G';  
  *(str+1) = 'f';   
  *(str+2) = 'G';     
  *(str+3) = '\0';   
  *(str+1) = 'n';  /* No problem: String is now GnG */
   getchar(); 
   return 0; 
}  

Example 2 (Try to return string from a function)
The below program works perfectly fine as the string is stored in a shared segment and data stored remains there even after return of getString()

char *getString() 
{ 
  char *str = "GfG"; /* Stored in read only part of shared segment */
  
  /* No problem: remains at address str after getString() returns*/
  return str;   
}      
  
int main() 
{ 
  printf("%s", getString());   
  getchar(); 
  return 0; 
} 

The below program also works perfectly fine as the string is stored in heap segment and data stored in heap segment persists even after the return of getString()

char *getString() 
{ 
  int size = 4; 
  char *str = (char *)malloc(sizeof(char)*size); /*Stored in heap segment*/
  *(str+0) = 'G';  
  *(str+1) = 'f';   
  *(str+2) = 'G'; 
  *(str+3) = '\0';   
    
  /* No problem: string remains at str after getString() returns */    
  return str;   
}      
int main() 
{ 
  printf("%s", getString());   
  getchar(); 
  return 0; 
} 

But, the below program may print some garbage data as string is stored in stack frame of function getString() and data may not be there after getString() returns.

char *getString() 
{ 
  char str[] = "GfG"; /* Stored in stack segment */
  
  /* Problem: string may not be present after getSting() returns */
  return str;  
}      
int main() 
{ 
  printf("%s", getString());   
  getchar(); 
  return 0; 
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值