473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

modifying stack variable

Why is f() giving access violation when called from tf1() or tf2() ?
Is this a compiler and/or language bug?

void f(char* psz)
{
*psz = 0;
}

void tf1()
{
char* p = "test";
f(p);
}

void tf2()
{
f("test");
}
Jul 23 '05 #1
6 1573
Uenal Mutlu schrieb:
Why is f() giving access violation when called from tf1() or tf2() ?
Is this a compiler and/or language bug?
No, string literals are of type const char*, the conversion to char* is
provided for C compatibility, but deprecated. When you modify the
pointed-to string literal, that invokes undefined behaviour (which
usually manifests itself in an A/V in this case, but anything could happen).
void f(char* psz)
{
*psz = 0; here ^^
}

void tf1()
{
char* p = "test"; char p[] = "test"; f(p);
}


What compiler are you using? It should have warned you about this.

Cheers,
Malte
Jul 23 '05 #2
"Malte Starostik" wrote
Uenal Mutlu schrieb:
Why is f() giving access violation when called from tf1() or tf2() ?
Is this a compiler and/or language bug?


No, string literals are of type const char*, the conversion to char* is
provided for C compatibility, but deprecated. When you modify the
pointed-to string literal, that invokes undefined behaviour (which
usually manifests itself in an A/V in this case, but anything could happen).
void f(char* psz)
{
*psz = 0;

here ^^
}

void tf1()
{
char* p = "test";

char p[] = "test";
f(p);
}


What compiler are you using? It should have warned you about this.


Microsoft's VS6. No, it does not give a warning, even if using the highest
warning level (4).
Jul 23 '05 #3
"Uenal Mutlu" <52***************@t-online.de> wrote in message
news:d2*************@news.t-online.com...
"Malte Starostik" wrote
Uenal Mutlu schrieb:
Why is f() giving access violation when called from tf1() or tf2() ?
Is this a compiler and/or language bug?


No, string literals are of type const char*, the conversion to char* is
provided for C compatibility, but deprecated. When you modify the
pointed-to string literal, that invokes undefined behaviour (which
usually manifests itself in an A/V in this case, but anything could happen).
If it's not too much work for you could you tell me where in the standards
this is stated? Where should I look (book, paragraph etc.)?
void f(char* psz)
{
*psz = 0;

here ^^
}

void tf1()
{
char* p = "test";

char p[] = "test";
f(p);
}


What compiler are you using? It should have warned you about this.


Microsoft's VS6. No, it does not give a warning, even if using the highest
warning level (4).

Jul 23 '05 #4
Uenal Mutlu schrieb:
"Malte Starostik" wrote
Uenal Mutlu schrieb:
Why is f() giving access violation when called from tf1() or tf2() ?
Is this a compiler and/or language bug?

[string literal to char* conversion]
What compiler are you using? It should have warned you about this.

Microsoft's VS6. No, it does not give a warning, even if using the highest
warning level (4).


That's a pity. Many warnings are indeed bogus, I can't imagine where
this one would be undue.
Anyway, just remember: don't assign a string literal to a non-const
char* variable. Either use:

void f(char* p);

const char* p = "test";
f(p);

This is only safe as long as you absolutely know f() will
never modify p. This should only ever be needed if
f() is a C function that doesn't declare its argument const

or, if you want a non-const char array that you can write to:
char p[] = "test";
f(p);

This one is safe as long as you know f will not write past
the array's end.
Whenever possible, use std::string instead of (const or not) char*, it
saves a lot of headaches.

Cheers,
Malte
Jul 23 '05 #5
> > "Malte Starostik" wrote
Uenal Mutlu schrieb:
> Why is f() giving access violation when called from tf1() or tf2() ?
> Is this a compiler and/or language bug?

No, string literals are of type const char*, the conversion to char* is
provided for C compatibility, but deprecated. When you modify the
pointed-to string literal, that invokes undefined behaviour (which
usually manifests itself in an A/V in this case, but anything could
happen).
If it's not too much work for you could you tell me where in the standards
this is stated? Where should I look (book, paragraph etc.)?


Ok, found a workaround, and a reference:

//################################################## ###########
PRB: Use of /ZI May Cause Access Violation
Q198477
--------------------------------------------------------------------------------
The information in this article applies to:
a.. Microsoft Visual C++, 32-bit Editions, version 6.0
--------------------------------------------------------------------------------
SYMPTOMS
Use of the compiler switch /ZI (Program Data Base for Edit and Continue) may
cause an access violation if you try to modify a text string.

RESOLUTION
Use one of the following workarounds:
a.. In the sample below, change "char * ptr" to "char ptr[]".
b.. Do not use the /ZI switch.

STATUS
As per C++ Standard (2.13.4.2) the effect of attempting to modify a string
literal is undefined.

MORE INFORMATION
By default the compiler switch /ZI (Program Data Base for Edit and Continue) uses
the compiler switch /GF.

The /GF option enables the compiler to pool strings and place them in read-only
memory. By placing the strings in read-only memory, the operating system does not
need to swap that portion of memory. Instead, it can read the strings back from
the image file. It is a good idea to do this as it saves pages of memory from
being written to and therefore reduces the working set used by the application.
In addition, it allows those pages to be shared between multiple instances of the
process that use that image file (.exe or .dll file), further reducing total
memory usage in the entire system. Strings placed in read-only memory cannot be
modified; if you try to modify them, you will see an Application Error dialog
box.

The following code when executed after compile produces an access violation.
Sample Code

// Test.cpp
// Compile with: cl /ZI /Od test.cpp

int main ()
{
char* ptr = "Hello World";
ptr[3] = 'Q'; //Access violation
return 0;
}

Since /ZI is used for debugging with Edit and Continue, the above code works
fine in the Release build, where the /ZI switch is not used.
REFERENCES
Additional query words: string strings arrays initialize initialise aggregate
access violation

Keywords : kbCompiler kbVC600
Issue type : kbprb
Technology :
//################################################## ###########
Jul 23 '05 #6
Uenal Mutlu schrieb:
"Uenal Mutlu" <52***************@t-online.de> wrote in message
news:d2*************@news.t-online.com...
"Malte Starostik" wrote
Uenal Mutlu schrieb:

Why is f() giving access violation when called from tf1() or tf2() ?
Is this a compiler and/or language bug?

No, string literals are of type const char*, the conversion to char* is
provided for C compatibility, but deprecated. When you modify the
pointed-to string literal, that invokes undefined behaviour (which
usually manifests itself in an A/V in this case, but anything could happen).

If it's not too much work for you could you tell me where in the standards
this is stated? Where should I look (book, paragraph etc.)?


section 4.2 "Array-to-pointer conversion", paragraph 2 defines the
conversion, Annex D.4 deprecates it.

2.3.4, paragraph 2 makes the modification of a string literal undefined

HTH,
Malte
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: news.tkdsoftware.com | last post by:
Aside from comp.compilers, is there any other forum, newsgroup or medium where I can post questions concerning the development of a byte code compiler & virtual stack machine? --
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
2
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
9
by: shine | last post by:
what is the difference between a heap and a stack?
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
5
by: dav3 | last post by:
I am by no means an ultra slick programmer and my problem solving skills.. well they leave much to be desired. That being said I have been working on the following problem for the past few days and...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.