Click here to Skip to main content
15,887,175 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: LPCWSTR Problem.? Pin
Richard MacCutchan2-Jan-11 1:14
mveRichard MacCutchan2-Jan-11 1:14 
AnswerRe: LPCWSTR Problem.? Pin
User 74293382-Jan-11 3:47
professionalUser 74293382-Jan-11 3:47 
GeneralRe: LPCWSTR Problem.? Pin
CPallini2-Jan-11 5:03
mveCPallini2-Jan-11 5:03 
GeneralRe: LPCWSTR Problem.? Pin
Stefan_Lang9-Jan-11 23:54
Stefan_Lang9-Jan-11 23:54 
GeneralRe: LPCWSTR Problem.? Pin
CPallini10-Jan-11 0:21
mveCPallini10-Jan-11 0:21 
GeneralRe: LPCWSTR Problem.? Pin
Stefan_Lang10-Jan-11 0:45
Stefan_Lang10-Jan-11 0:45 
GeneralRe: LPCWSTR Problem.? Pin
CPallini10-Jan-11 0:58
mveCPallini10-Jan-11 0:58 
GeneralRe: LPCWSTR Problem.? [modified] Pin
Stefan_Lang10-Jan-11 2:44
Stefan_Lang10-Jan-11 2:44 
Thanks for the link. My problem with the original question was that I didn't notice the 'C' part of the return type, assuming it was LPWSTR, like the cast on the return statement. Also the variable hello is not a const string pointer. At the very least the local variable used to temporarily store a pointer to the literal is a non-const pointer however, and thus should be usable for modifying the string it points to!

I've tested it in Visual Studio though, and it appears that even a non-const string pointer initialized to point to a string literal may not be used to change that literal: code that tries to do this does compile without warnings, but when run causes a run-time exception. For all I know this behaviour is Microsoft Specific.

Consider this example
char* DisplayInfo()
{
   char* hello = "hello!";
   return hello;
}

int _tmain(int argc, _TCHAR* argv[])
{
   char* h = DisplayInfo();
   if (strlen(h)>0)
       h[0] = 0;
	return 0;
}

When you call DisplayInfo, normally all you should care about is the function prototype, which states it returns a pointer to characters. By testing the string length you assert that the pointer is non-zero and points to an array of characters of non-zero length. it should be totally legitimate to alter individual characters within that array, as the return type was non-const.

But it turns out doing so might result in a run-time error, depending on how DisplayInfo is being implemented, and based on how string literals are being treated, even when dealing with non-const string types!

The link you provided mentions const string types only, and thus doesn't apply here - this is indeed compiler-specific, and might indeed just be Microsoft Specific.


Regarding my PS question: I was just being overly cautious. A dynamic library will only be loaded once it's used, not neccessarily upon starting the application that uses it. So the static variables will not exist from the start. I wondered whether for similar reasons the DLLs might be discarded once they are no longer needed. But of course, that is absurd, as the application wouldn't know in advance when a DLL is being called for the last time... Blush | :O


P.S. (another one):
I just noticed that the last section of the quote from the standard states
The C++ Standard wrote:
The effect of attempting to modify a string literal is undefined.

This implies you do in fact have a non-const string pointer. Before it says
The C++ Standard wrote:
An ordinary string literal has type "array of n const char"
(or const wchar_t).

How can you modify const char? I am confused now Confused | :confused:

modified on Monday, January 10, 2011 8:52 AM

AnswerRe: LPCWSTR Problem.? [updated] Pin
CPallini2-Jan-11 5:12
mveCPallini2-Jan-11 5:12 
GeneralRe: LPCWSTR Problem.? Pin
Sauro Viti2-Jan-11 5:39
professionalSauro Viti2-Jan-11 5:39 
GeneralRe: LPCWSTR Problem.? Pin
CPallini2-Jan-11 5:59
mveCPallini2-Jan-11 5:59 
GeneralRe: LPCWSTR Problem.? Pin
Sauro Viti2-Jan-11 6:03
professionalSauro Viti2-Jan-11 6:03 
GeneralRe: LPCWSTR Problem.? Pin
CPallini2-Jan-11 6:33
mveCPallini2-Jan-11 6:33 
GeneralRe: LPCWSTR Problem.? Pin
goldenrose94-Jan-11 0:11
goldenrose94-Jan-11 0:11 
GeneralRe: LPCWSTR Problem.? Pin
CPallini4-Jan-11 0:27
mveCPallini4-Jan-11 0:27 
AnswerRe: LPCWSTR Problem.? [modified] Pin
Stefan_Lang10-Jan-11 0:21
Stefan_Lang10-Jan-11 0:21 
QuestionAdding a reference to MFC Project.... Pin
AmbiguousName1-Jan-11 8:09
AmbiguousName1-Jan-11 8:09 
QuestionHow can I initialize an CListBox& null reference ? Pin
mesajflaviu1-Jan-11 4:57
mesajflaviu1-Jan-11 4:57 
AnswerRe: How can I initialize an CListBox& null reference ? Pin
BicycleTheif1-Jan-11 5:28
BicycleTheif1-Jan-11 5:28 
GeneralRe: How can I initialize an CListBox& null reference ? Pin
mesajflaviu1-Jan-11 5:32
mesajflaviu1-Jan-11 5:32 
GeneralRe: How can I initialize an CListBox& null reference ? Pin
«_Superman_»1-Jan-11 7:41
professional«_Superman_»1-Jan-11 7:41 
AnswerRe: How can I initialize an CListBox& null reference ? Pin
CPallini1-Jan-11 7:54
mveCPallini1-Jan-11 7:54 
AnswerRe: How can I initialize an CListBox& null reference ? Pin
bleedingfingers2-Jan-11 20:08
bleedingfingers2-Jan-11 20:08 
QuestionHow can send message to an notepad ? [modified] Pin
mesajflaviu1-Jan-11 2:53
mesajflaviu1-Jan-11 2:53 
AnswerRe: How can send message to an notepad ? Pin
Espen Harlinn1-Jan-11 4:43
professionalEspen Harlinn1-Jan-11 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.