Help needed :(

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 70
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Help needed :(

 
0
  #1
May 5th, 2007
I just started mess with Memory at c++ so i am very confused now, and this is why i am asking 3 questions that made me confuse so much.

1. Look at this:
char *str = "Literal String";

Here we are creating a char pointer called str then we are giving to some CELLS in memory this values "Literal String" and setting str to point on the first element at this memory block.

so it means that this expression "Literal String" returns an address of the first element in this memory block to str everywhere??
or its just in char pointers case??

2.
and what is going on here?

char x = 's';

We are giving to some CELL in memory a name X and then we are giving to this CELL the value 's'??

Or we are giving to some CELL in memory a name X then we are giving to some CELL this value 's' and then we are copying the value 's' from this CELL to the CELL X??

What is the correct answer here??

3.
And what is this?

void main()
{
"Literal String";
}

Here we are just giving to some CELLS in memory this values "Literal String" ???

and thats all??

I very need answers to this questions because when i started mess with memory its made me very very confused and i cant understand a thing now .
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Help needed :(

 
1
  #2
May 5th, 2007
1. The expression "Literal string" is a cute way of writing a memory address that points to to the front of an array of bytes, somewhere, whose contents are "Literal string". Except when initializing an array.

2. Both of those produce equivalent behavior, nyes?

3. Your program doesn't do anything; why would the compiler treat it any differently from void main() { }? And main is supposed to return an integer.
Last edited by Rashakil Fol; May 5th, 2007 at 5:47 pm.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Help needed :(

 
0
  #3
May 5th, 2007
Originally Posted by laconstantine View Post
1. Look at this:
char *str = "Literal String";

2.
char x = 's';
1. This is creating a pointer to a char, writting "Literal String" at that address in memory

2. This is creating a char in memory, and assigning the value of 's' to it.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 70
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Help needed :(

 
0
  #4
May 6th, 2007
So here
char *str = "Literal String";
we creating a pointer str and then pointing it to "Literal String" and thats all?

we are not creating "Literal String" first and then pointing??

or its already loaded in the program like numbers 1234??
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 70
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Help needed :(

 
0
  #5
May 6th, 2007
Aha i got it
char *ptr = "Hello world";

the compiler automatically puts a null-character at the end of the literal string of characters "Hello world". It then creates a storage space for the resulting string - this is an array of const chars
and gives the ptr an address of the first element.

can you just tell me if its correct??
Last edited by laconstantine; May 6th, 2007 at 6:55 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help needed :(

 
0
  #6
May 6th, 2007
That's correct.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 70
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Help needed :(

 
0
  #7
May 6th, 2007
One more question please

here
char* szFirst = "Literal String";
char* szSecond = "Literal String";

szFirst[3] = 'q';
printf("szFirst (%s) is at %d, szSecond (%s) is at %d\n",
szFirst, szFirst, szSecond, szSecond);
The addresses are the same is it because when we declared this pointer
char *szFirst = "Literal String"
We also gave to some CELLS in memory this values "Literal String"
and then setting szFirst point at the first element there.
now if we want to use the same string "Literal String" the compiler wont let us to create another copy so he gives us the already declared string??

Its all i need to know guys.

Am i right here??
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help needed :(

 
0
  #8
May 6th, 2007
> szFirst[3] = 'q';
This is a really bad idea.
Literal strings can be made read-only by the implementation. So whilst you might be able to modify a string, all it will do for me is stop the program dead.

> The addresses are the same
Again, this is something which is implementation specific. A compiler is allowed to merge identical string literals, so all references to it are to a single unique string (and not one of many identical copies).

Neither of these properties matter if you don't try to modify a string literal.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 70
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Help needed :(

 
0
  #9
May 6th, 2007
So if we declare string literal once
for example here

1.
char *ptr = "String Literal";

and then i will try to use it again
2.
char *ptr2 = "String Literal";

The compiler sees that in 1 we already gave to "String Literal" a place in memory so the compiler dont want to create one more copy, and just giving to ptr2 the address of the already exist "String Literal"??

i mean
if you use identical strings to allocate string buffers, the compiler pools the strings. Thus, what was intended as multiple pointers to multiple memory places ends up as multiple pointers to a single memory place.

and it does it because it wont that our program will take more RAM.

Is it right?
Last edited by laconstantine; May 6th, 2007 at 11:13 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help needed :(

 
0
  #10
May 6th, 2007
Right, but again it is implementation specific. The language doesn't mandate it.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC