Byte array as string

char str[] = "Hello World";
str[] is an array of characters whose elements are:
'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'

Byte array as string will be linked to global data area or in stack area depending on where it is declared;

Pointer to Constant String

str is a pointer to a constant string. str stores the address to the data section of the executable where the "Hello World" string is stored.

char *str = "Hello World";

Constant String & Byte array memory segments

A program runtime memory segment will consume - TEXT/Code segment, Const Data segment, Data segment, BSS segment, Heap segment, Stack segment. This below table shows the segments and they will be arranged from lower address to upper most address in virtual memory area.

C program memory segment map

Constant String & Byte array example

#include <stdio.h>

char * g_str = "Hello World";
char g_str_array[] = "Hello World";
int g_bss;
int g_data = 0;
const int g_const = 10;
int main (int argc, char *argv[])
{
  char * l_str = "Hello World";
  char  l_str_array[] = "Hello World";
  printf("g_str location %p\n", g_str);
  printf("l_str location %p\n", l_str);
  printf("g_str_array location %p\n", g_str_array);
  printf("l_str_array location %p\n", l_str_array);
  printf("g_bss location %p\n", &g_bss);
  printf("g_data location %p\n", &g_data);
  printf("g_const location %p\n", &g_const);
  return 0;
}

Linker map

# Path: a.out
# Arch: x86_64
# Object files:
[  0] linker synthesized
[  1] /var/folders/kg/5z9vwhy93674hvprgk5kgw040000gn/T/string-9c1aae.o
[  2] /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd
# Sections:
# Address	Size    	Segment	Section
0x100000DD0	0x0000010B	__TEXT	__text
0x100000EDC	0x0000000C	__TEXT	__stubs
0x100000EE8	0x00000024	__TEXT	__stub_helper
0x100000F0C	0x000000A0	__TEXT	__cstring
0x100000FAC	0x00000004	__TEXT	__const
0x100000FB0	0x00000048	__TEXT	__unwind_info
0x100001000	0x00000008	__DATA	__nl_symbol_ptr
0x100001008	0x00000010	__DATA	__got
0x100001018	0x00000010	__DATA	__la_symbol_ptr
0x100001028	0x00000014	__DATA	__data
0x10000103C	0x00000008	__DATA	__common
# Symbols:
# Address	Size    	File  Name
0x100000DD0	0x0000010B	[  1] _main
0x100000EDC	0x00000006	[  2] ___stack_chk_fail
0x100000EE2	0x00000006	[  2] _printf
0x100000EE8	0x00000010	[  0] helper helper
0x100000EF8	0x0000000A	[  2] ___stack_chk_fail
0x100000F02	0x0000000A	[  2] _printf
0x100000F0C	0x0000000C	[  1] literal string: Hello World
0x100000F18	0x00000013	[  1] literal string: g_str location %p\n
0x100000F2B	0x00000013	[  1] literal string: l_str location %p\n
0x100000F3E	0x00000019	[  1] literal string: g_str_array location %p\n
0x100000F57	0x00000019	[  1] literal string: l_str_array location %p\n
0x100000F70	0x00000013	[  1] literal string: g_bss location %p\n
0x100000F83	0x00000014	[  1] literal string: g_data location %p\n
0x100000F97	0x00000015	[  1] literal string: g_const location %p\n
0x100000FAC	0x00000004	[  1] _g_const
0x100000FB0	0x00000048	[  0] compact unwind info
0x100001000	0x00000008	[  0] non-lazy-pointer
0x100001008	0x00000008	[  0] non-lazy-pointer-to-local: ___stack_chk_guard
0x100001010	0x00000008	[  0] non-lazy-pointer-to-local: dyld_stub_binder
0x100001018	0x00000008	[  2] ___stack_chk_fail
0x100001020	0x00000008	[  2] _printf
0x100001028	0x00000008	[  1] _g_str
0x100001030	0x0000000C	[  1] _g_str_array
0x10000103C	0x00000004	[  1] _g_data
0x100001040	0x00000004	[  1] _g_bss

Output

$./a.out
g_str location 0x104aa8f0c
l_str location 0x104aa8f0c
g_str_array location 0x104aa9030
l_str_array location 0x7ffeeb157a7c
g_bss location 0x104aa9040
g_data location 0x104aa903c
g_const location 0x104aa8fac
g_str       0x104aa8f0c    <<< Const Data
l_str       0x104aa8f0c    <<< Const Data
g_str_array 0x104aa9030    <<< Data global
l_str_array 0x7ffeeb157a7c <<< Stack area
g_bss       0x104aa9040    <<< Data global
g_data      0x104aa903c    <<< Data global
g_const     0x104aa8fac    <<< Const data

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#