Can some 1 explain why this behaviour


 
Thread Tools Search this Thread
Top Forums Programming Can some 1 explain why this behaviour
# 1  
Old 12-16-2005
Can some 1 explain why this behaviour

#include <iostream>
using namespace std;

int main()
{
const int l_test = 999999999;
int *l_ptr = (int*) &l_test;

cout<<"Constant Addr:"<<&l_test<<" Value:"<<l_test<<endl;
cout<<"Pointer Addr:"<<l_ptr<<" Value:"<<*l_ptr<<endl;

*l_ptr = 888888888; // Manipulating the pointer

cout<<"-------------------------------"<<endl;
cout<<"Constant Addr:"<<&l_test<<" Value:"<<l_test<<endl;
cout<<"Pointer Addr:"<<l_ptr<<" Value:"<<*l_ptr<<endl;

return 0;
}

The o/p for this program is

Constant Addr:0012FF7C Value:999999999
Pointer Addr:0012FF7C Value:999999999
-------------------------------
Constant Addr:0012FF7C Value:999999999
Pointer Addr:0012FF7C Value:888888888


please look into the last 2 lines,
the question is : how can same address 0012FF7C have 2 different values 888888888 & 999999999
Thanks , Smilie
# 2  
Old 12-16-2005
You're violating basic rules. You tell the compiler l_test is a constant. That means you GUARANTEE not to mess with it - specifically you say 'I promise I will not change this to 888888888'.

What you're seeing is called 'undefined behavior', because you broke all the rules.
# 3  
Old 12-18-2005
Indeed, you're breaking the rules. If you made I_test a global variable, your program would probably segfault as it attempted to write to protected memory! But since it's a stack variable it can't protect it from roundabout tampering like this.

What's probably happening is the compiler is going "Hmm, since it's a constant, I don't have to actually *read* it every time -- it won't change. I'll just keep it in a CPU register to save time." So when you change the memory, the copy in the register doesn't change with it.

The const specifier is more a reminder to the programmer, to tell them that they're really not supposed to be writing to some memory from this function. The truly determined might get around that with pointers and typecasting, but if you made it const in the first place you probably had a reason.

Last edited by Corona688; 12-18-2005 at 03:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Ps command different behaviour

Hi Experts, ps command behavior in Redhat is such that it outputs all the output(of long lengths). In Unix the ps command output was limited to only 80 chars. In that if you pipe its output to another command hen the 80 chars restriction wouldn't be there. This 80 char limitation will only be... (14 Replies)
Discussion started by: Albert_Pinto7
14 Replies

2. Programming

Sort behaviour

I see strange results when sorting with -n options and I wander if somebody can explain it. Input file and two results: $ cat aa 14 -1 11 -1 0 -1 0 $ sort -u aa -1 0 (1 Reply)
Discussion started by: migurus
1 Replies

3. UNIX for Dummies Questions & Answers

behaviour of awk

Can someone explain how the below awk simulates cat? pandeeswaran@ubuntu:~$ awk '1' file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO 92000 4087.5625 4 96 pandeeswaran@ubuntu:~$ awk '2' file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO... (3 Replies)
Discussion started by: pandeesh
3 Replies

4. Programming

different behaviour in fg and bg

fg = foreground bg = background I have a cobol program that I start with a very simple script. The script is not at fault as it has not changed and the program worked in fg and bg before. I have altered the logging in the program and moved my cursor declare to working storage. The program runs... (6 Replies)
Discussion started by: Bruble
6 Replies

5. Shell Programming and Scripting

cp -R behaviour

i 've noticed the following difference between freebsd cp and gnu cp from the freebsd cp man page: -R ... If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. ... on gnu cp from the man pagewhile on gnu cp manpage: ‘-r'... (2 Replies)
Discussion started by: aegis
2 Replies

6. Shell Programming and Scripting

Why this behaviour of IF condition?

I have a variable, defndata, which is a number (fetched from a file using awk). I want that if defndata is not initialized (that is its not found in the file using awk), then to execute a block of statements, otherwise execute another block. if then .... else ... fi Now this... (4 Replies)
Discussion started by: indianjassi
4 Replies

7. Shell Programming and Scripting

A Strange Behaviour!!!

Can some-one give me a view to this : I have a directory in an unix server, having permissions r-xr-xr-x .This directory is basically a source directory. Now there is another directory basically the destination directory which has all the permissions. Note:I log in as not the owner,but user... (5 Replies)
Discussion started by: navojit dutta
5 Replies

8. Programming

Different behaviour of this program

Hi, I have one doubt, in the below program, if I declare char *b inside the main(), the function compiles & runs properly. But at the same time, if I declare it globally it compiles but when we run it, it creates core dump (segmentation fault) both in C & C++. It is not being trapped by catch... (7 Replies)
Discussion started by: royalibrahim
7 Replies

9. Shell Programming and Scripting

Count behaviour when using su -

Gentlemen, OK, I have an odd issue here perhaps someone can shed some light for me. When running a script as its user/owner the below pause/wait works just fine. When a developer has used su - to assume the username the pause does not happen... This is a HPUX11.00 machine, I have copied the blurb... (1 Reply)
Discussion started by: Eronysis
1 Replies

10. Programming

Behaviour of default

Hi, If I have the following code : int i=2; switch(i) { case 1 : {};break; default : { printf("d"); };break; case 2 : { printf("2"); };break; } what will be the output? I had an understanding that case statements are taken sequentially so default will be executed. But... (5 Replies)
Discussion started by: soorajmu
5 Replies
Login or Register to Ask a Question