the null reference

This is a discussion on the null reference within the C++ Programming forums, part of the General Programming Boards category; I think Sebastiani wants the kind of pointer-like object references that exist in Java and co. Then he should program ...

  1. #16
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,169
    I think Sebastiani wants the kind of pointer-like object references that exist in Java and co.
    Then he should program in Java.

    undefined!!!
    that's the purpose and intent of the OP (at least it was my purpose)
    how do i check that "ref" is a valid or invalid reference?
    we have this convention of NULL pointer being invalid (not a bulletproof way though, but that's C++, and programmers know)
    how we check for above "ref" being valid or invalid?
    i has the same situation in my project, and it is giving me nightmares! >_<
    No. Just... no. God, no! No amount of jury rigging will protect you from idiocy. Sorry, but we are discussing C++ not some magic language that disallows all forms of programmer errors. The code, and offering, provided by Sebastiani will not work to check against a null reference because, strangely, as iMalc said, there is no such thing. The only thing that the Sebastiani offering will provide is a check against his offering which would have to be returned specifically--returning it by accident would be an even bigger issue.

    what if i got an object by reference, which was not a local object, which was heap allocated one, but some how, in difficult to follow code, the object is deleted, and all i have is the reference, how to check is it valid?
    This offering, indeed any offering that subverts reference logic, will not help you debug a program. It can't help you find and correct programmer errors. There are however generations of tools that will help you debug a program... use them. (In your case a heavily modified form of new/new[]/delete/delete[]/malloc/realloc/calloc/free/etc. and something very similar to 'assert' would be a huge help.)

    Soma

  2. #17
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    what if i got an object by reference, which was not a local object, which was heap allocated one, but some how, in difficult to follow code, the object is deleted, and all i have is the reference, how to check is it valid?
    Use the delete key.

  3. #18
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Perfect Solution!
    but if i only figure out where to use the "delete" key?
    my biggest head-ache in managing the project is invalid pointers, invalid references, and the code follows the path of a Cob-Web !
    does someone have any experience with such stuff? what would you suggest me to make it a little easy to follow?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    but if i only figure out where to use the "delete" key?
    That's why Prelude claims her best code is written with the delete key. (Or was it backspace?)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    undefined!!!
    that's the purpose and intent of the OP (at least it was my purpose)
    how do i check that "ref" is a valid or invalid reference?
    we have this convention of NULL pointer being invalid (not a bulletproof way though, but that's C++, and programmers know)
    how we check for above "ref" being valid or invalid?
    i has the same situation in my project, and it is giving me nightmares! >_<
    One way to easily detect that some object has been free/deleted is to create your own versions of new & delete, and let the delete version fill in the memory with some known pattern (e.g. 0xCFCFCFCFCF - that's almost guaranteed to crash you if you use it as a 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.

  6. #21
    Banned
    Join Date
    Nov 2007
    Posts
    678
    oh thanks Mats!
    i run my program under windbg, so the moment i reference an invalid object, the program breaks to the point where it happened.
    in my case it happens when a function on deleted object is called, and when that function tries to read a member variable.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    oh thanks Mats!
    i run my program under windbg, so the moment i reference an invalid object, the program breaks to the point where it happened.
    in my case it happens when a function on deleted object is called, and when that function tries to read a member variable.
    Yes, and then you have to trace back where that object was created.

    If you have your own new and delete operator functions, then you can also record the return address [may need a bit of trickery to do that, but it can be done], and find where the object was created/deleted - this would of course involve allocating a bit of extra memory and using that extra memory to store this sort of info.

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

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by manav View Post
    oh thanks Mats!
    i run my program under windbg, so the moment i reference an invalid object, the program breaks to the point where it happened.
    in my case it happens when a function on deleted object is called, and when that function tries to read a member variable.
    Argh. Use Visual Studio you... you... MONKEY!
    Windbg is not as useful as Visual Studio when it comes to debugging if you ask me.
    Visual Studio already fills memory with a known pattern when uninitialized and when deleted in debug mode, so it provides this features for you for free. Why are you trying to make things hard on yourself?
    Use Visual Studio.

    And do yourself another favour - use smart pointers to ensure objects don't delete prematurely and don't use references to heap objects, use smart pointers!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks Elysia!
    (i have hijacked this thread it seems, sorry)
    but i think to fully utilize the features of MSVC i have to make a fully working MSVC project. currently i have a dummy MSVC project (after you helped me a little). this dummy project allows me to use other features of MSVC like completion, opening the headers by right click, finding definitions etc., but i can't build (you remember, we discussed that before) it under MSVC. that's why it's a dummy project. my work involves Qt etc. and to really use it MSVC way, i need a commercial version, while i am using open source version, with a patch, that allows it to compile using MSVC cl compiler.
    end of story! i will kill my self! urrrgh!

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by manav View Post
    but i think to fully utilize the features of MSVC i have to make a fully working MSVC project. currently i have a dummy MSVC project (after you helped me a little). this dummy project allows me to use other features of MSVC like completion, opening the headers by right click, finding definitions etc., but i can't build (you remember, we discussed that before) it under MSVC. that's why it's a dummy project. my work involves Qt etc. and to really use it MSVC way, i need a commercial version, while i am using open source version, with a patch, that allows it to compile using MSVC cl compiler.
    Bzzt. Wrong.
    You only need Professional if you want the resource editor or MFC or team edition stuff. Otherwise Express works just fine.
    And I also told you before that your manual compiling with the stupid makefile is destroying the code is all sorts of horrible ways (like no C++ exceptions!). You should ask on the forum or google or ask on Qt's forum or some support alternative on how to compile Qt in a Visual Studio project.

    Alternatively, another bad way, is to create a makefile project first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    >> There never has been, nor will there ever be such a thing as a null reference in C++. There is no way of creating such a thing and no way for such a thing to ever exist.

    that is correct. but that doesn't mean it can't be simulated.

    >> Uh... no. At the very best, the very best, with your "addition" you save one character per reference event over a pointer, and you still subvert reference logic.

    noone uses a library of code without first reading the documentation. having done so, the only thing subverted would be small-mindedness.

    >> my biggest head-ache in managing the project is invalid pointers, invalid references, and the code follows the path of a Cob-Web !

    the easiest way to address that is to use 'smart' pointers.

    at any rate, I admit something like this is a bit unorthodox, maybe even a little taboo. like opera, either you love it or you hate it.
    Last edited by Sebastiani; 04-15-2008 at 07:45 AM.

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    The first example can also throw if it can't find a valid return!
    But I guess that you've thought of that and wanted a returning function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Sebastiani View Post
    >> Uh... no. At the very best, the very best, with your "addition" you save one character per reference event over a pointer, and you still subvert reference logic.

    noone uses a library of code without first reading the documentation. having done so, the only thing subverted would be small-mindedness.
    Ah yes, I remember the good old days when I was working for a company that actually took the time to document their code... It doesn't work so well with undocumented spagetti code.

  14. #29
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by manav View Post
    what if i got an object by reference, which was not a local object, which was heap allocated one, but some how, in difficult to follow code, the object is deleted, and all i have is the reference, how to check is it valid?
    You can't.

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I can, off-the-cuff, think of three problems with this idea.

    1) It's undefined behaviour. You dereference a type-punned pointer in the Type& conversion operator. It doesn't matter that the result is a reference, so the compiler doesn't actually dereference. The code is simply undefined.

    2) It is a detriment to debugging. If you dereference the null pointer, the program will crash and burn. This is good. If you dereference your null reference, you read or write to the global variable area. This is bad.

    3) It is a detriment to understanding. This is the worst problem of all. C++ references are never null. Every programmer knows this. Only, you break this rule and expect programmers to accommodate your oddness. They won't.
    You claim that no one uses a library without reading the documentation. But library documentation is there to document what the library provides. It's not there to tell the programmer in what ways the library breaks the language rules. Nobody expects that references can't be null, so they won't even notice if you put this in the documentation. That's because C++ references aren't null, so clearly they can't be null in your library, no matter what the documentation says.
    The only way to get programmers to notice this oddity is to put it in fat, 72-point font on the first page of the documentation: "References can be null."
    But even then, people will not know what you mean. They'll either think you're confusing references and pointers, or they'll go looking into the library reference to search for a class called "reference". They won't suspect that you mean C++ references, because C++ references can't be null.
    And when the programmers finally do understand what you mean, they'll drop your library like a poisonous snake and run away to where programmers play by the rules.

    Do not break the language rules.
    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

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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