-
May 14th, 2011, 10:50 AM
#16
Re: Dereferencing NULL pointer
Practically, this C++ code may be translated to global Assembly function with first "this" parameter, or inlined by assigning s = 1443 (yes, I did this with calculator!). In both cases, it is OK. So, where is that door?
BTW, once in the interview I was asked, how to copy a file from computer A to B without wasting any A resources (CPU etc.). The genius who asked this wanted to hear the answer: we should run CopyFile from computer B. Few months after this company was closed (yes, my answer was "It is impossible", and yes, I used both metaphorical and physical doors)
Last edited by Alex F; May 15th, 2011 at 12:20 AM.
-
May 14th, 2011, 11:12 AM
#17
Re: Dereferencing NULL pointer
-
May 14th, 2011, 11:57 AM
#18
Re: Dereferencing NULL pointer
Originally Posted by Alex F
Practically, this C++ code may be translated to global Assembly function with first "this" parameter, or inlined by assigning s = 1443 (yes, I did this with calculator!). In both cases, it is OK. So, where is that door?
It is OK if you consider it on it's own, but note that when you "translate", you change the semantics. You loose all the information modeled by the concept of a class - so in that respect, it is not the same. My point is, my answer (not OK) doesn't follow from the properties of the language per se, but from a more abstract level.
Originally Posted by Alex F
BTW, once in the interview I was asked, how to copy a file from computer A to B without wasting any A resources (CPU etc.). The genius who asked this wanted to hear the answer: we should run CopyFile from computer B. Few months after this the company was closed (yes, my answer was "It is impossible", and yes, I used both metaphorical and physical doors)
Originally Posted by ovidiucucu
One good answer would be: "With the help of the Holy Spirit, which can save your company as well".
-
May 14th, 2011, 01:59 PM
#19
Re: Deference NULL pointer
Originally Posted by superbonzo
given that A is a POD and that A's ctor...
I thought POD's weren't allowed constructors?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
May 14th, 2011, 06:21 PM
#20
Re: Dereferencing NULL pointer
Originally Posted by ovidiucucu
Is any problem with this answer?
Is it in accordance with the C++ standard?
I can hardly believe it.
Last edited by nuzzle; May 14th, 2011 at 06:26 PM.
-
May 15th, 2011, 01:15 AM
#21
Re: Dereferencing NULL pointer
If you present rubbish code to your prospective new hires, be prepared for your new hires to write rubbish code for you.
-
May 15th, 2011, 01:31 AM
#22
Re: Dereferencing NULL pointer
Is Mr Brown or Mrs Smith who played the tricks on kids ? It's hard to define. I like laserlight's fabulous comment, it's true. I did so once. Hiring a detective costs money, and what else can he Do if he is now that Old ?
OOps I mean C code, POD, and pointers in C world
-
May 15th, 2011, 01:45 AM
#23
Re: Dereferencing NULL pointer
Well, I hope for he sake of C++ that the result is undefined and just happens to work on some compilers.
I'm too lazy to look it up though and I'm not looking for a job so I can just hope somone else checks this out with the standard.
-
May 15th, 2011, 02:13 AM
#24
Re: Dereferencing NULL pointer
Originally Posted by nuzzle
I'm too lazy to look it up though and I'm not looking for a job so I can just hope somone else checks this out with the standard.
We are all too lazy to check this out with the standard, posting rubbish in this interesting discussion. So, my five cents:
C++ is C with classes.
C is faster than C++.
Assembly is faster than C.
-
May 15th, 2011, 02:21 AM
#25
Re: Dereferencing NULL pointer
Originally Posted by nuzzle
Well, I hope for he sake of C++ that the result is undefined and just happens to work on some compilers.
I'm too lazy to look it up though and I'm not looking for a job so I can just hope somone else checks this out with the standard.
Originally Posted by Alex F
We are all too lazy to check this out with the standard, posting rubbish in this interesting discussion.
Except that superbonzo already quoted the C++ standard on this in posts #12 and #15.
-
May 15th, 2011, 05:07 AM
#26
Re: Dereferencing NULL pointer
Originally Posted by superbonzo
I think it refers to the latter instead . Anyway, here is another quote:
Originally Posted by 2003 standard, 1.9.4
Certain other operations are described in this International Standard as undefined (for example, the effect of dereferencing the null pointer). [...]
BTW, note that the expression "a->b" is by definition equivalent to "(*(a)).b", so I don't think there are doubts corcening the illegality of null pointer dereferencing.
In the new C++ standard (aka C++0x), 1.9.4 was modified as follows:
1.9 Program execution
[ ... ]
4. Certain other operations are described in this International Standard as undefined (for example, the effect
of attempting to modify a const object). [ ... ]
[ the underline is mine ]
I think, they had a good reason to replace "dereferencing the null pointer" in the example, isn't it?
Last edited by ovidiucucu; May 15th, 2011 at 05:11 AM.
-
May 15th, 2011, 05:29 AM
#27
Re: Dereferencing NULL pointer
Originally Posted by superbonzo
BTW, note that the expression "a->b" is by definition equivalent to "(*(a)).b".
And BTW, the expression "a->b" [ equivalent to "(*(a)).b" ] which gives an object, is pretty different to
a->b() [ equivalent to "(*(a)).b()" ] which calls a function.
-
May 15th, 2011, 06:45 AM
#28
Re: Dereferencing NULL pointer
Originally Posted by ovidiucucu
In the new C++ standard (aka C++0x), 1.9.4 was modified as follows:
(...)
I think, they had a good reason to replace "dereferencing the null pointer" in the example, isn't it?
If you think that they had a good reason, state what it is instead of asking a rhetorical question.
I don't know if they had a good reason to replace that text. I do know that dereferencing a null pointer results in undefined behaviour:
Originally Posted by C++11 Final Draft Clause 8.3.2 Paragraph 5 Note (part)
in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the "object" obtained by dereferencing a null pointer, which causes undefined behavior.
Notice that this text was retained from C++03. The note concerns null references, but it states that dereferencing a null pointer causes undefined behaviour.
Originally Posted by ovidiucucu
And BTW, the expression "a->b" [ equivalent to "(*(a)).b" ] which gives an object, is pretty different to
a->b() [ equivalent to "(*(a)).b()" ] which calls a function.
It is the same when you regard b as a metasyntactic variable, i.e., in the case of x->y(), b is y(). The text of both C++03 and the final draft of C++11 use E1 and E2 instead of a and b, respectively.
-
May 15th, 2011, 06:52 AM
#29
Re: Dereferencing NULL pointer
Originally Posted by ovidiucucu
Well that's not a quiz, and let me detail to avoid cheasing the own tail...
Someone has told: "to call a non-static member function it's obviously necessary to have an object".
I said, "generally that's not true" and gave the above example(s).
His last argument was: "No, it's not OK what you are doing because dereferencing a NULL pointer leads in undefined behavior".
Is it true or not?
Are you guys having a bet? You seem determined to beat him.
This is Spartaaaaaaa!
-
May 15th, 2011, 09:14 AM
#30
Re: Dereferencing NULL pointer
Originally Posted by Alex F
C++ is C with classes.
C is faster than C++.
Assembly is faster than C.
1) People who view C++ this way usually do not write good C++ code. If you want to write C, write C. C++ code should have a different mindset behind it even if much of the syntax is the same, emphasizing type safety and genericity much more than typical C code.
2) Not always. See std::sort() vs qsort() for a good example. It is true in most cases that C will compile faster than C++, however.
3) Well-written assembly will of course be at least as fast as C. Whether a given programmer is capable of out-coding a good optimizing compiler is much more in question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|