473,508 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't dereference pointer to stack?

Why does the following give a segmentation fault?

void breakme(char* st) {
char* cp = st;
*cp = 'x' // This is the problem line.
}

int main() {
char* mine = "teststringfortestingpurposes";
breakme(mine);
}

Feb 10 '06 #1
9 2014
* Tu*********@gmail.com:
Why does the following give a segmentation fault?

void breakme(char* st) {
char* cp = st;
*cp = 'x' // This is the problem line.
}

int main() {
char* mine = "teststringfortestingpurposes";
breakme(mine);
}


Why not?

The program has undefined behavior.

Therefore, anything can happen, including a SIGSEGV.

If instead you ask, why does C++ allow that particular nastie?

That's to maintain backwards compatibility with C.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 10 '06 #2

Tu*********@gmail.com wrote:
Why does the following give a segmentation fault?

void breakme(char* st) {
char* cp = st;
*cp = 'x' // This is the problem line.
}

int main() {
char* mine = "teststringfortestingpurposes";


This string is constant and cannot be changed. Change to "char mine[]
= " and it will work. This is because you will be declairing an array
of char instead of a pointer to an unmutable string.

Feb 10 '06 #3
Tu*********@gmail.com wrote:
Why does the following give a segmentation fault?

void breakme(char* st) {
char* cp = st;
*cp = 'x' // This is the problem line.
}

int main() {
char* mine = "teststringfortestingpurposes";
breakme(mine);
}

Modifying a static string literal is - whilst legal - undefined. The
statement
char* mine = "teststringfortestingpurposes";
is actually converting a const char * to char * (again which is legal
but deprecated). Result - undefined. In your case (probably most
compilers will do the same) - dummy spit.

--dakka
Feb 10 '06 #4
Thanks everyone.

This was from a string manipulation function I wrote that was seemingly
not working. The worst part is that it would have worked if I hadn't
used a string literal to test it.

Feb 10 '06 #5
hmm..
now I'm getting worried. Has something happened with c++, that
I am not aware of?
you can even do:(if you like to i dont see why though)
void breakme(char* st) {
char* cp = st;
*cp = 'x'; }
int main() {
breakme("teststringfortestingpurposes");
}
This is a basic pointer..
I am a bit confused now.
And just so you know, I tested this (borland compiler set to ansi
complience and force c++ compile).
Please help me understand.
/Jesper

Feb 10 '06 #6
je****@alphacash.se wrote:
hmm..
now I'm getting worried. Has something happened with c++, that
I am not aware of?
you can even do:(if you like to i dont see why though)
void breakme(char* st) {
char* cp = st;
*cp = 'x'; }
int main() {
breakme("teststringfortestingpurposes");
}
This is a basic pointer..
I am a bit confused now.
And just so you know, I tested this (borland compiler set to ansi
complience and force c++ compile).
Please help me understand.


Understand what? You don't mention what it is that confuses you.

The code fragments shown exhibit undefined behavior. It may seem like a
tautology, but there is no defined behavior for undefined behavior. The
code is broken and should not be used, but you can't expect anything in
particular from it.

Brian

Feb 10 '06 #7

Alf P. Steinbach wrote:
* Tu*********@gmail.com:
Why does the following give a segmentation fault?

void breakme(char* st) {
char* cp = st;
*cp = 'x' // This is the problem line.
}

int main() {
char* mine = "teststringfortestingpurposes";
breakme(mine);
}


Why not?

The program has undefined behavior.

Therefore, anything can happen, including a SIGSEGV.

If instead you ask, why does C++ allow that particular nastie?

That's to maintain backwards compatibility with C.


The idea that modification of literals is undefined in C++ for the sake
of compatibility with C is quite ridiculous.

Other languages have similar restrictions against what is essentially
self-modifying code.

Modification of literals is also undefined in Lisp for instance. Is
that for compatibility with C also?

(setf (car '(a b c)) 42) ;; Nasal demons!
(setf (char "abc" 1) #\z) ;; ditto

This type of liberty with respect to literals allows for ROM-able code:
both the program code and its literal data can be compiled into a
single program image, which can then be put into write-protected
memory. The program can reference that data using a direct pointer into
that memory.

A literal should be thought of as a component of the program itself,
and not some external data. Through a literal quoting mechanism, a
program gains access to a piece of itself which it can use as data in a
computation.

If such objects had to be modifiable, then only their initial values
could be stored in that read-only memory. Modifiable storage would have
to be allocated for them at program startup, and the initial values
copied there. This is a waste of time and storage for objects which are
treated as immutable.

And let's not forget that immutable objects can also be compressed
together to save space through substructure sharing. If one string
literal matches a suffix of another, or possible the entire string,
then it can be represented as a pointer to that suffix. If those
objects could be modified, there would be surprising behaviors.
Changing one literal would change some unrelated string that shares
storage with it.

Many C and C++ implementations for virtual memory systems provide a
nice, accurate diagnostic for this error. The compiler places string
literals into the same object file sections as code, so literals are
co-allocated with code. The dynamic loader maps the program's
executable image file into the process address space as read only, so
any self modification (code or literal data) results in an instant
violation. It's a nice setup.

Feb 10 '06 #8
* Kaz Kylheku:
Alf P. Steinbach wrote:
* Tu*********@gmail.com:
Why does the following give a segmentation fault?

void breakme(char* st) {
char* cp = st;
*cp = 'x' // This is the problem line.
}

int main() {
char* mine = "teststringfortestingpurposes";
breakme(mine);
}

Why not?

The program has undefined behavior.

Therefore, anything can happen, including a SIGSEGV.

If instead you ask, why does C++ allow that particular nastie?

That's to maintain backwards compatibility with C.


The idea that modification of literals is undefined in C++ for the sake
of compatibility with C is quite ridiculous.


Yes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 10 '06 #9

<je****@alphacash.se> skrev i meddelandet
news:11**********************@g43g2000cwa.googlegr oups.com...
hmm..
now I'm getting worried. Has something happened with c++, that
I am not aware of?
you can even do:(if you like to i dont see why though)
void breakme(char* st) {
char* cp = st;
*cp = 'x'; }
int main() {
breakme("teststringfortestingpurposes");
}
This is a basic pointer..
Yes, but the type of the string literal is 'const char*'. It is
convertible to non-const char*, just to be compatible with old C code.

Actually modifying (*cp = 'x';) the const data is what is the
undefined behaviour.

On some other compilers, this would cause a segfault, as the string
literal could be stored in a read-only segment. As usual though, one
possible outcome of the undefined behaviour is the nasty 'seems to
work'.
I am a bit confused now.


We all are. .-)
Bo Persson
Feb 11 '06 #10

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

Similar topics

26
7031
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
11
1457
by: Tamas Demjen | last post by:
I was shocked to learn that VC++ 2005 Beta 2 can't catch Access Violation exceptions in unmanaged code. To reproduce this, I created a minimal Win32 console application: #include "stdafx.h" ...
6
5387
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
26
3023
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
26
4466
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Do you think we can reach any kind of consensus on whether the following code's behaviour is undefined by the Standard? int my_array; int const *const pend = *(&my_array + 1); Considering...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.