473,396 Members | 1,942 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,396 software developers and data experts.

const string which cause segmentation fault - any way to detect this bug during compile time?

Hi, consider the following bug at

#include <stdio.h>

class Drink {
public:
Drink( char *name_ ) : name( name_ ) {}
char *name;
char & get(int i) { return name[i]; }
};

void stir( char &ch ) { ch++; }

int main() {
Drink Martini("Martini");
printf("%c\n", Martini.get(2));
stir(Martini.get(2));
return 0;
}

which will dump out output:

r
Segmentation fault

I was understand that it is because "Martini" is a const string.
Run-time error will occur while trying to modify const string content.
Now, the challenge here is, is there any way to detect this bug during
compile time? Or is there any good programming practice to prevent
this?

My initial thought is that, re-design Drink object to use std::string.
What if the Drink code is copyrighted to others and I does not have
right to modify it? Is there anywhere I can improve it from client code
side (main function)??

Thank you very much!

-cheok

Jul 23 '05 #1
1 1729

<yc*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi, consider the following bug at

#include <stdio.h>

class Drink {
public:
Drink( char *name_ ) : name( name_ ) {}
char *name;
char & get(int i) { return name[i]; }
};

void stir( char &ch ) { ch++; }

int main() {
Drink Martini("Martini");
printf("%c\n", Martini.get(2));
stir(Martini.get(2));
return 0;
}

which will dump out output:

r
Segmentation fault

I was understand that it is because "Martini" is a const string.
Run-time error will occur while trying to modify const string content.
It might, it might not. The behavior is undefined.

Now, the challenge here is, is there any way to detect this bug during
compile time? Or is there any good programming practice to prevent
this?
Sure. Make 'get()' a const member function:

char &get(int i) const { return name[i]; }

Any member function which is not intended to modify
the object's state should be declared const.

My initial thought is that, re-design Drink object to use std::string.
That would allow you to modify the string, yes (unless the
string member were declared const). IMO std::string should
be preferred over 'C-style strings' anyway.
What if the Drink code is copyrighted to others and I does not have
right to modify it? Is there anywhere I can improve it from client code
side (main function)??


If you can't modify the code, then all you can do is
'follow the rules' (i.e. don't invoke undefined behavior).

-Mike
Jul 23 '05 #2

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
9
by: Gianguz | last post by:
The following code compiled with gcc version 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6) produce a segmentation fault and has a size of 9095 byte on my machine: #include...
3
by: Michaël Boland | last post by:
I have a fonction to trim a string : char *trimstr(char *pbuf1) { char *pbuf2 , *pbuf3; pbuf2 = pbuf1 ; pbuf3 = pbuf1 ; while(*pbuf1 != '\0') { if(*pbuf1 != ' ') { pbuf2 = pbuf1; pbuf2++;...
9
by: siliconwafer | last post by:
Hi All, Where is an identifier with a 'const' keyword given physical space in RAM ? Is it on stack or heap or data segment depending on storage class or is it treated as a part of code ,so kept...
48
by: Michel Rouzic | last post by:
I know it must sound like a newbie question, but I never really had to bother with that before, and I didn't even find an answer in the c.l.c FAQ I'd like to know what's the really proper way...
4
by: vivekian | last post by:
Hi, This is the part of the code am trying to compile to : void Server::respondToClient ( std::string response ) { .... .... if ((numbytes = sendto ( sockFd_ , response , sizeof(response)...
7
by: xgngli | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of...
4
by: junky_fellow | last post by:
Hi guys, Consider the following statements. const int *ptr; /* ie the contents pointed to by ptr cannot be changed */ My question is how/who prevents the contents from being modified ?...
13
by: S James S Stapleton | last post by:
I have some code, and I want to make it future-resistant. I have a bunch of variables that are set up run-time, and once set up, should act as constants. I don't want to #define them, because their...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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
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...

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.