| | |
Help needed :(
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2007
Posts: 70
Reputation:
Solved Threads: 1
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
.
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
. 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
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.
•
•
Join Date: May 2007
Posts: 70
Reputation:
Solved Threads: 1
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??
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.
•
•
Join Date: May 2007
Posts: 70
Reputation:
Solved Threads: 1
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??

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??
> 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.
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.
•
•
Join Date: May 2007
Posts: 70
Reputation:
Solved Threads: 1
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?
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.
![]() |
Similar Threads
- Harddrive failure and 100% CPU under Win2K, Help Needed! (Windows NT / 2000 / XP)
- XP PRO iis help needed (Windows NT / 2000 / XP)
- CWS. help needed (Viruses, Spyware and other Nasties)
- are all these needed (Windows NT / 2000 / XP)
- help much needed !! (OS X)
Other Threads in the C++ Forum
- Previous Thread: C++ and data access
- Next Thread: library in visual c++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






