News for hackers. Stuff that matters.
Re:Weekend projects: getting silly with C by cbrpnk on Sunday June 30, 2024 @09:04AM (#40836937)
I don't think I've ever thought of explicitly calling main(). Made me chuckle.
Re:Weekend projects: getting silly with C by akdev1l on Sunday June 30, 2024 @01:11PM (#40838417)
I think it is UB

Edit: actually looks like it is UB in C++ but not C

Re:Weekend projects: getting silly with C by colejohnson66 on Sunday June 30, 2024 @06:24PM (#40840874)
Why would calling main be UB!? How is crt0 supposed to work?
Re:Weekend projects: getting silly with C by tomjakubowski on Sunday June 30, 2024 @07:44PM (#40841382)
crt0 generally isn't C and isn't subject to C's rules
Re:Weekend projects: getting silly with C by astrange on Sunday June 30, 2024 @09:36PM (#40841860)
malloc() can't be implemented in C either because it's defined as doing things (creating new memory objects) there are no lower level mechanisms in C to do.
Re:Weekend projects: getting silly with C by eacnamn on Monday July 01, 2024 @05:04AM (#40843907)
all malloc is defined to do is to return a pointer to storage of appropriate size and alignment, which can easily be done in pure standard C by defining a static array and chopping it up as needed. that's not a brilliant way of doing that, but achievable without leaving standard C
Re:Weekend projects: getting silly with C by astrange on Monday July 01, 2024 @07:16PM (#40851860)
> The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

> The lifetime of an allocated object extends from the allocation until the deallocation. Each such allocation shall yield a pointer to an object disjoint from any other object.

A static array is "an object" already. A pointer to the middle of it is not a new object.

Re:Weekend projects: getting silly with C by gpderetta on Tuesday July 02, 2024 @05:28AM (#40854882)
AFAIK that can break because of the strict aliasing rules (although it might work in practice). Even if char can alias anything, the reverse is not true and you can't legally store other types in a static array of char type. You should be able to use anonymous memory though, so for example if you get your storeag via mmap or some other allocator it should be fine.
Re:Weekend projects: getting silly with C by astrange on Tuesday July 02, 2024 @01:58PM (#40858969)
There isn't anything called mmap in the C standard. That's what I mean by it not being possible to implement in standard C. It is possible in some implementations of C.
Re:Weekend projects: getting silly with C by akdev1l on Monday July 01, 2024 @09:18AM (#40845608)
As the other person pointed out, anything that happens before main is strictly not covered by the C standard.

Even things like “printf” can’t be implemented purely in standard C. Even making a syscall is outside of the scope of the C standard.