944,033 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6005
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 5th, 2007
0

Help needed :(

Expand Post »
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 .
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 5th, 2007
1

Re: Help needed :(

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.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
May 5th, 2007
0

Re: Help needed :(

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.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
May 6th, 2007
0

Re: Help needed :(

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??
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 6th, 2007
0

Re: Help needed :(

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 6th, 2007
0

Re: Help needed :(

That's correct.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 6th, 2007
0

Re: Help needed :(

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??
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 6th, 2007
0

Re: Help needed :(

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 6th, 2007
0

Re: Help needed :(

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 6th, 2007
0

Re: Help needed :(

Right, but again it is implementation specific. The language doesn't mandate it.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ and data access
Next Thread in C++ Forum Timeline: library in visual c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC