954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Doubt on Strings

void main(void)
{
 char *p = "name";
 p[0] = 's';
 printf("%s", p); 
}


The above pieceof code is giving me eror at run time. The reason for this error might be that I am not allocating any memory for the pointer(correct me if I am wrong).
My doubt is, In the first line of code what exactly is happening.

char *p = "name";

1) Will the compiler places the string "name" in the stack segment of the memory and then initializes p with the address where the string "name" is stored ?

2) Will the string "name" be in the stack???

3) If that is the case why I am getting the error at the run time. The memory is anyhow allocated to the string.

4) I commented out each line and tried debuging. That forced me to the conclusion that the error is in line number 2.
p[0] = 's'; What is the problem with this statement?

If I allocate the memory to the pointer using malloc the error vanishes. Why is it like that??

dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
 

Assume string literals are nonmodifiable. Use a modifiable array.

char p[] = "name";


And main returns an int. Are you just starting out and have a bad reference?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

If I use array i can modify the string, but y not in this case ????

I havent got answers to my questions ... if u can answer that also it would be a grt help

dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
 

> The reason for this error might be that I am not allocating any memory for the pointer(correct me if I am wrong).
Yes, it has been allocated, but the memory which you're pointing at is read-only (meaning you can't change it). The variable p can be changed (say p="world"), but what it points at cannot be changed (you get the error).

> 1) Will the compiler places the string "name" in the stack segment of the memory
No, only p is on the stack, the string is somewhere else, allocated by the compiler.
In effect, the compiler does this for you. You just never see (or know) the name of the array it creates.

const char anon[] = "hello";
int main(void)
{
 char *p = anon;
 p[0] = 's';
 printf("%s", p); 
}


If you want something you can change, try

int main(void)
{
 char hello[] = "hello";
 char *p = hello;
 p[0] = 's';
 printf("%s", p); 
}

Both your pointer and string are on the stack, and you can change the string via your pointer.

> 2) Will the string "name" be in the stack???
No, see above.

> 3) If that is the case why I am getting the error at the run time. The memory is anyhow allocated to the string.
Because the 'anon' string itself is not WRITEABLE.
Yes, it is allocated, no you can't change it.

> void main(void)
main returns an int.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Your explanation says,

1) Will the compiler places the string "name" in the stack segment of the memory
No, only p is on the stack, the string is somewhere else, allocated by the compiler.
In effect, the compiler does this for you. You just never see (or know) the name of the array it creates.

What I understood is that the compiler places the string in some area which is not writable and assigns the starting address to p.
somewherein the explanation is not clear.
Can you tell me where exactly compiler places that string?(which segment of memory)

dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
 
Can you tell me where exactly compiler places that string?(which segment of memory)

This depends on the compiler, so you shouldn't worry about it. The standard has made this part implementation dependent. Infact it hasn't even said whether the same string literal used in two different places are equal or not.

For example

char* p = "string";
char* q = "string";


The above two string objects "string" can be stored in two different places or in the same location to save memory. The Microsoft Compiler enables both.Microsoft Specific
In some cases, identical string literals can be "pooled" to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. /GF enables string pooling.
END Microsoft Specific Reference .

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Thanks guys,

dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You