Printing a null pointer in c




















Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.

Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. I just return an integer and it's a valuable trick in some situations. I find that hard to believe. It suggests that you haven't put enough thought into finding a better solution.

Perhaps asm is too nasty :-D If you link asm to C code you are sacrificing portability, which is fine in some circumstances. However you are or should be still basing the code on specifications that define its behaviour.

By doing things like calling functions incorrectly you have NO specification of behaviour, you are trusting to blind luck and hope that things will continue to work as you have observed in the past. This is no way to program. Today's compiler optimisers are too complex to predict with any certainty. You may use a trick that worked many times, then one day with the same compiler you use it in a situation which the compiler decides it can optimise and suddenly the trick no longer works.

There was an example of this in another thread to do with overlaying structures. One of the reasons C leaves some areas of behaviour undefined is to allow more aggressive optimisations. Keith Thompson. If "very likely" isn't good enough, several followups have shown how to break the representation down into a sequence of bytes. This is something. Therefore, we must do this. Clark S. Cox III.

I agree, but it doesn't hurt knowing it's possible though not portable , and knowing how things work. Yes, sir! No irony, I completely agree it's not the style to use too often, but if I remember well, the original post asked for a manner to see what NULL is.

Just a toy program after all. Sometimes, it may be necessary: you mention a memory allocator, but there may be other uses. It's a good practice to try to avoid them, it's not good practice do deny their existence. This newsgroup deny all that is not perfectly described by the Standard. It's only this attitude I reject, not the Standard itself. You are right, I must confess :- Oh, and when you link asm code to C, what do you think you do?

I agree but do I need to say that? By doing things like calling functions incorrectly you have NO specification of behaviour, Well, the standard doesn't specify anything, but your knowledge of the system you're programming on including compiler , gives you all needed informations on the behaviour.

When in doubt, it's always? I never trust a compiler :- Even with beautiful C code, I'm not confident in its optimizations, especially concerning floating point. And this opinion is not going to change in the near future: "paranoia" is old, but still an interesting test just as an example. Here I disagree. There are many predictable parts in gcc output, with enough habit, you know when writing some code if it will be well optimized or not, and which kind of optimization occurs.

If you think your compiler is perfect, well I hope it is! Gcc, to stay with something I am acquainted with, won't use prefetch or Altivec instructions I heard gcc 4 will, but I'm still not convinced. Hence it's not so difficult to beat its optimizer. On the other hand, it's much more difficult to beat xlc won't use Altivec, but apart from that, it is very, very clever. I think a good practice is always having a look at assembly output, for function that need good optimizations, or that use non standard tricks.

Oh, and I said "acquainted with gcc", I wouldn't even try to make anyone believe I know perfectly how it works. Not seen for the moment, but I am vigilant :- There was an example of this in another thread to do with overlaying structures. I'll look for it! And why does many if not all compilers allow so many extensions? Some are completely understandable, but many including gcc's extensions are too tempting, and when used, code is irremissibly struck with one specific compiler. It may seem strange for me to say that :- In fact I agree with the principle of a standard I've seen this necessity with these many implementations of f77 hanging around , but I don't agree to deny specific use of it, when it's needed.

Only when it's needed. And I'm glad you pointed out a good way to do what was asked in this thread. I hope I've clarified some points. By the way, is it completely portable? Use of an array of chars to read what is very often an integer may seem strange.

It's fun: it's portable only because the standard don't know a pointer is an int or something else :- Actually, we only want to know if NULL is 0, so no problem. On the other hand, I've never heard of this being a problem in practice. If the output isn't intended to be re-scanned which is probably the case most of the time , you merely have to count on the implementer to produce something legible. If you need to be able to re-scan it, it probably suffices to surround it with white space.

Sorry, poor example. So, you can't use delimiters. And how the heck do you print a function pointer? But I don't think any of the machines listed as having non-zero null pointers are in the real world any more.

In the case of the Primes, I certainly hope not. Anonymous wrote: And how the heck do you print a function pointer? Dik T. You can. There is a good reason for that. There are systems where function pointers simply are not compatible with data pointers. For example, when they are much wider. But what is a good reason to print a function pointer? Neil Kurzman. Theoretical vs Practical. Michael Wojcik. It's this that prevents me crossing over.

And what is there on the other side? Only what people longingly refer to as "the other side". Margaret Mitsutani. Depends on your definition of good, but how about debugging a non-portable feature like dynamic libraries? Technically there is no need to be able to print object pointers either, yet we can do so.

Is there a good reason appart from debugging? A pointer that is assigned NULL is called a null pointer. The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. On most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location.

But by convention, if a pointer contains the null zero value, it is assumed to point to nothing. Thus, if all unused pointers are given the null value and you avoid the use of a null pointer, you can avoid the accidental misuse of an uninitialized pointer.



0コメント

  • 1000 / 1000