473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(char *) to (const char *) is also dangerous but allowed?

C stops the conversion from (char **) to (const char **). c-faq.com
sec 11.10 has explanation on this point. But, for example, even the
conversion from (char *) to (const char *) brings the same dangerous
as in the previous conversion. Why the latter simple but dangerous one
is allowed in C?

$ cat f1.c
int main(void)
{
const char c = 'a';
char *p;
const char *cp = p;

cp = &c;
*p = 'x'; /*line 8*/

return 0;
}
$ cc -Aa -g f1.c
$ ./a.out
Bus error(coredump)
$ gdb -q ./a.out core
Core was generated by `a.out'.
Program terminated with signal 10, Bus error.

warning: The shared libraries were not privately mapped; setting a
breakpoint in a shared library will not work until you rerun the
program.

#0 0x29d4 in main () at f1.c:8
8 *p = 'x';
(gdb) quit
$

Apr 9 '07 #1
10 9363
On Apr 9, 10:53 am, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
$ cat f1.c
int main(void)
{
const char c = 'a';
char *p;
const char *cp = p;

cp = &c;
*p = 'x'; /*line 8*/

return 0;}
That crash has nothing to do with const char*. Remove all the lines
involving cp and c, and the program will crash just the same. This is
just a case of stupid programmer error.

Apr 9 '07 #2
lovecreatesbea. ..@gmail.com <lo************ ***@gmail.comwr ote:
C stops the conversion from (char **) to (const char **). c-faq.com
sec 11.10 has explanation on this point. But, for example, even the
conversion from (char *) to (const char *) brings the same dangerous
as in the previous conversion. Why the latter simple but dangerous one
is allowed in C?
$ cat f1.c
int main(void)
{
const char c = 'a';
char *p;
const char *cp = p;
cp = &c;
*p = 'x'; /*line 8*/
There isn't any issue with conversions between char and const char
at all here. The simple and only problem is that you use a random
address to store a value in. 'p' is never initialized, so there's
no memory assigned to it that you own. Instead 'p' will point to
some random position in memory. But you try to store a value at
that memory location anyway. This invokes undefined behaviour and
from now on anything can happen. You may get a segmentation fault,
a bus error, it may even appear to work or the (in)famous nasal
daemons could make their appearance.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Apr 9 '07 #3

"lovecreatesbea ...@gmail.com" <lo************ ***@gmail.comwr ote in message
>C stops the conversion from (char **) to (const char **). c-faq.com
sec 11.10 has explanation on this point. But, for example, even the
conversion from (char *) to (const char *) brings the same dangerous
as in the previous conversion. Why the latter simple but dangerous one
?
It isn't dangerous to cast a pointer to a const *, as long as the data is
set up correctly. Going in reverse from const * to a plain pointer is
potentially dangerous, and is only allowed because of weaknesses in the
language arising from the fact that const was an afterthought.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Apr 9 '07 #4
On Apr 9, 4:03 am, j...@toerring.d e (Jens Thoms Toerring) wrote:
lovecreatesbea. ..@gmail.com <lovecreatesbea ...@gmail.comwr ote:
C stops the conversion from (char **) to (const char **). c-faq.com
sec 11.10 has explanation on this point. But, for example, even the
conversion from (char *) to (const char *) brings the same dangerous
as in the previous conversion. Why the latter simple but dangerous one
is allowed in C?
$ cat f1.c
int main(void)
{
const char c = 'a';
char *p;
const char *cp = p;
cp = &c;
*p = 'x'; /*line 8*/

There isn't any issue with conversions between char and const char
at all here. The simple and only problem is that you use a random
address to store a value in. 'p' is never initialized, so there's
no memory assigned to it that you own. Instead 'p' will point to
some random position in memory. But you try to store a value at
that memory location anyway. This invokes undefined behaviour and
from now on anything can happen. You may get a segmentation fault,
a bus error, it may even appear to work or the (in)famous nasal
daemons could make their appearance.
I'm sorry to make a wrong example. The pointer p wasn't initiated. p
and cp ever pointed to a same address, cp was re-assigned, p wasn't
changed and had a random address all the time. I understand it now.
Thank you for your time.

Apr 9 '07 #5
lovecreatesbea. ..@gmail.com wrote:
Why the latter simple but dangerous one
is allowed in C?
Because the programmer is responsible for making sure that pointers have
legal values. Your program self-destructs for a reason completely
unrelated to the const-ness of the chars and and pointers-to-char.
>
$ cat f1.c
int main(void)
{
const char c = 'a';
char *p;
p is not initialized.
const char *cp = p;
p is still not initialized, and the value of cp is indeterminate,

cp = &c;
*p = 'x'; /*line 8*/
^^
dereferencing p, which has never been given a value.
>
return 0;
}
Apr 9 '07 #6
On Apr 9, 5:53 am, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
C stops the conversion from (char **) to (const char **). c-faq.com
sec 11.10 has explanation on this point. But, for example, even the
conversion from (char *) to (const char *) brings the same dangerous
as in the previous conversion. Why the latter simple but dangerous one
is allowed in C?

$ cat f1.c
int main(void)
{
const char c = 'a';
char *p;
const char *cp = p;

cp = &c;
*p = 'x'; /*line 8*/

return 0;}

$ cc -Aa -g f1.c
$ ./a.out
Bus error(coredump)
$ gdb -q ./a.out core
Core was generated by `a.out'.
Program terminated with signal 10, Bus error.

warning: The shared libraries were not privately mapped; setting a
breakpoint in a shared library will not work until you rerun the
program.

#0 0x29d4 in main () at f1.c:8
8 *p = 'x';
(gdb) quit
$
It's due to programming error.

However, you can change the value of const char using a pointer.

Ex:

#include <stdio.h>
int main ()
{
const char c = 'c';
char *p = &c;
*p = 'x';
printf("%c \n", c);
return 0;
}

However compiler thorws the warning of using a char pointer to a const
character.

$>cc del.c
"del.c", line 7: warning: assignment type mismatch:
pointer to char "=" pointer to const char

Cheers
-Vallabha
Apr 11 '07 #7
Vallabha wrote, On 11/04/07 06:30:

<snip>
However, you can change the value of const char using a pointer.
You can attempt to, but it invokes undefined behaviour which means that
anything can happen, including it causing your in-laws to move in
permanently. The most likely results are either the program crashing or
working as you expect. Don't do it.
Ex:

#include <stdio.h>
int main ()
Better to be explicit
int main(void)
{
const char c = 'c';
char *p = &c;
*p = 'x';
printf("%c \n", c);
return 0;
}

However compiler thorws the warning of using a char pointer to a const
character.
Some compilers do, but this is not required and not all compiler will.
--
Flash Gordon
Apr 11 '07 #8
Flash Gordon said:
[...] it invokes undefined behaviour which means
that anything can happen, including it causing your in-laws to move in
permanently.
The UB war just got a little bit nastier.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 11 '07 #9
>>>>"RH" == Richard Heathfield <rj*@see.sig.in validwrites:

RHFlash Gordon said:
>[...] it invokes undefined behaviour which means that anything
can happen, including it causing your in-laws to move in
permanently.
RHThe UB war just got a little bit nastier.

There's a reason the DS9000 was a commercial failure.

Charlton
--
Charlton Wilbur
cw*****@chromat ico.net
Apr 11 '07 #10

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

Similar topics

30
5073
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () { srand(time(0)); int array_length; int count;
8
4580
by: Simon | last post by:
Hi, I think this is slightly OT, (I am not certain that Macros are part of the standard), but I am hopping that someone could help. I am trying to use a language file in my system. the format of the file would be something like open=Open
11
813
by: Paul Emmons | last post by:
In writing a function similar to strstr(), I'm calling both of the arguments "const char *". My compiler (gcc) complains "warning: return discards qualifiers from pointer target type" unless I have the function return "const char*" as well (rather than just "char *"). In turn, any pointer receiving this value when calling the function must also be "const char *" or the compiler will complain, "warning: assignment discards qualifiers...
6
27772
by: G Patel | last post by:
I've been looking at some code for string functions (certain implementation of them) and the non modified string is usually declared as a const char *s in the parameter list. I was wondering, what exactly does that mean. Does that mean the function is restricted from changing the address (changing s) or changing what's being pointed to (*s)?
24
2348
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other compilers like GCC though. Many thanks! - Kevin
2
1940
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but when compiled with 6.0 it contains string from Vector (that's how I want it to work). std::vector<std::string> Vector; ... void MyClass::DoThis(std::vector<std::string> Vector) { const char *arrString;
42
32187
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
9
10529
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int strspcmp(const char * s1, const char * s2) {
35
34565
by: Sean Farrow | last post by:
Hi: What is best and safest way of converting a char* to a const char *? Can I use const_cast? Cheers Sean.
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
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 we have to send another system
3
2880
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.