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

Where does the string gets stored?

Please look at the following code snippets:
 
char str[]="abcd"; // Gets stored in stack frame.
 
char *str="abcd"; // Where does this gets stored?
 
 
In case of first declaration ie char str[]="abcd"; the string "abcd" gets stored in the stack frame of the invoked funciton. I wonder where does the string gets stored when we declare char *str="abcd"; (Is it in the heap?). Please reply.
 
 
Thanks
Iqbal
Iqbal_h_a
Light Poster
30 posts since Aug 2006
Reputation Points: 51
Solved Threads: 1
 

Please post your question outside code tags. I corrected your first two posts, do this one yourself, or I will delete this thread. Since it has been only 2 minutes since your post, you can edit it now.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
(Is it in the heap?)

I think both goes on the stack both times. Would only go on the heap/free store if you use the 'new' keyword.

Wolfie ?

p.s.
Yeah and put [ CODE ] [/ CODE ] around JUST the code not the whole post, like Wolpack said please.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

Wolfpack just gave me this URL
http://www.daniweb.com/techtalkforums/thread50780.html

Moral:
Search the forums before posting.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

>>char *str="abcd";

There are two objects here -- the pointer itself is a variable that is on the stack. It contains the address of the string that is stored in the heap -- often in read-only memory.

[edit]Sorry Holly I didn't see your last post when I posted this. The link you posted is a much better and thorough explaination.[/edit]

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
[edit]Sorry Holly I didn't see your last post when I posted this. The link you posted is a much better and thorough explaination.[/edit]

He he, well like they always say:
"Big red and firey, always late"

:lol:

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 
Please post your question outside code tags. I corrected your first two posts, do this one yourself, or I will delete this thread. Since it has been only 2 minutes since your post, you can edit it now.


Sorry for my ignorance. As I am new to this blog, I was unaware of the rules.
I tried to edit the post as you suggested, but could not do so.
I will definitely take care of all the rules in my future posts.
I am extremely sorry if I hurt anybody by breaking the rules.
Thanks
Iqbal

Iqbal_h_a
Light Poster
30 posts since Aug 2006
Reputation Points: 51
Solved Threads: 1
 
Sorry for my ignorance. As I am new to this blog, I was unaware of the rules. I tried to edit the post as you suggested, but could not do so. I will definitely take care of all the rules in my future posts. I am extremely sorry if I hurt anybody by breaking the rules. Thanks Iqbal

It's ok it doesn't hurt too much ;) he he
anyway at least we know you got the message now.

Of course we don't expect you to know all the rules straight off. If you look in my signature there's a link to 'the basics', I recommend reading that it will help you to make good posts that will get read and answered. Forums are a bit like Ebay you gotta put some effort into the listing to get bids and a sale, same here to get good answers.

Lastly don't mind WolfPack he's a big softy really, but he has the onerous responsibility of being a moderator and keeping everyone in line and the threads looking neat and tidy, so he has to throw his weight around a litte otherwise everyone would just ignore him, and the forum would be an unsightly mess. The knowledgeable people with all the answers only come and stay and answer posts in nice neat forums so the effort is worth it I think.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 
Sorry for my ignorance. As I am new to this blog, I was unaware of the rules. I tried to edit the post as you suggested, but could not do so. I will definitely take care of all the rules in my future posts. I am extremely sorry if I hurt anybody by breaking the rules. Thanks Iqbal

As long as you learn from your mistakes, everyone is happy about it. And btw welcome to the forums. Just be kind enough to respect other ppl out here and i am sure you would get all the help you require.

Keep rocking....

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You