Is it possible that a single adress is shared by two distinct values ?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 6371 Views - Last Post: 26 June 2011 - 02:31 PM Rate Topic: -----

#1 chauhan.vikram00  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 6
  • Joined: 24-June 11

Is it possible that a single adress is shared by two distinct values ?

Posted 24 June 2011 - 09:54 AM

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const int a=100;
int *p;
(const*)p=&a;
(*p)++;
int *add1;
(const*)add1=&a;
cout<<"a="<<a<<"\t&a="<<add1<<"\t*p="<<*p<<"\tp="<<p;

getch();
}


After running the given program there is single address is sharing by two distinct values .
Please may you explain to me hurry ...............
Thanks

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 24 June 2011 - 10:02 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible that a single adress is shared by two distinct values ?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 24 June 2011 - 10:05 AM

After converting your code to something compilable on a compiler newer than 20 years old, here's the problems:

whawhawha.cpp: In function ‘int main()’:
whawhawha.cpp:9: error: ISO C++ forbids declaration of ‘type name’ with no type
whawhawha.cpp:9: warning: target of assignment not really an lvalue; this will be a hard error in the future
whawhawha.cpp:12: error: ISO C++ forbids declaration of ‘type name’ with no type
whawhawha.cpp:12: warning: target of assignment not really an lvalue; this will be a hard error in the future


Please...petition your school -- or your country in this case -- to DROP THE ANCIENT TURBO C++ COMPILER!!!

I do believe you're engaging in undefined behavior.
Was This Post Helpful? 3
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 24 June 2011 - 10:05 AM

First of all your program invokes nasal demons undefined behavior since you're mutating a const value, so any possible behavior the program might exhibit would be legal according to the standard.

That being said here's the most probable explanation why you get the behavior your get on your particular system:

There are of course not two different values at the same address. The compiler probably optimizes your cout line to something like cout << "a=" << 100 <<"\t&a="<<add1<<"\t*p="<<*p<<"\tp="<<p;. Since the compiler knows the value of a is not allowed to change, so it can only legally be 100, that's a perfectly valid optimization for the compiler to make. There's no need to read the value from memory when the value is already "known" at compile time.
Was This Post Helpful? 2
  • +
  • -

#4 chauhan.vikram00  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 6
  • Joined: 24-June 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 24 June 2011 - 10:40 AM

Sir
we r not asking the errors which comes in program,that all we know about our complier
we want a fresh reply for this one
please answer our question instead of counting error
thanks sir for your advice
Was This Post Helpful? -4
  • +
  • -

#5 chauhan.vikram00  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 6
  • Joined: 24-June 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 24 June 2011 - 10:46 AM

sir
Is there any explanation for this undefined behavior of the program?
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 24 June 2011 - 10:52 AM

I'm not sure what kind of explanation you're looking for exactly.

a is declared as const. That means you're not allowed to change it. You change it anyway. This means you're doing something illegal, which in C means that the behavior of your program is not defined by the C standard and your implementation may choose to do whatever it wants to. Which in this case means acting as if you didn't change a when you access it directly.

This post has been edited by sepp2k: 24 June 2011 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#7 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 24 June 2011 - 11:03 AM

What exactly does your program print out?
If you run the program are a, add1, *p, p value?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 24 June 2011 - 12:01 PM

The simple answer is that &a is lying to you, for your own protection. I got a version of this to run with a modern compiler.

#include <iostream>

using namespace std;

int main() {
	const int a=5;
	int *p;
	// error: ISO C++ forbids declaration of ‘type name’ with no type [-fpermissive]
	// error: lvalue required as left operand of assignment
	// (const*)p=&a; 
	p=(int *)&a; 
	(*p)++;
	
	cout << "&a=" << &a << '\t' << "a=" << a  << endl;
	cout << "p =" << p << '\t' << "*p=" << *p << endl;
	
	int *p2 = (int *)&a;
	cout << "p2=" << p2 << '\t' << "*p2=" << *p2 << endl;
		
	return 0;
}



Result:
&a=0x7fff7f5d89bc	a=5
p =0x7fff7f5d89bc	*p=6
p2=0x7fff7f5d89bc	*p2=6



Note that it's just dumb luck the address I got back was valid. I probably could have crashed something doing this.
Was This Post Helpful? 0
  • +
  • -

#9 chauhan.vikram00  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 6
  • Joined: 24-June 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 25 June 2011 - 02:37 AM

Sir
Thank you for your kind attention to our program but here you going to declare illegal to given program cause of we tried to modifying the constant value.
But for your kind information we do not try to modify the constant 'a' we use to change the value at the address of it. which is not illegal according to c standard. here is question that where is the storage location for the value 101(printed as the *p) in the following program if it is not at the address stored in 'p' which is of the 'a'.
Again very very thank you for it .
Don't let it the testing of you ,its only to enhancing knowledge.
Was This Post Helpful? 0
  • +
  • -

#10 chauhan.vikram00  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 6
  • Joined: 24-June 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 25 June 2011 - 03:21 AM

Sir
No doubt your programing still print the value 5 for 'a' and 6 for any pointer variable assigned with the adress of that variable.
Problem is what is the reason that both distinct values '5' and '6' share the same address at any singe instant while the standard of c++ does not allowed to it .
Thank you for your kind attention.
Was This Post Helpful? 0
  • +
  • -

#11 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 25 June 2011 - 04:50 AM

They don't. Your compiler is LYING to stop you breaking the standard of C++.
Was This Post Helpful? 0
  • +
  • -

#12 Niha  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 20-April 07

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 25 June 2011 - 05:14 AM

I have a certain doubt regarding the code in the first post. are we allowed to declare constant pointers and if we are then shouldn't there be a problem incrementing the pointer in the next step? I haven't tried executing the code or anything (don't want to risk it); but I couldn't help noticing this.
Was This Post Helpful? 0
  • +
  • -

#13 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 25 June 2011 - 05:27 AM

Yes you can have constant pointers.

Read comments
// Constant pointer to nonconstant Data
// Pointer always points to same memory location
// value at memory address can be modified through pointer

int x = 5;
int y= 7;

int *const ptr = &x;

//Allowed
*ptr = 12;

//Error Not Allowed
ptr = &y;




// Constant pointer to constant Data
// Pointer always points to same memory location
// value at memory address can NOT be modified through pointer
int x = 5;
int y= 7;

const int *const ptr = &x;

//Error Not Allowed
*ptr = 12;

//Error Not Allowed
ptr = &y;



Was This Post Helpful? 3
  • +
  • -

#14 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 25 June 2011 - 07:25 AM

View Postchauhan.vikram00, on 25 June 2011 - 11:37 AM, said:

But for your kind information we do not try to modify the constant 'a' we use to change the value at the address of it.


Which is the same thing.


Quote

which is not illegal according to c standard.


Yes, it is. If you declare a variable as constant then the memory occupied by that variable is read-only as far as the standard is concerned (this does not mean that writing to that memory will necessarily cause a segmentation fault, but it certainly might). If you write to that memory, be it directly using the variable or using a pointer, that's undefined behavior.

Quote

here is question that where is the storage location for the value 101(printed as the *p) in the following program if it is not at the address stored in 'p' which is of the 'a'.


The value 101 is stored at the address in p, which is the address of a. So the value of a is 101 because you illegally changed it from 100 through the pointer. The reason that it still prints 100 even though the value is now 101 is as I already explained: The value of a is not actually read when print a, it just prints 100 without reading anything at all from memory, because the compiler (legally) assumes that the value of a can only ever be 100 and thus it is not necessary to read it from memory.

This post has been edited by sepp2k: 25 June 2011 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Is it possible that a single adress is shared by two distinct values ?

Posted 25 June 2011 - 12:07 PM

*
POPULAR

You can screw with the address as much as you like, in C++ it isn't going to affect that const value.

I got curious how my compiler was doing this trick. I took this program:
#include <stdio.h>
#include <stdlib.h>

void showData(const char *addrName, const void *addr, const char *valName, int val) {
	printf("%5s = %X  %5s = %d\n", addrName, (unsigned int)addr, valName, val);
}

int main() {
	const int a=5;
	int *p = (int *)&a; 
	
	(*p)++;
	showData("&a", &a, "a", a);
	showData("p", p, "*p", *p);
	
	printf("\n");
	
	(*p)++;
	showData("&a", &a, "a", a);
	showData("p", p, "*p", *p);
		
	return 0;
}



Which produced this output with a C++ compiler:
   &a = BFFB0C8C      a = 5
    p = BFFB0C8C     *p = 6

   &a = BFFB0C8C      a = 5
    p = BFFB0C8C     *p = 7



And, more interestingly, produced this with C:
   &a = BFFB0C8C      a = 6
    p = BFFB0C8C     *p = 6

   &a = BFFB0C8C      a = 7
    p = BFFB0C8C     *p = 7



Time to dig deeper. Here's an assembly dump of main:
# const int a=5;
movl   $0x5,0x1c(%esp) # first surpise, constant is on the regular stack

# int *p = (int *)&a; 
lea    0x1c(%esp),%eax # put the address in eax
mov    %eax,0x18(%esp) # store address in p
# so, second surprise, p has the real address
	
# (*p)++;
mov    0x18(%esp),%eax # load the address of p into eax
mov    (%eax),%eax     # load the value of *p into eax
lea    0x1(%eax),%edx  # ++ the value into edx ( don't ask me to explain this odd optimization, but it's normal )
mov    0x18(%esp),%eax # load the address of p into eax
mov    %edx,(%eax)     # load edx into the address where the constant was stored!

# right, that's unexpected
# it really did overwrite the value where our const was stored
# time for some C++ magic

# showData("&a", &a, "a", a);
movl   $0x5,0xc(%esp)         # wait, that's the original const value!
movl   $0x80486f4,0x8(%esp)   # string literal "a"
lea    0x1c(%esp),%eax        # &a int eax
mov    %eax,0x4(%esp)         # then into parameter stack
movl   $0x80486f6,(%esp)      # string literal "&a"
call   8048504                # call the function


# showData("p", p, "*p", *p);
mov    0x18(%esp),%eax        # load the address of p into eax
mov    (%eax),%eax            # load the value of *p into eax
mov    %eax,0xc(%esp)         # value onto parameter stack
movl   $0x80486f9,0x8(%esp)   # string literal
mov    0x18(%esp),%eax        # load the address of p into eax
mov    %eax,0x4(%esp)         # value onto parameter stack
movl   $0x80486fc,(%esp)      # string literal



So, C++ holds onto that const value and does a nice global replace at compile time. I've seen compilers put such values in read only memory, but this does seem more elegant. Now, the C dump looks identical, except that the magic movl $0x5,0xc(%esp) is missing and it just grabs the value from the address.

As expected, C will let you shoot yourself in the foot where C++ will protect you from yourself. That should, hopefully, clear up the mystery.

This post has been edited by baavgai: 25 June 2011 - 12:08 PM

Was This Post Helpful? 7
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2