my code breaks unique pointer rule?

This is a discussion on my code breaks unique pointer rule? within the C++ Programming forums, part of the General Programming Boards category; Sorry Mats, I do not fully agree. You mentioned, "so the function is guaranteed that the data from the "unque" ...

  1. #16
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry Mats,


    I do not fully agree.

    You mentioned,

    "so the function is guaranteed that the data from the "unque" pointer is not reachable from any other pointer"

    But the data pointed by ptr1 could be reached by ptr2 in main.

    "this is a gurantee that the paraemeter coming into the function is unique"

    Not unique, ptr1 and ptr2 point to the same memory.

    Any comments?

    Quote Originally Posted by matsp View Post
    Yes, so the function is guaranteed that the data from the "unque" pointer is not reachable from any other pointer - this is a gurantee that the paraemeter coming into the function is unique, not necessarily that it actually is unique inside the function.

    --
    Mats

    regards,
    George

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The compiler only checks what happens when you pass the pointers. It doesn't care what you do with them afterwards.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    I do not think compiler checks anything before passing the parameters. I have modified the sample and posted the code below.

    You can see ptr1 and ptr2 are aliasing in main. Compile and run ok, any comments?

    Code:
    __interface IFoo {
    
    public:
    
    	virtual int foo ([unique] char  *ptr1, [unique] char  *ptr2) = 0;
    };
    
    class Foo : public IFoo {
    
    public:
    
    	int foo (char  *ptr1, char  *ptr2)
    	{
    		char array1[] = "Hello";
    
    		ptr1 = array1;
    		ptr2 = array1; // should be wrong, no aliasing allowed, but compile and run ok
    		
    		return 0;
    	}
    };
    
    
    int main()
    {
    	char buf1[] = "Hello World! \n";
    	char* ptr1 = buf1;
    	char* ptr2 = ptr2;
    
    	Foo f;
    	// f.foo(ptr1, ptr2);
    	f.foo(ptr1, ptr1);
    
    	return 0;
    }
    Quote Originally Posted by CornedBee View Post
    The compiler only checks what happens when you pass the pointers. It doesn't care what you do with them afterwards.

    regards,
    George

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, so I don't think the compiler checks - you are just obliged to follow the rules.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    You mean it is just an indicator to developer of function and the user of the function to be aware more of the attribute of parameter/return value, but compiler/linker/runtime will never force anything more than the situation when you do not put any additional attibute, right?

    If yes, let me know if you have found any formal documents from MSDN or something else where which indicates the above points. I am also looking for the documents, but have not found any formal ones. :-)

    Quote Originally Posted by matsp View Post
    Yes, so I don't think the compiler checks - you are just obliged to follow the rules.

    --
    Mats

    have a good weekend,
    George

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. unique pointer issue
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 02-14-2008, 10:25 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. different pointer changes in same code
    By fizisyen in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21