Board index » Visual Studio » Access denied on char*
|
JanScheider
|
|
JanScheider
|
Access denied on char*
Visual Studio216
This program is crashing while running. It is getting exception access denied at point pData[0] = 'a'; But same program works fine with Turbo C. Does anyone know the concept behind this? int main(int argc, char* argv[]) { char* pData = {"0123456789"}; printf( "%s", pData ); pData[0] = 'a'; printf( "%s", pData ); return 0; } Thanks and Regards, Amal P. - |
| Doug
Registered User |
Thu Dec 22 01:24:58 CST 2005
Re:Access denied on char*
On 21 Dec 2005 22:51:38 -0800, "Amal P" <enjoyamalp@gmail.com>wrote:
QuoteThis program is crashing while running. It is getting exception access char* pData = "0123456789"; Quoteprintf( "%s", pData ); puts(pData); QuotepData[0] = 'a'; to string literals is undefined. VC++ combines identical string literals when string pooling is in effect, and it also places them into read-only memory; writing to this read-only memory is what's causing you to crash. To modify the string, make the pointer an array, and initialize it like this: char data[] = "0123456789"; Leaving the size off the array declaration causes the compiler to set aside enough space to hold the entire string literal, including the nul at its end. So in this case, the actual type of "data" is: char data[11]; You can write anywhere within this array. -- Doug Harrison Visual C++ MVP - |
| Shark
Registered User |
Thu Dec 22 01:29:42 CST 2005
Re:Access denied on char*
its still working fine !, but did u try with a char array??
- |
| Jeff
Registered User |
Thu Dec 22 01:28:33 CST 2005
Re:Access denied on char*
"Amal P" <enjoyamalp@gmail.com>wrote in message
QuoteThis program is crashing while running. It is getting exception access decision, but may (should?) be stored in read-only memory. My recollection is that VC6 did not do this and that VC7 does. -- Jeff Partch [VC++ MVP] - |
| Amal
Registered User |
Thu Dec 22 01:51:02 CST 2005
Re:Access denied on char*
Hi
I have found that working if it is an array. Thanks for the information. But my question was why is it failing while writing. What i understood from your reply is that it is happening because the data is stored into the read only memory. But what is actually the concept of this read only memory? If at first it can be written to that location why it cant be written again? How OS prevents it? Can you give me a detailed idea behind this? Thanks and regards, Amal P. - |
| Shark
Registered User |
Thu Dec 22 02:17:25 CST 2005
Re:Access denied on char*
may be thru tables with flags?? :-??
- |
| David
Registered User |
Thu Dec 22 05:50:46 CST 2005
Re:Access denied on char*
Amal P wrote:
QuoteThis program is crashing while running. It is getting exception access The type of a string literal in C++ is const char*. You should always write const char* pData = "0123456789"; Then the compiler will prevent you from changing the contents. The syntax char* pData = "0123456789"; is only allowed in C++ for backward compatibilty with old C programs. Don't use it!! To solve your problem, as others have noted, you should use an array char data[] = "0123456789"; David Wilkinson - |
| Alexander
Registered User |
Thu Dec 22 10:18:11 CST 2005
Re:Access denied on char*
The read-only memory means READ ONLY. The data is initialized when the
executable is loaded (actually when this address is paged in). There is no write there on run time. The page attributes set to READ ONLY, which does hardware protection from write. Any attempt to write to that area causes access violation exception. "Amal P" <enjoyamalp@gmail.com>wrote in message QuoteHi - |
| Doug
Registered User |
Thu Dec 22 11:31:40 CST 2005
Re:Access denied on char*
On 21 Dec 2005 23:51:02 -0800, "Amal P" <enjoyamalp@gmail.com>wrote:
QuoteHi Memory Protection msdn.microsoft.com/library/default.asp=true">msdn.microsoft.com/library/default.asp=true -- Doug Harrison Visual C++ MVP - |
| Joseph
Registered User |
Thu Dec 22 13:02:42 CST 2005
Re:Access denied on char*
VC6 and VC7 both place the string in the const (read-only) area. However, in VC6, the
data type of a string literal was "char *" but in VC7, the data type is "const char *". The difference being that if you had a function void whatever(char * p); then in VC6, you could write whatever("abc"); but in VC7, this will generate a compiler error. Essentially VC7 makes the data type consistent with the actual storage mechanism that was used. VC6 used what was actually the wrong data type. joe On Thu, 22 Dec 2005 01:28:33 -0600, "Jeff Partch [MVP]" <jeffp@mvps.org>wrote: Quote"Amal P" <enjoyamalp@gmail.com>wrote in message Web: www.flounder.com">www.flounder.com MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm - |
| Joseph
Registered User |
Thu Dec 22 12:59:35 CST 2005
Re:Access denied on char*
The concept of read-only memory is that it is, well, read-only. Any attempt to write to
it will result in an access fault. It cannot be written to at all. It is initialized during the program load. The OS doesn't directly prevent it; the actual hardware prevents it. The OS sets the hardware up to do this. The way this is done is by the OS setting flags in the page table. When an address is mapped, the access desired (e.g., write access) is checked against the rights granted in the page table; if the accesses are incompatible, an access fault occurs. Thus a page may be marked as inaccessible (not readable, not writeable), read-only (not writeable), execute-only (only an instruction-fetch cycle can read the contents) or non-executable (any attempt to execute code is denied; this is a new feature of high-end machines to prevent buffer overrun code exploits). Once the bits are set, the enforcement is done by the actual hardware. joe On 21 Dec 2005 23:51:02 -0800, "Amal P" <enjoyamalp@gmail.com>wrote: QuoteHi Web: www.flounder.com">www.flounder.com MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm - |
