the null reference

This is a discussion on the null reference within the C++ Programming forums, part of the General Programming Boards category; Embedded systems usually have their own rules....

  1. #46
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Embedded systems usually have their own 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

  2. #47
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    Quote Originally Posted by CornedBee View Post
    Embedded systems usually have their own rules.
    Having their own rules would imply that the language isn't C++, no matter how much it might otherwise look like it. So the point stands, C++ is useless on embedded, although languages which look extremely like C++ might not be.

  3. #48
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,188
    No. Dereferencing the null pointer to get a reference is explicitly mentioned to be undefined. It doesn't matter that no hardware dereferencing actually takes place. A compiler is allowed to reject the code if it can prove you're dereferencing 0.
    Who said anything about dereferencing 'null' to obtain anything? (Yes, that's rhetorical; I'm just saying... I didn't.) From my perspective you can't obtain anything by dereferencing 'null'--hence the beauty of 'null'.

    I have no doubt that 'type & r(*(0));' is undefined, regardless of the decoration, but that isn't what I suggested. There are handfuls of "Horrible Hacks (R)", every compiler I've ever used has a few relevant to this case, that allow you to bypass assignment of a reference. The right code, or rather the very horribly wrong code, will work in virtually every compiler; the code I suggest never manipulates the reference directly so the compiler can't complain about the assignment to 'null'--note: assignment to 'null' not assignment to '*(0)'.

    Don't break the language rules!
    Agreed. I'm just saying, if you are going to break them, break the @$%! out of them.

    Soma

  4. #49
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    That's kind of dumb. How am I supposed to access memory location 0 on an embedded system with such a rule in place? If true, it renders C++ useless for embedded development.
    You can access 0x0 on embedded systems??
    I've never touched an embedded system; why are they different from regular PCs in that area?

  5. #50
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    >> No. Dereferencing the null pointer to get a reference is explicitly mentioned to be undefined. It doesn't matter that no hardware dereferencing actually takes place. A compiler is allowed to reject the code if it can prove you're dereferencing 0.

    interesting. I always assumed that the compiler was guaranteed to generate a hardware access when dereferencing a pointer to get a reference. I wonder why they felt it needed to be undefined (perhaps they were anticipating precisely this type of hack )?

    >> the code I suggest never manipulates the reference directly so the compiler can't complain about the assignment to 'null'--note: assignment to 'null' not assignment to '*(0)'.

    ah, I misunderstood what you were saying in your earlier post. sorry about that.

    >> Don't break the language rules!
    >> Agreed. I'm just saying, if you are going to break them, break the @$%! out of them.

    these two statements may be the moral of the story here. I shouldn't have presented this as any sort of viable programming paradigm for standard C++ program. hobby code, maybe, but nothing more.



    ITSA
    Socket Library!

  6. #51
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    interesting. I always assumed that the compiler was guaranteed to generate a hardware access when dereferencing a pointer to get a reference.
    Since references are practically always implemented as pointers in hardware, the expression
    Code:
    T &r = *psomething;
    becomes a simple mov in machine code, if the compiler doesn't optimize the existence of the reference away completely.
    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 4 of 4 FirstFirst 1234
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