ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am reading about the way Linux Systems Access a null pointer. The GNU "C" implementation allows you to read it, but not write to it.
Code:
char *tst = (char*) 0;
printf("Reading a null pointer gives you %s.\n", tst);
sprintf(tst, "Write to null pointer");
//so you get:
$./tst
Reading a null pointer gives you (null).
Segmentation fault.
If you read it as a string you get "(null)\0". If you are not using the GNU "C" library, you can do neither and you get your segmentation fault on the read.
//so you get
$./tst
Segmentation fault.
How do I eliminate GNU "C" Library to execute this code?
No, you cannot read it. The function printf is cautious enough to check whether you passed a null pointer (which you probably shouldn't), and in that case does NOT dereference it but instead prints "(null)".
The printf function happens to check if the pointer you give it is null, and prints "(null)" if so. There's nothing fundamental in C or linux that makes reading a null pointer give you "(null)". In fact, if printf() had actually used the null pointer, it would have segfaulted.
No, you cannot read it. The function printf is cautious enough to check whether you passed a null pointer (which you probably shouldn't), and in that case does NOT dereference it but instead prints "(null)".
What are you trying to do?
I think this is a great answer!
How do I use a different C Library from GNU C Lib?
Thanks. Aluser's reply is more accurate than your book.
Normally you'd strive to write portable code, that doesn't rely on a particular implementation's quirks. So I assume you are into experimenting and just want to see that SEGV. (You could get one easily with "char oops = *tst;" -- that illustrates that you cannot read through the null pointer.)
To answer your question, read "man gcc", particularly about the linker option -nostdlib; you'll have to supply a replacement library. Disclaimer: I haven't done that.
> The code is from a book. And the book says the GNU "C" Lib allows you to read the pointer but not write to the memory that it points to.
your book must be leaving out an important illustration that would show you why it is could be possible to read or write to a NULL ptr. a NULL ptr points to memory that is just as usable as any other memory if the OS would let it. when memory is laid out for your program at runtime, the lowest memory 0-?? (usually a couple of addresses above 0) are set aside for a NULL area.
anyone correct me if im wrong but i think the runtime address space looks like so
--- high address
heap
---
...
... extra space for heap to grow
...
---
stack
---
NULL address
---
so in theory it would be possible to read or write to null, but it is typically not accessible.
what i am not sure about is if it is read only or not. i thought it was unreadable, completely unreadable. that is why you segfault, because you cant read that adderess. but i have not had a reason to dig into that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.