Conversation
what size is argc

genuine question please dont laugh at me
4
0
0
i would presume its either uint8 or something ridiculous like uint32/64 depending on architecture
1
0
0
but i dont actually know which lol
0
0
0
@coolbean um its an into usually but I have no idea what it is in like the Linux ABI
2
0
1
@yakmacker too late i already got confused
0
0
0

@coolbean its a int because bell labs are a flock of useless chuds

1
0
2
@coolbean the type is int yeah
which is usually 32 bits in most C abis
0
0
1
@fiore what size fiore

what size of int
2
0
0
@fiore emilia said 32 usually so ill go with that. its a w instead of an x (i hate forth types with a passion)
0
0
0

@coolbean sizeof(int) , which depends on the target . but its 32bit if we are being real

1
0
1

@coolbean im pretty sure the standard explicitly does not say what the size of int is . but idk maybe we could ask @navi

2
0
0
@fiore @coolbean int is guaranteed to be at least 16 bit in the standard
0
0
2
@fiore @coolbean (oh i was already typing a reply)

first, argc itself purely is defined by the abi, e.g. for the x86_64 sysv abi¹ (which linux uses), the argument count is defined as "an eightbyte", which is more than an int in x86_64 (4 bytes)

but c does require main to be `int argc, char *argv[]`, so, c spec time, does int have a size? yes... kinda², the standard says that UINT_WIDTH, the width for an object of type unsigned int, is 16 bits or more

so your minimum, is 2 bytes, though in most platforms, it's 4 bytes, and "natively" outside c in x86_64 sysv specifically, it's actually 8 bytes, because it's just the word size of the processor -- your libc then implements _start, parses the aux data in the stack, and calls your main function passing in the value as an `int`

not that it matters much, tbh, even with the lowest range here, 2 byte int, that's still 32767 arguments, you're gonna hit posix's ARG_MAX wayy before you hit that limit

1: https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf -- figure 3.9, initial process stack
2: https://open-std.org/JTC1/SC22/WG14/www/docs/n3220.pdf -- 5.2.5.3.2 - Characteristics of integer types <limits.h>
3
0
2
@navi @coolbean @fiore ooh i had no idea about the sysv x86_64 abi actually making it an int64, brb passing 18 quintillion params to my program
3
0
1
@fiore @coolbean @navi the effects of murder are not specified by posix
1
0
0
@emiliamicrosystems @coolbean @fiore
ARG_MAX is 2megs on linux, meanwhile the pointers alone, assuming they point to empty strings, would be 9 bytes each (8 for the pointer, 1 for the null byte)

meaning you have at max 233k empty string arguments before the kernel just straight up says "no"
1
0
2
@fiore @coolbean @navi omg door is that a sun microsystems® spring® reference
0
0
1
@navi @fiore im just trying to avoid causing a memory bug while writing c-interop code in forth so i do, in fact, need to know the actual size so i dont read 8 bytes when its actually just 4 for instance
3
0
1
@navi @coolbean @fiore another example of linux Hating Fun..
0
0
1
@navi @fiore or i read 4 but depending on if its a big or little endian system that reads either the upper or lower bytes of an int64
0
0
1
@coolbean @fiore either keep a list of sizes per platform, or better imo, during build time build a simple c program that just returns sizeof(int)
3
0
1
@coolbean @fiore actually no this is a bad idea it makes cross compilation hell

hm, what does meson and autotools do, lemme look it up
3
0
2
@navi @fiore i love how me kind of losing it about "what size is argc even" has, in retrospect, not made me look like an idiot cause its actually rather troubling
1
0
1

@navi @coolbean @fiore meson would take it from the machine file but i think it can also build a program (assuming it can run executables natively or via wrapper)

0
0
2
@coolbean @fiore well size of argc is int, size of int depends on the system abi

the problem here is getting the size of int from not-a-c-compiler tbh

though, how does your interop works, is it calling forth from c, or calling c from forth, or smth else?
1
0
2
@navi @fiore calling c from forth and also compiling callbacks back in c. in this specific case im running a c function that takes argc and argv pointers that i have to pass along from forth. i can in fact add my own function that just gives me sizeof(int) and i suspect ill leave it at that until someone comes up with a better solution
1
0
1
@coolbean @fiore if the function takes argc and argv, then you can just define the argc of that function to a known sized type, like int32_t or int64_t or whatever word size forth has, and let the compiler do the conversion

or, if it's that dynamic, yeah, take sizeof(int) as an argument somewhere
1
0
2
@navi @fiore that did just remind me that forth cells are also just ints

im killing myself
3
0
2
@coolbean @fiore oh awesome, i love when the solution turns out to be "do nothing" :3
0
0
2
@coolbean @fiore oh, then it's native word size, not ints, :blep: well conversion will be needed then
0
0
2
@navi @coolbean @fiore sizeof is available in preprocessor conditions right? you could at build time run cpp on a file with
#if sizeof (int) == 2
2
#elif sizeof (int) == 4
4
#elif sizeof (int) == 8
8
#else
#error what
#endif

and that works with cross compiling
3
0
0
@emiliamicrosystems @coolbean @fiore

i don't think you can use sizeof in preprocesor directives, the preprocessor has no awareness of types at all (which is why sizeof and _Generic and typeof and such are keywords, not preprocessor macros)

foo.c:1:11: error: missing binary operator before token ‘(’
1 | #if sizeof(int) == 4
1
0
0

@navi @emiliamicrosystems @coolbean @fiore Yeah only way with asking C is probably to use static_assert (C11+) and a shell loop, which is awfully autotools.

1
0
1
@coolbean @fiore @navi usually intptr or smth lime that so the compiler doesn't yell at you and to make sure it's register sized
0
0
1
@lanodan @coolbean @fiore @navi you don't need static assert (you can use typedef foo char[cnd ? 1 : -1] or switch(0){ case 0: case cnd: }) but yeah
1
0
0
@emiliamicrosystems @coolbean @fiore @navi Yeah, somewhat less readable but can be used as polyfill.
0
0
0
@navi @coolbean @fiore but you can just hardcode/assume 32-bit for int unless u wanna support 16-bit platforms or some cursed dead ILP64 unix
0
0
2
@navi @coolbean @fiore on sysV anything other than a float can go in the same register without issues, as I found with a hackjob repl that I made without knowing how to parse C code
0
0
1

miss @emiliamicrosystems , mx @coolbean , miss @fiore , miss @navi : think the correct way to do this is with the __INT_WIDTH__ macro instead

3
0
1
@coolbean couldn't you just write a program to print sizeof(argc)? or is that not how it works.
0
0
0
@minekpo1 @coolbean @fiore @navi where is that supposed to be defined
2
0
1
@emiliamicrosystems @coolbean @fiore @minekpo1

seems like it is, but actually, you can also `#include <limits.h>` and use UINT_WIDTH which is standard
1
0
1
@navi @coolbean @fiore @minekpo1 it is standard in C23, which is not available everywhere and not required by POSIX
0
0
1
@minekpo1 @coolbean @fiore @navi there's INT_WIDTH without the underscores but thats C23
0
0
1