Live data from Hacker News

Writing C software without the standard library

weeb.ddns.net

91–100 of 188 posts

Re: Writing C software without the standard library

#91

Earlier quoted context omitted.

Unless you also need to free, it's pretty simple.

Of course. When I wrote that comment I asked myself should I have written "malloc" or "malloc/free" - surely one implies the other.

Not surely. Many programs are written in a way that allocates all the needed heap space at startup, and just reuse it forever. And those are overrepresented on the minimal-system kinds of environment.

Re: Writing C software without the standard library

#92

Earlier quoted context omitted.

Yeah off topic but if HN supported markdown (and GitHub's flavor of markdown so we could take different types of syntax with their language) would be amazing. There would be a ton more coding examples and discussion, in my opinion, if this were to happen. How do we summon Dang? :)

my netiquette says, more than about three lines of code should go in a pastebin anyway

Then in a few years when someone is reading old posts the links to old services are broken or the service is gone. If the code is central to the comment, why would you put it some place other than the comment? If it's not small enough you can collapse it inline and if it's so large that you don't want it with the comment then perhaps you should be rethinking what you are posting.

tldr; practicality and longevity should trump netiquette.

Re: Writing C software without the standard library

#93
post #4

He claims that your code will be easy to port but then goes straight to Linux system calls. Still I like the idea. This is something that should be covered in a CS 102 type course. I know way to many cs guys who have no idea how to debug, let alone how their is being implemented.

As far as PortableOSIX compliant syscalls are concerned, they are literally portable.

The userspace/kernel interface for invoking syscalls which the OP calls with assembly code is obviously not portable. Not even between similar OSs on one architecture and absolutely not between architectures.

POSIX only specifies a set of C library functions. Once you stop using them, you are on your own.

Re: Writing C software without the standard library

#94
post #85

Earlier quoted context omitted.

Win32 is itself a layer on top of the syscalls, and already provides much of the functionality of the C standard library.

Yes, that was partly my point. You can get reduced executable size and a lack of dependencies on various msvc*.dlls, without giving up much of the functionality of the C standard library and without having to write it all yourself.

Fine, but you get this thanks to already being linked with these megagodzillas like kernel32.dll. Try without them.

Re: Writing C software without the standard library

#95
A little self promotion but mostly because it addresses some of the other commenters concerns about malloc (or the lower-level api around sbrk): a couple years ago I wrote rt0 [0], a small (mostly minimal) C runtime for i386 & amd64 that makes it easier to replace libc & crt0 (as long as you have the kernel headers installed). Also, as part of the examples, I wrote wrappers around the sbrk syscall. Pretty easy to do and all documented in the repo. I expect to eventually port the lib to arm (raspberry pi) and aarm64. There's also lots of references to other small c runtimes. I'll be adding this one as well.

[0] https://github.com/lpsantil/rt0

Re: Writing C software without the standard library

#96
post #94
post #85

Earlier quoted context omitted.

Yes, that was partly my point. You can get reduced executable size and a lack of dependencies on various msvc*.dlls, without giving up much of the functionality of the C standard library and without having to write it all yourself.

Fine, but you get this thanks to already being linked with these megagodzillas like kernel32.dll. Try without them.

A program which isn't linked against the Win32 stuff is an invalid program, since syscalls in Windows are not at all stable.

Re: Writing C software without the standard library

#97

A value in the range between -4095 and -1 indicates an error, it is -errno. The syscall/errno stuff has always seemed unusual, inelegant, and inefficient --- instead of just returning a negative error code directly, the function returns the vague "an error has occurred" -1, and you have to then check errno separately after that. It only adds insult to injury when you realise that the kernel itself isn't doing it, but…

> At least on Linux the first few (i.e. the oldest, most common and useful) syscalls have not really moved around over the years IIRC raw syscalls are an officially supported kernel API, that's why you can have alternate libc implementations (e.g. musl), and Linux is an oddity in that, on most systems even if the syscalls are fairly stable there are no actual guarantees with respect to them, and the only officially s…

Another example: for OpenBSD part of the manual upgrade procedure (not the recommended method, but useful if you don't have a console to run the installer) is to make a copy of /sbin/reboot, because there's a good chance that the new /sbin/reboot you're about to install will have the wrong syscalls for the running kernel.

Re: Writing C software without the standard library

#98
> When we learn C, we are taught that main is the first function called in a C program. In reality, main is simply a convention of the standard library.

Well, I've already learned something new. I assumed that convention was from the compiler. This is a great resource.

Re: Writing C software without the standard library

#99

Earlier quoted context omitted.

Yeah off topic but if HN supported markdown (and GitHub's flavor of markdown so we could take different types of syntax with their language) would be amazing. There would be a ton more coding examples and discussion, in my opinion, if this were to happen. How do we summon Dang? :)

my netiquette says, more than about three lines of code should go in a pastebin anyway

That's a very personal netiquette :-)

Re: Writing C software without the standard library

#100
post #77
post #4

He claims that your code will be easy to port but then goes straight to Linux system calls. Still I like the idea. This is something that should be covered in a CS 102 type course. I know way to many cs guys who have no idea how to debug, let alone how their is being implemented.

When I was learning C, I loved Plauger's book on the standard library for just that reason; it's very informative to be guided through the implementation of the whole thing, not only to see how it works, but it's also a good way just to be introduced to everything in it without any magic. It's an excellent 2nd book on C, I think.

Plauger is one of my unsung heroes.

I discovered a little book by him after wading thru recursive descent parsers chapter by Herb Schildt... I don't want to get down on Schildt, I enjoyed that too, he showed some cool stuff..

but Plauger had me thinking that programming could be a craft, an art, a noble intellectual pursuit.

Post reply on HN