Thread: non-static member functions, called without an object problem...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    50

    Exclamation non-static member functions, called without an object problem...

    Code:
    #include <iostream>
    
    using namespace std;
    
    static int i;
    class x {
     int i;
     public:
      x() { cout << "In HERE" << endl; }
      int geti() { return i; }
    };
    
    int main()
    {
     cout << "Inside main() = " << ((x *)(&i+1))->geti() << endl;
     *(&i+1) = 17;
     cout << "Inside main() = " << ((x *)(&i+1))->geti() << endl;
     static x aa;
     cout << "Inside f() : " << aa.geti() << endl;
     x bb;
    }
    I came across this code on the internet. I tried to compile it using MingGW and it compiles and works!!! I'm confused. Why? Would somebody explain this??

    TIA.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that's evil. Don't do that.

    your answer: it uses dirty pointer arithmetic and casting to acheive it's purposes.

    I think this is how it works, but if I'm wrong, somebody please point it out to me:
    Code:
    #include <iostream>
    
    using namespace std;
    
    static int i;
    class x
    {
    	private:
    		int i;
    	public:
    		x() { cout << "In HERE" << endl; }
    		int geti() { return i; }
    };
    
    int main()
    {
    	//this line takes the address of i (the static one) and adds one to it,
    	//then casts that address to a pointer of type x and uses it's geti
    	//method.  This pretty much creates a pointer to type x in the memory
    	//location adjacent to i
    	cout << "Inside main() = " << ((x*)(&i+1))->geti() << endl;
    	//this assignes the number 17 to what the pointer next to i is pointing
    	//to
    	*(&i+1) = 17;
    	//this is the same as the first line
    	cout << "Inside main() = " << ((x*)(&i+1))->geti() << endl;
    	//this actually creates an x and calls it aa
    	static x aa;
    	//this uses the geti method in that x
    	cout << "Inside f() : " << aa.geti() << endl;
    	//this creates another x, but doesn't use it...
    	x bb;
    }
    I changed how it was organized a tad bit, but logically it's the same code. Basically, it just assumes the code is written a certain way, and if you move things around (add another int to the class, throw in another global int under the static one), it will change the outcome of the program.
    Last edited by major_small; 03-08-2006 at 11:21 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    thanks!

    but i've tried your suggestions( moving things around, adding classes, adding static variables after or before...) it still have the same output.

    I'm still confused...

    if i am understanding the code correctly...
    Code:
     *(&i+1) = 17;
    based from the main program, this allocates memory as if you're instantiating a variable pointed by i + 1 to 17?

    TIA.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by what3v3r
    thanks!

    but i've tried your suggestions( moving things around, adding classes, adding static variables after or before...) it still have the same output.

    I'm still confused...

    if i am understanding the code correctly...
    Code:
     *(&i+1) = 17;
    based from the main program, this allocates memory as if you're instantiating a variable pointed by i + 1 to 17?

    TIA.
    I don't think that's it. I think the first cast must set the layout for the pointer; and it doesn't seem like memory ever gets allocated--it just gets hijacked.
    Last edited by 7stud; 03-09-2006 at 12:26 AM.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the first cast creates a pointer, and I think the compiler allocates the memory somewhere else. That line just uses that pointer and puts a 17 wherever the memory was allocated.

    as for my example, try putting int j; above int i; and see what happens. it's possible it won't change anything, but it did for me =P
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    ahhhhh! Thank you i got it now!

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ahhhhh! Thank you i got it now!
    Well, that makes one of us.

    Can the memory be allocated by the system to something else? Or, does the program own it?

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yes, the memory doesn't belong to the program, so this could (theoretically) do as much as crash your system because it uses memory it never claimed as it's own. For all you know the OS could be using that memory location to keep track of some critical process, and you just hijacked and changed it.

    On a side note, that's highly unlikely, because most OS'es keep programs in a seperate memory address range than the OS uses for it's own purposes, so most likely if it affects anything it'll affect any programs running at the same time that happen to allocate memory while this one is running.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCP Header problem (error)
    By nasim751 in forum C Programming
    Replies: 1
    Last Post: 04-25-2008, 07:30 AM
  2. How to pass member functions into a function object...
    By TeenWolf in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 01:01 PM
  3. Static & Data Member
    By ecoliteracy in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2007, 08:46 PM
  4. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  5. Declaration/Definition ??
    By Manish in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:37 AM