answersLogoWhite

0

Declaring strings as char* is generally faster than declaring an array of type char.

Consider the following:

#include<stdio.h>

int main (void) {

char c1[12] = "Hello world";

char c2[] = "Hello world";

char* c3 = "Hello world";

// ...

return 0;

}

The string literal, "Hello world" is duplicated three times in the source code but in the resultant machine code that string only appears once (in the data segment). This is known as string-pooling; it makes no sense to maintain separate copies of the exact same value in the data segment.

Given that there is only one copy of the string, it would be natural to assume that c1, c2 and c3 all refer to the same memory location but they don't. In fact, the only one that does refer to the data segment copy is the third one.

If we examine the assembly for the c1 initialisation we find the following:

mov eax, DWORD PTR ??_C@_0M@KIBDPGDE@Hello?5world?$AA@

mov DWORD PTR _c1$[ebp], eax

mov ecx, DWORD PTR ??_C@_0M@KIBDPGDE@Hello?5world?$AA@+4

mov DWORD PTR _c1$[ebp+4], ecx

mov edx, DWORD PTR ??_C@_0M@KIBDPGDE@Hello?5world?$AA@+8

mov DWORD PTR _c1$[ebp+8], edx

The symbol ??_C@_0M@KIBDPGDE@Hello?5world?$AA@ identifies the string in the data segment but the assembly clearly shows data is being copied (moved) to a new location 32-bits (4 bytes) at a time. The new location is the address of c1. The same thing happens for c2, making yet another copy of the string.

However the c3 initialisation consists of just one instruction.

mov DWORD PTR _c3$[ebp], OFFSET ??_C@_0M@KIBDPGDE@Hello?5world?$AA@

The difference is that instead of copying the string to new memory, we simply copy the address of the string.

This is low-level stuff of course, but it makes no sense to copy objects from the data segment or indeed anywhere else when we can simply refer to them directly using a pointer. We only need to copy objects when we actually intend to make some change to the object without affecting the original object. In all other cases we can simply refer to them.

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

How do you declare a pointer to a character string in c?

char *ptr;


How do you declare a string in a function prototype?

example: size_t strlen (const char *s);


Declare and initialize variables of int double char boolean and String?

public class Test { int i = 0; double d = 0; char c = 'A'; boolean val = true; String str = "Rocky"; ....... ...... ..... }


What is the difference between Char a equals string and char a equals String?

Well, if you write like char a=string; it is wrong. You have to declare the size of the array or else put the brackets immediately after the variable declaration. You also have to put the string in quotes, or provide a comma-separated list of characters. E.g.,char a[]={'s','t','r','i','n','g'};Or more simply:char a[] = "string";Remember that C/C++ is case-sensitive and that all keywords are lower case. Thus Char would be regarded as an invalid keyword.


How does one declare a string of 49 letters?

char str[50]; Note that you must include the null-terminator, thus you need 50 characters to store a string of 49 letters. Alternatively, you can use dynamic allocation: char* p; p = malloc (50 * sizeof(char)); /* ... */ free (p);


Declare a character pointer 'ptrString' and make it to point the string ati Explain your work diagrammatically?

char *ptrString = "ati'; ptrString is an object that contains the address of "ati". *ptrString, however, is a reference to the string "ati", and would be similar to the statement char nonptrString[] = "ati";


How do you convert char to String in java?

A String is nothing but a bunch of characters one after the other whereas a character has a size of only '1' So converting a string into a char is not possible. what you can do is form a character based on only one element from the string. Ex: String name = "Rocky"; char xyz = name.charAt(0); now the char xyz will have one character from the String.


C prog to concatenate two string without using buid in function?

//String Concatination#include#includeusing namespace std;char* strcat(char*,char*);int main(){char str1[100];char str2[100];coutstr1;coutstr2;cout


How do you declare a string?

prefix with a """string()""


How do you declare two character variable?

One by one: char x; char y; &amp; both together char x,y;


How do you convert char to string?

Character.toString('c')


C plus plus programs - to replace every space in a string with a hyphen?

char *string = "this is a test"; char *p; for (p=string; *p!='\0'; p++) if (*p==' ') *p='-';