LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2004, 11:43 AM   #1
zyzyis
LQ Newbie
 
Registered: Nov 2003
Location: ChengDu(China)
Distribution: Debian
Posts: 23

Rep: Reputation: 15
An interesting problem about C++


Code:
#include <iostream>
using namespace std;

int main( int argc, char* argv[] )
{
    const int a=5;
    const int* aptr = &a;
    int* ptr = ( int* )(aptr);
    cout << ptr << " " << &a << " " << aptr << endl;
    *ptr = 3;
    cout<< *ptr << " " << a << " " << *aptr <<endl;
    return 0;
}
compile it with g++ and the result is
0xbffffb34 0xbffffb34 0xbffffb34
3 5 3

I know it's fool to write code like that in a really software, but

I'm really interested at how the g++ work with the const pointer typecast

anyone could tell me ?

thanks very much~~~
 
Old 10-10-2004, 12:11 PM   #2
MadCactus
Member
 
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Rep: Reputation: 30
Why are you converting a_ptr when its already a pointer?
 
Old 10-10-2004, 12:43 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Haven't looked in the g++ source for this, but I think that 'const' just adds work to the compiler. It produces an error when the value is changed. But with special casting (pointer to pointer is enough) you can modify consts, so it's not very sophisticated.
 
Old 10-10-2004, 09:00 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
I compiled the following with gcc
Code:
#include <stdio.h>

int main()
{
        const int a = 5;
        const int *aptr = &a;
        int *ptr = (int*)aptr;
        printf("%p %p %p\n", ptr, &a, aptr);
        *ptr = 3;
        printf("%d %d %d\n", *ptr, a, *aptr);
        return 0;
}
and found that, without any flags, it prints 3 3 3, but with -O1 through -O3 it prints 3 5 3.

This simple answer to the question is of course that writing to a const variable gives undefined results You could get a better answer by using the -S switch to generate assembly output.. but I'm not good at reading that.
 
Old 10-11-2004, 02:38 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I checked with -O3 and it optimizes the second printf in a funny way. No pointers, but the values are passed to printf. The compiler assumes that aptr content doesn't change (it shouldn, right? it's a const int*) and uses its orginal value instead of getting what's currently kept in a.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Interesting Wifi problem dguitar Linux - Wireless Networking 3 10-06-2005 04:06 AM
Interesting Video Problem with ATI Radeon 9800 Pro (not a 3D accel based problem) binarynova Linux - Hardware 1 08-27-2005 09:49 PM
interesting problem..any idea?? mchitrakar Linux - Networking 3 07-06-2005 08:41 AM
Interesting Kernel Problem Shade Linux - General 2 09-27-2003 08:52 AM
an interesting problem dlpmdr Linux - Distributions 2 03-30-2003 12:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration