473,725 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can reading (not writing) a bad pointer cause the program to crash later ?

4 New Member
Hello,

Let’s assume the following piece of code :

void f9 ()
{
char* c= "Could not execute f9";
throw c;
}

try
{
f9 ();
}
catch (char* c)
{
char message[4096];
sprintf (message, "Exception : %s", c);
cout << message;
}
This code works fine, its output is : "Exception : Could not execute f9".

Nevertheless, in the catch clause, the pointer c is indeterminate, the pointee may have been lost.

About such indeterminate (or “bad”) pointers, I have read the following :

If you are lucky, the dereference operation will crash or halt immediately (Java behaves this
way). If you are unlucky, the bad pointer dereference will corrupt a random area of
memory, slightly altering the operation of the program so that it goes wrong some
indefinite time later.
http://cslibrary.stanford.edu/102/PointersAndMemory.pdf
It seems I am unlucky, since my program has not crashed immediately.
But my question is : can it crash later because of this illicit dereferencing ?

In this case, I do not write in the area of memory pointed by by undeterminate pointer.
I only read it.

So, in such a case, can really the memory be corrupted so that my program may crash later ?

Thanks a lot for your ouputs
Apr 22 '09 #1
11 4052
newb16
687 Contributor
It is not lost as it's a constant string; It is not destroyed with f9 scope.
And yes, reading memory that doesn't belong to your program will cause program termination.
Apr 22 '09 #2
jmnat
4 New Member
thanks for your response, newb16, but in the catch clause of my program, we are no longer in f9 scope, so, there, it should be lost, isn't it ?
Apr 22 '09 #3
Banfa
9,065 Recognized Expert Moderator Expert
Strictly speaking reading an indeterminate pointer location is undefined behaviour. That means anything can happen including the program running as expected and demons flying out of your nose. What actually happens may change for what appears to be a completely unrelated event on the system.

Undefined behaviour should be avoided at all costs.

as newb16 pointed out though you you are returning an indeterminate pointer, it is a pointer to a constant string. What you should be doing is throwing and catching const char * to reflect the constness of the string.
Apr 22 '09 #4
jmnat
4 New Member
@Banfa
Thanks for your response, Banfa

I know that undefined behavior should be avoided, no problem with that.

But can you give me some insight about how reading such an indeterminate pointer can cause the program to crash later (after the reading of the pointer) ?

Thanks in advance for your help
Apr 22 '09 #5
jmnat
4 New Member
@Banfa
Here is what one can read about that in the book "More Effective C++" :

char *p = "Hello"; // non-const pointer, // non-const data

const char *p = "Hello"; // non-const pointer, // const data

char * const p = "Hello"; // const pointer, // non-const data

const char * const p = "Hello"; // const pointer, // const data

https://www.lenep.uenf.br/~bueno/Dis...EC/EI21_FR.HTM
According to the °C++ standard, the type of "Hello" is const char [], a type that's almost always treated as const char*. We'd therefore expect it to be a violation of const correctness to initialize a char* variable with a string literal like "Hello". The practice is so common in C, however, that the standard grants a special dispensation for initializations like this. Nevertheless, you should try to avoid them, because they're deprecated. ¤
https://www.lenep.uenf.br/~bueno/Dis...EC/EI21_FR.HTM
Could you clarify (I am a bit lost) ?

Thanks again
Apr 22 '09 #6
Banfa
9,065 Recognized Expert Moderator Expert
Yes, "Hello" is stored as part of the programs const data that is data used by the program that is known to be constant. This includes all string literals plus and any variables declared as const in the global scope.

However whether the const data is actually stored in physically unchangeable store or not is system dependent. For instance most PCs (MAC, Linux or Windows) store all data in RAM allocated from the system and so even the const data is stored in a memory location that is actually writeable, where as most embedded systems will store const data in the systems ROM (or more typically today EEPROM) so this data will be physically not writeable in the normal course of a program (of course will EEPROM you can always go the erase-rewrite route but that is not a simple assignment).

Because of that some C compilers for systems like PCs where everything is stored in RAM give "Hello" the type char * (or the systems char equivalent i.e. TCHAR * on Windows) where as others treat "Hello" as const char * even on systems where "Hello" is stored in RAM (for instance gcc I believe).

The only safe and portable thing to do is to treat string literals as constant, i.e. const char *.

However the C++ standard allows char *p = "Hello"; in order not to break the large body of C code out there that still assumes that string literals are not constant. But as your posted quite says that is deprecated behaviour, in new programs you should always treat string literals as const char *.
Apr 22 '09 #7
Banfa
9,065 Recognized Expert Moderator Expert
@jmnat
I don't need to, once the program has invoked undefined behaviour it is in trouble. There is no need or point in trying to work out how it might be in trouble as this could vary from system to system and compiler to compiler.
Apr 22 '09 #8
donbock
2,426 Recognized Expert Top Contributor
I suggest the following construction for defining c:
Expand|Select|Wrap|Line Numbers
  1. static const char * const c = "Could not execute f9";
"static" makes pointer c persistent beyond the scope of f9; the two "const" may be overkill, but they help to make sure that the value of c and the contents of the string are stable.
Apr 22 '09 #9
kanoop
1 New Member
jmnat,

Reading the pointer cannot make the program crash in a delayed fashion. Seems that the discussion here was misdirected due to your example code which, depending on the compiler, may or may not show the issue you were wanting to illustrate.

Either the pointer will point at an address which is not mapped by the processor (or is mapped to a page that your program is not allowed to access), in which case a page fault will result and your program will crash immediately you dereference the pointer.

Or the pointer will point to an address which is mapped and you'll get away with it.

The text you quoted talks about 'bad pointer dereference', but the author has assumed (without clearly stating so) that the dereference is for a write operation - hence the confusion.
Apr 23 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1673
by: Yang Lee | last post by:
Hi, Could you please try to run the following program it doesnt work for operator overloading function. It doesn't print peoper empName for emp2 object. It prints junk. Just what could be the cause. I get values for emp2 object from following DeltaEmp emp2=*emp1+10; where emp1 is a pointer to object of class DeltaEmp.
30
3734
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
6
2026
by: junw2000 | last post by:
Below is a simple code: #include <iostream> class base{ public: base(): i(11){std::cout<<"base constructor"<<'\n';} virtual void f() = 0; virtual ~base(){ std::cout<<"base destructor"<<'\n';} int i;
5
5082
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I have it store info it gets from when the programs check in into a DataSet (XML file). Problem is, that file now has to be used by other programs to find out version information (the file is ALWAYS less that 1K.) The record itself is only five fields...
111
20043
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- http://www.riscossione.info/
8
3467
by: jean.daniel.michaud | last post by:
Hi all, Something I don't get. The code is: // snippet on #include <list> #include <iostream> int main()
10
1352
by: Lilith | last post by:
I have a simple class method that receives as its second parameter an unsigned int. But, regardless of what I do it never receives the value that I pass to it. Instead it always comes up 4198769. I've passed the number as a literal, as a variable and as a constant and the method still recives the larger, unviable value. I've tried changing the value I pass to it and I've tried casting a constant and it still comes out wrong. I can't...
17
9940
by: byte8bits | last post by:
How does C++ safely open and read very large files? For example, say I have 1GB of physical memory and I open a 4GB file and attempt to read it like so: #include <iostream> #include <fstream> #include <string> using namespace std; int main () {
41
5401
by: biplab | last post by:
Hi all, I want to read the pixel values of a .bmp image(which will be input to the code) into a matrix...plz help me out as i can not understand how to achieve this... thanks in advance biplab
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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
9113
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...
0
6011
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
4519
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...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
2157
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.