Board index » Visual Studio » Access denied on char*

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.


-
 

Re:Access denied on char*

On 21 Dec 2005 22:51:38 -0800, "Amal P" <enjoyamalp@gmail.com>wrote:



Quote
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"};



That can be expressed more simply and conventionally as:



char* pData = "0123456789";



Quote
printf( "%s", pData );



That's equivalent to the simpler:



puts(pData);



Quote
pData[0] = 'a';



The "pData" points to the first character of a string literal, and writing

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

-

Re:Access denied on char*

its still working fine !, but did u try with a char array??



-

Re:Access denied on char*

"Amal P" <enjoyamalp@gmail.com>wrote in message

Quote
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;

}



The string literal is technically const. I think its an implementation

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]





-

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.



-

Re:Access denied on char*

may be thru tables with flags?? :-??



-

Re:Access denied on char*

Amal P wrote:



Quote
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.





Amal:



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



-

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

Quote
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.







-

Re:Access denied on char*

On 21 Dec 2005 23:51:02 -0800, "Amal P" <enjoyamalp@gmail.com>wrote:



Quote
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?



For starters, see:



Memory Protection

msdn.microsoft.com/library/default.asp=true">msdn.microsoft.com/library/default.asp=true



--

Doug Harrison

Visual C++ MVP

-

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

news:1135234298.421329.58270@o13g2000cwo.googlegroups.com...

>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;

>}



The string literal is technically const. I think its an implementation

decision, but may (should?) be stored in read-only memory. My recollection

is that VC6 did not do this and that VC7 does.

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

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:



Quote
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.

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-