What do you guys think about this?

This is a discussion on What do you guys think about this? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Well, it is true that there is no need to check for NULL before doing a delete.... :P Still not ...

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Well, it is true that there is no need to check for NULL before doing a delete.... :P
    Still not implemented in MSVC .NET 2005. Calling delete on a NULL pointer will cause an exception. I know it's not supposed to be that way, but then when does MS listen to anyone but themselves?

  2. #17
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I agree on the true/false vs. 1/0 issue.
    Quote Originally Posted by Bubba
    Still not implemented in MSVC .NET 2005. Calling delete on a NULL pointer will cause an exception. I know it's not supposed to be that way, but then when does MS listen to anyone but themselves?
    What? This compiles and runs fine in 2005:
    Code:
    int main()
    {
        delete (char*)0;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #18
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    >>i += i++ + ++i; wheeeeeeeeeeee!

    Haha, Perspective, I like your point of view

  4. #19
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    As I am now the person evaluating programmers this company will hire, I can tell you that I would consider this a black mark against you (if I was interviewing you).

    Its not your teachers job to make the language/code simpler. Its your teachers job to ensure you can handle any programming task put before you. Even if that means knowing where/when to ask for help.

    >>Should have perhaps mentioned that the OU (Open University) course I'm currently doing insists on i=i+1 instead of i++...

    Great, so when you get out into the real world, find that all C++ code contains these operators, you will be have difficulty reading it (as opposed to someone who was taught to use them).

    Its not like all the code that contains these operators (or the programmers who wrote it) are going away soon.

    If something as simple (IMHO) as as these operators confuses you then you are going to have issues reading 'real world' code.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #20
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    605
    Erm, I wasn't stating my original post as my opinion, or implying that I was confused by it. What precisely is it that you would see as a black mark against me? There's nothing actually wrong with i=i+1, just as there's nothing (as far as I can see) wrong with i++.

    Please explain
    Visit entropysink.com - It's what your PC is made for!

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    What precisely is it that you would see as a black mark against me?
    I think novacain's reasoning is that since your teachers teach in this way, the students are likely to be unable to deal with real world code. You thus will be lumped in that category (of probably incompetent programmers), even if you are actually different.
    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

  7. #22
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    605
    Some justification, again quoted. (MT262 is the course I'm doing currently). To be fair, the course isn't a C++ course as such, it's more a general programming principles course that happens to use C++.

    C++ has #defines which can look like functions:

    Code:
    #define squareit(x) (x*x)
    Then you can use it to square numbers:

    Code:
    int MyInt;  
    int NumberThree;  	
    NumberThree = 3;  	
    MyInt = squareit(NumberThree); // Now MyInt has the value 9.
    BUT some programmers combine ++ and 'function' calls.

    These clever people would write

    Code:
    MyInt = squareit(NumberThree++);
    and would expect MyInt to be 9 and NumberThree to be 4. Instead, MyInt would be 9 but NumberThree would be 5, try it if you don't believe me :-)

    Moral: don't use ++. Ok it is safe in some places, but explaining exactly where is beyond MT262.

    BTW You don't know what (in a library) is a genuine function and what is a macro, and they can and do change over time. Code gets broken that way.
    Visit entropysink.com - It's what your PC is made for!

  8. #23
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Actually, if you can, use i++ instead of i = i + 1. I read somewhere that using i++ actually results in more optimized code; the assembly code simply uses incr (or whatever it's called) instead of all that other stuff.

  9. #24
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    605
    >> I read somewhere that using i++ actually results in more optimized code

    I could be wrong, but I doubt that.
    Visit entropysink.com - It's what your PC is made for!

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Moral: don't use ++. Ok it is safe in some places, but explaining exactly where is beyond MT262.
    Actually, I think that the moral in this case is to avoid macros as pseudo-functions. If it was an actual (inline) function, the use of ++ would not be a problem here.

    For example, the code would also fail in:
    Code:
    MyInt = squareit(NumberThree + 1);
    Since that would be expanded to:
    Code:
    MyInt = NumberThree + 1 * NumberThree + 1;
    which leads to:
    Code:
    MyInt = NumberThree + NumberThree + 1;
    The more correct macro definition would be:
    Code:
    #define squareit(x) ((x)*(x))
    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

  11. #26
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I read somewhere that using i++ actually results in more optimized code
    In C++, this can easily be the case with classes that overload the operator. For primitive types, you can expect identical machine code from i=i+1, i+=1, ++i, and i++ when used as standalone statements. Anything less would get a compiler laughed out of business, since this is a supremely trivial optimization.
    My best code is written with the delete key.

  12. #27
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Moral: don't use ++. Ok it is safe in some places, but explaining exactly where is beyond MT262.
    Some canned example:
    int x = getc(f++);
    [Warning 666] Expression with side effects passed to repeated parameter 1 in macro 'getc'
    Moral: choose good tools.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #28
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Moral: don't use ++. Ok it is safe in some places, but explaining exactly where is beyond MT262.
    It's beyond MT262 because they cannot justify this.

    It is sadly ironic that a programming course uses what is widely considered bad practice (macros) to justify the removal of a style that is widely considered a standard and good practice (increment and decrement operators).

    It's sadly ironic and ridiculous.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    It is sadly ironic that a programming course uses what is widely considered bad practice (macros) to justify the removal of a style that is widely considered a standard and good practice (increment and decrement operators).
    haha, I checked out Stroustrup's technical FAQ on macros, and what RobR's tutor mentioned was indeed an example, except that it was about... what's wrong with using macros.

    One of the most common subtle problems is that a function-style macro doesn't obey the rules of function argument passing. For example:

    Code:
    #define square(x) (x*x)
    
    void f(double d, int i)
    {
    	square(d);	// fine
    	square(i++);	// ouch: means (i++*i++)
    	square(d+1);	// ouch: means (d+1*d+1); that is, (d+d+1)
    	// ...
    }
    The "d+1" problem is solved by adding parentheses in the "call" or in the macro definition:

    Code:
    #define square(x) ((x)*(x))	/* better */
    However, the problem with the (presumably unintended) double evaluation of i++ remains.
    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

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

Similar Threads

  1. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 04:47 PM
  2. How long have you guys been working with C/C++??
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 08-01-2003, 03:41 PM
  3. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 10:22 AM
  4. hello guys
    By lupi in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:42 AM
  5. hello guys
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 01:00 AM

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