unique pointer issue

This is a discussion on unique pointer issue within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Could anyone help to explain what does the two rules for unique pointer mean? http://msdn2.microsoft.com/en-us/lib...94(VS.85).aspx 1. Especially confused ...

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

    unique pointer issue

    Hello everyone,


    Could anyone help to explain what does the two rules for unique pointer mean?


    http://msdn2.microsoft.com/en-us/lib...94(VS.85).aspx

    1.

    Especially confused about "before the call"

    Can use existing memory on the client without allocating new memory. When a unique pointer changes during a call from one non-NULL value to another, the pointer is assumed to point to a data object of the same type. Data returned from the server is written into existing storage specified by the value of the unique pointer before the call.

    2.

    Can orphan memory on the client. Memory referenced by a non-NULL unique pointer may never be freed if the unique pointer changes to NULL during a call and the client does not have another means of dereferencing the storage.


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The key point is this:
    Does not cause aliasing. Like storage pointed to by a reference pointer, storage pointed to by a unique pointer cannot be reached from any other name in the function.
    This is a help to the compiler and the coder that there is no risk of dealing with this pointer in the sense that it should never point to the same data as anything else in that function.

    Your two points says that these two operations are not forbidden by the unique attribute:
    Point 1:
    Code:
    char [unique] *ptr;
    char array[] = "Hello";
    
    ptr = array;
    ...
    ptr = NULL;
    ... 
    ptr = new char [30]; 
    ...
    Point 2:
    Code:
    ptr = new char [30]; 
    ... // no "delete ptr" here
    ptr = NULL;
    Point 2 obviously means a memory leak (orphaned memory), but it's still valid use of a unique pointer.

    It is NOT valid to do this:
    Code:
    char [unique] *ptr1;
    char [unique] *ptr2;
    
    char array[] = "Hello";
    
    ptr1 = array;
    ptr2 = ptr1;
    ptr2 has the same value as ptr1 which means that the pointer is not unique.

    --
    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.

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


    Sorry for my limited knowledge. I tried to compile the code,

    Code:
    int main()
    {
    	char [unique] *ptr1;
    	char [unique] *ptr2;
    	char array[] = "Hello";
    
    	ptr1 = array;
    	ptr2 = ptr1;
    
    	return 0;
    }
    Seems compiler does not recognize the unique attribute? I am using Visual Studio 2008.

    But from MSDN, seems we do not need to include additional headers?

    http://msdn2.microsoft.com/en-us/library/aa367294.aspx

    Here is the error message,

    1>Compiling...
    1>main.cpp
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(3) : warning C4091: '' : ignored on left of 'char' when no variable is declared
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(3) : error C2143: syntax error : missing ';' before '['
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(3) : error C2143: syntax error : missing ';' before '*'
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(3) : error C3094: 'unique': anonymous usage not allowed
    1> d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\predefined c++ attributes (compiler internal)(1155) : see declaration of 'unique'
    1> attribute can only be applied to: 'member', 'interface member function', 'formal argument of interface member function', 'typedef'
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(3) : error C2065: 'ptr1' : undeclared identifier
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(4) : warning C4091: '' : ignored on left of 'char' when no variable is declared
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(4) : error C2143: syntax error : missing ';' before '['
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(4) : error C2143: syntax error : missing ';' before '*'
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(4) : error C3094: 'unique': anonymous usage not allowed
    1> d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\predefined c++ attributes (compiler internal)(1155) : see declaration of 'unique'
    1> attribute can only be applied to: 'member', 'interface member function', 'formal argument of interface member function', 'typedef'
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(4) : error C2065: 'ptr2' : undeclared identifier
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(7) : error C2065: 'ptr1' : undeclared identifier
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(8) : error C2065: 'ptr2' : undeclared identifier
    1>d:\visual studio 2008\projects\test_uniquepointer1\test_uniquepoint er1\main.cpp(8) : error C2065: 'ptr1' : undeclared identifier

    Quote Originally Posted by matsp View Post
    The key point is this:


    This is a help to the compiler and the coder that there is no risk of dealing with this pointer in the sense that it should never point to the same data as anything else in that function.

    Your two points says that these two operations are not forbidden by the unique attribute:
    Point 1:
    Code:
    char [unique] *ptr;
    char array[] = "Hello";
    
    ptr = array;
    ...
    ptr = NULL;
    ... 
    ptr = new char [30]; 
    ...
    Point 2:
    Code:
    ptr = new char [30]; 
    ... // no "delete ptr" here
    ptr = NULL;
    Point 2 obviously means a memory leak (orphaned memory), but it's still valid use of a unique pointer.

    It is NOT valid to do this:
    Code:
    char [unique] *ptr1;
    char [unique] *ptr2;
    
    char array[] = "Hello";
    
    ptr1 = array;
    ptr2 = ptr1;
    ptr2 has the same value as ptr1 which means that the pointer is not unique.

    --
    Mats

    regards,
    George

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Erm, yes, I think you need to use the MIDL compiler for those things, and you'll get C/C++ headers from it.

    http://msdn2.microsoft.com/en-us/lib...64(VS.85).aspx

    Note that I've never used MIDL.

    --
    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. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Mats,


    About the code you write before,

    Code:
    ptr = new char [30]; 
    ... // no "delete ptr" here
    ptr = NULL;
    This is what you mentioned,

    --------------------
    Point 2 obviously means a memory leak (orphaned memory), but it's still valid use of a unique pointer.
    --------------------

    In this situation, there is no chance to free the memory buffer pointed by ptr before, right? I am not sure why unique pointer allows this behavior, since I do not think it is good code -- at least it will cause memory leak.

    Do you think it is good code or why do you think unique pointer allows this feature?


    regards,
    George

    Quote Originally Posted by matsp View Post
    Erm, yes, I think you need to use the MIDL compiler for those things, and you'll get C/C++ headers from it.

    http://msdn2.microsoft.com/en-us/lib...64(VS.85).aspx

    Note that I've never used MIDL.

    --
    Mats

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I "think" it's allowed because MS says it's allowed - and NO, I don't think it's a good thing, but it's mentioned in the specification so that it's clear that the tag "unique" doesn't guarantee that the memory pointed to by the pointer will be freed - it's not a smart pointer.

    --
    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.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Great Mats!


    I think we have completed the discussion of the 2nd point of my question. How about your comments about the 1st point? :-)

    --------------------
    1.

    Especially confused about "before the call"

    Can use existing memory on the client without allocating new memory. When a unique pointer changes during a call from one non-NULL value to another, the pointer is assumed to point to a data object of the same type. Data returned from the server is written into existing storage specified by the value of the unique pointer before the call.
    --------------------

    I do not know why "Data returned from the server is written into existing storage specified by the value of the unique pointer before the call", especially "the value of the unique pointer before the call".

    Suppose the following scenario,

    1. client pass point ptr1 to server and ptr2 originally pointed to memory buffer1;
    2. server change ptr1 to pointed to memory buffer2, which is different from buffer1;
    3. server writes data through pointer ptr1, it should be written to buffer2.

    So, it should be,

    Data returned from the server is written into existing storage specified by the "lastest" value of the unique pointer, which is memory buffer 2.

    Other than,

    Data returned from the server is written into existing storage specified by the value of the unique pointer "before" the call, which is memory buffer 1.

    Any comments?

    Quote Originally Posted by matsp View Post
    I "think" it's allowed because MS says it's allowed - and NO, I don't think it's a good thing, but it's mentioned in the specification so that it's clear that the tag "unique" doesn't guarantee that the memory pointed to by the pointer will be freed - it's not a smart pointer.

    --
    Mats

    regards,
    George

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, I don't know.

    --
    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.

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


    I think you agree with me on post #7 and do not agree with the words on MSDN. :-)

    Quote Originally Posted by matsp View Post
    Sorry, I don't know.

    --
    Mats

    regards,
    George

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not agreeing with either - I'm saying that I don't know - which means that I can't say if one or the other is right or wrong or both are right/wrong but just different ways to express the same thing, for example.

    --
    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.

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Ophan memory is basically memory that you allocated, did not free, and discarded or overwrote the pointer to and so your program cannot free it or even know that it exists. This is the primary cause of memory leaks. It is a fairly common novice bug, but it can also happen in highly complex memory management models. A similar problem is a ghost pointer, where you free'd the memory, but forgot to NULL the pointer, adn so you have a pointer that was valid and now points to an invalid memory location. There are two things this can do. Bad - you use the pointer and it causes a protection fault, crashing your program. Worse, some other thread allocates a block of memory that makes the bad pointer valid again, and you get memory corruption but the program runs 'normally' and you have a bug that is extremely difficult to track down.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Great abachler!


    Any comments to my post #7? :-)

    Quote Originally Posted by abachler View Post
    Ophan memory is basically memory that you allocated, did not free, and discarded or overwrote the pointer to and so your program cannot free it or even know that it exists. This is the primary cause of memory leaks. It is a fairly common novice bug, but it can also happen in highly complex memory management models. A similar problem is a ghost pointer, where you free'd the memory, but forgot to NULL the pointer, adn so you have a pointer that was valid and now points to an invalid memory location. There are two things this can do. Bad - you use the pointer and it causes a protection fault, crashing your program. Worse, some other thread allocates a block of memory that makes the bad pointer valid again, and you get memory corruption but the program runs 'normally' and you have a bug that is extremely difficult to track down.

    regards,
    George

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, 03:29 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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