CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 56

Thread: Pseudopointers

Threaded View

  1. #22
    Join Date
    Feb 2013
    Posts
    27

    Re: Pseudopointers

    I'll make a stew of all exposed codes (buggy codes) XD.
    Here's the Paul's code
    Code:
    #include <iostream>
    int main()
    {
       static int x;
       static int y;
       int *z = &x + 1;  // What is the magic number to add? 
       printf("%X\n",&x);
       printf("%X\n",&y);
       printf("%X\n",z);
       system("pause");
    
       if ( z==&y )
           std::cout << " OK\n";  // how will we get here?
       else
           std::cout << "Not OK\n";
       return 0;
    }
    For example, if you would like 2 integers to be stored
    contiguously, then you can do something like this:
    Code:
    #include <iostream>
    int main()
    {
    	int array[2]={0};
       int *z = &array[0] + 1;  // What is the magic number to add?  
       if ( z==&array[1] )
           std::cout << " OK\n";  // how will we get here?
       else
           std::cout << "Not OK\n";
       return 0;
    }
    But getting back to the first code, it wasn't working,
    so I went to make some changes and got it to work IN MY COMPILER.

    if you give those 2 variables called x and y, an initialization
    value then they got stored contiguously.
    If you do not, then they're not.
    Ok, let's say that now they are contiguously AT LEAST in my compiler,
    now if I do this comparision it doesn't come with the expected result.
    Code:
    if ( z==&y)
    As you see z is a local pointer variable, that means is in the stack,
    but it is just a pointer which holds a memory address.

    so why if I do:
    Code:
    if ( z==&y)
    it wouldn't work?. don't they mean just 2 memory addresses?

    if you like to see it in other look, then see this:

    I added another pointer that points to the second variable.
    Code:
    int* w = (&y);
    and now

    Code:
    if ( z==w)
    it doesn't work again..

    I fixed it in my compiler by making the z pointer variable to be 'static'

    Code:
    #include <iostream>
    int main()
    {
       static int x=0;
       static int y=0;
       static int *z = &x + 1;  // What is the magic number to add?
       printf("%X\n",&x);
       printf("%X\n",&y);
       printf("%X\n",z);
       system("pause");
    
       if ( z==&y )
           std::cout << " OK\n";  // how will we get here?
       else
           std::cout << "Not OK\n";
       return 0;
    }
    so this is working for me, but is not working in other compiler, for example
    http://www.compileonline.com/compile_cpp_online.php

    but if you want it to work in that online compiler you do:
    Code:
    #include <iostream>
    static int x=0;
    static int y=0;
    int main()
    {
       static int *z = &x + 1;  // What is the magic number to add?
       printf("%X\n",&x);
       printf("%X\n",&y);
       printf("%X\n",z);
       system("pause");
    
       if ( z==&y )
           std::cout << " OK\n";  // how will we get here?
       else
           std::cout << "Not OK\n";
       return 0;
    }
    the other fix that I managed to do is this (Only if they are contiguously stored):
    Code:
    #include <iostream>
    static int x=0;
    static int y=0;
    int main()
    {
       int *z = &x + 1;  // What is the magic number to add? 
       printf("%X\n",&x);
       printf("%X\n",&y);
       printf("%X\n",z);
       system("pause");
    
       if ( (z-&y) ==0 )
           std::cout << " OK\n";  // how will we get here?
       else
           std::cout << "Not OK\n";
       return 0;
    }
    As you see, I implemented a substraction of 2 memory addresses,
    I assume it won't be get out of the INT valid range, and assuming
    the INT type and the memory addresses are of 32 bits.
    If the 2 addresses are the same then it will give ZERO as result.
    That just works, and the pointer doesn't need to be 'static'.

    But all this is relying in the fact that if the 2 variables will be
    stored contiguously in memory. That is not guaranteed by the C++ specifications.
    and I understand that is not the case of making fixes for every compiler.

    You just said: "it's buggy code" ,
    if that is the case I will be not discussing anymore on this, instead I will change my first
    code to use an array or maybe a struct. Because every compiler seems to handle storage
    in its own way (at least for the contiguousness).

    EDIT:
    I added the new code in the first post, now there are 3 examples of implementation.
    (The first example is considered with an undefined behaviour).
    Last edited by eightyfive; March 23rd, 2013 at 05:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured