News for hackers. Stuff that matters.
Re:Oops! We Automated Bullshit by matheusmoreira on Friday November 17, 2023 @08:45AM (#38303443)
Just yesterday ChatGPT thought it was possible to use sizeof in C preprocessor macros. I called it out on this and it doubled down on it.
Re:Oops! We Automated Bullshit by yorwba on Friday November 17, 2023 @09:05AM (#38303698)
Where's the problem?

  ~ $ cat sizeofmacro.c
  #include <stdio.h>

  #define pintsize(T) printf("sizeof(%s) is %zu. Big or small? You decide.\n", #T, sizeof(T))

  int main(int argc, char **argv) {
          pintsize(int);
          pintsize(argv);
          return 0;
  }
  ~ $ gcc sizeofmacro.c && ./a.out
  sizeof(int) is 4. Big or small? You decide.
  sizeof(argv) is 8. Big or small? You decide.
Re:Oops! We Automated Bullshit by blibble on Friday November 17, 2023 @09:34AM (#38304138)
the preprocessor isn't evaluating it, it's just pasting the tokens in

the preprocessor doesn't know anything about structs or types

Re:Oops! We Automated Bullshit by diydsp on Friday November 17, 2023 @09:49AM (#38304343)
I was curious, too. Looked it up. Still don't understand tho.

>The sizeof in C is an operator, and all operators have been implemented at compiler level; therefore, you cannot implement sizeof operator in standard C as a macro or function. You can do a trick to get the size of a variable by pointer arithmetic.

Re:Oops! We Automated Bullshit by mortehu on Friday November 17, 2023 @09:36AM (#38304168)
Do you mean preprocessor directives?
Re:Oops! We Automated Bullshit by pelagicAustral on Friday November 17, 2023 @08:50AM (#38303507)
I've got a colleague that would've double down on it as well...
Re:Oops! We Automated Bullshit by fanf2 on Friday November 17, 2023 @09:13AM (#38303809)
You can use sizeof() in macros but not in #if expressions.
Re:Oops! We Automated Bullshit by matheusmoreira on Friday November 17, 2023 @10:01AM (#38304468)
Yeah, I meant #if directives, like static_assert. I asked it to help me statically verify my assumptions about the ELF format and it gave me some pretty insane code. Here's the excerpt:

> Certainly, you can use a combination of sizeof and #error directives for conditional compilation to achieve a similar effect without static_assert.

  // Compile-time check for structure sizes
  #if sizeof(Elf32_Phdr) != sizeof(((Elf32_Ehdr*)0)->e_phentsize)
      #error "32-bit structure size mismatch"
  #endif

  #if sizeof(Elf64_Phdr) != sizeof(((Elf64_Ehdr*)0)->e_phentsize)
      #error "64-bit structure size mismatch"
  #endif
> In this example, if the structure sizes don't match, the preprocessor will generate an error at compile time using the #error directive.

> I didn't know sizeof could be used with the C preprocessor

> Yes, sizeof can be used in certain contexts within the C preprocessor.

> It's commonly employed in combination with the #if and #error directives to perform compile-time checks on sizes or other constants.

> This usage allows you to catch potential issues during compilation rather than at runtime.

I should probably go sleep a bit before I embarrass myself any further. After nearly 24 hours awake it seems I have lost the ability to tell macros and directives apart.

Re:Oops! We Automated Bullshit by fanf2 on Friday November 17, 2023 @02:34PM (#38308899)
[delayed]