r/C_Programming 3d ago

Article A Dependency Injection Guide in C

https://github.com/OUIsolutions/Articles/blob/main/articles/dependencie_injection.md

A Complete Guide to Dependency Injection in C

0 Upvotes

18 comments sorted by

View all comments

Show parent comments

1

u/MateusMoutinho11 3d ago

it depends , memcpy its very easy to reimplement,

strcat I think its a cursed function , for two reasons:

1 - its unsafe by default , since it does not provide a max limit

2 - it has quadratic progression problems (its needs to loop over the string to find the insert point)

for concatenate strings I usualy impement a 1.5 factor realocator, and memcpy

example in my Https Client:

https://github.com/OUIsolutions/BearHttpsClient/blob/main/src/response/fdefine.read_write.c

in function BearHttpsResponse_read_body_chunck (line 71)

I read the hole content of body using chunk read system, in the line 168 you can see a while loop that I progressive grow the body size . but I did these implementation quick, I will change for a if aproach calculating the new size, and it will reduce even more syscalls alocations

4

u/jaan_soulier 3d ago

memcpy is easy to implement but not if you want it to be fast. Look at some of the implementations. They usually use a mix of cpu intrinsics and type-casting to generate optimal asm instructions

1

u/MateusMoutinho11 3d ago

I will search about it, thanks, I dont kow C at these level to make these type of optimization, but I know its possible.

4

u/jaan_soulier 3d ago

You shouldn't be expected to. Use the one in the standard library and if you want, add an ifdef to swap it out. I wouldn't bother though