r/cprogramming • • Jan 22 '26

Why some famous open source projects rewrite some C standard function from zero?

Hello,

I was watching NGINX and libuv source code and noticed that both the projects (at different ratios) rewrite standard functions (such as string manipulation functions) or rewrite existing macro including their prefix (es.
UV__INET6_ADDRSTRLEN in inet.c).

Is it due to performance or maybe to create a common wrapper between OS?

Thanks!

55 Upvotes

52 comments sorted by

View all comments

Show parent comment

2

u/RoomNo7891 Jan 22 '26

which approach do you think is a good one?

I was thinking of a struct with a size_t for the len and the char * containing the string itself, but not sure if efficient or not.

6

u/[deleted] Jan 22 '26

How else would you do it?

1

u/pjl1967 Jan 22 '26

That's how most every other programming language implements strings under the hood.

1

u/ComradeGibbon Jan 22 '26

I just do embedded stuff, but I have two types a slice and a buffer type. The buffer is just a slice with a capacity.

Rally for non embedded stuff you should use someones string library.

1

u/EpochVanquisher Jan 22 '26

Usually you would also have a size_t for the buffer size too. That way, you can append to a string in-place and know when to resize the buffer.

But ptr/len makes more sense if you are not modifying the string.

1

u/[deleted] Jan 22 '26

It's conventional wisdom to leave strings immutable. Only madness can be found in the way of mutable strings.

1

u/SymbolicDom Jan 23 '26

Mutable strings can in some cases be more performant. Change a byte here and there is way faster than allocating memory on the heap.

3

u/[deleted] Jan 23 '26

Of course it is faster, it's almost as if there's other issues with mutable strings.

1

u/serious-catzor Jan 24 '26

you made me laugh, have a upvote

0

u/EpochVanquisher Jan 22 '26

It is conventional wisdom to have both options available.

1

u/[deleted] Jan 22 '26

It's not. There isn't a single standard library out there whose language uses mutable string. Even the ancient standard C library does not modify input strings, for good reason.

2

u/EpochVanquisher Jan 22 '26

Not talking about modifying input strings, but just about writing a string library—which usually involves a writable buffer to create strings, and a representation for strings that you’ve created.

I thought this was clear but I guess you assumed that I was thought all strings should be mutable, or something like that. We both assumed wrong.

1

u/[deleted] Jan 22 '26

It is conventional wisdom to have both options available.

Is what you wrote, but that's just not true. Yes, internally in the library a string will briefly live a life as a mutable buffer, but the moment it leaves the function it should never change again.

So no, you don't have both options available. I am not sure what else you could've meant.

2

u/EpochVanquisher Jan 22 '26

I don’t see how your point is relevant, sorry.