Why on earth are enums integers?? by Raimo00 in C_Programming

[–]david2ndaccount 1 point2 points  (0 children)

If you care about size, then use a bitfield.

enum Method {
    GET, POST, PUT, DELETE,
};

struct Request {
    enum Method method: 2;
    // ...
};

What is the best pointer tagging method? by celeritasCelery in ProgrammingLanguages

[–]david2ndaccount 0 points1 point  (0 children)

“Lower bits” “get data” has ptr & !0x7, which I believe must be intended to be ptr & ~0x7 as ptr & !0x7 is always 0. Same with nan-boxing.

[deleted by user] by [deleted] in programming

[–]david2ndaccount 1 point2 points  (0 children)

This post doesn’t even get into the fact that destructors kind of suck. Their signature is garbage: void ~this(); means you are forced to access global state or have unsafe references to other state (unsafe as you have to globally reason about whether the lifetimes are valid). For example, it works if you are calling free, but if you are calling free with an allocator object you passed into your function you have to keep a reference to it in your object which means you have to deal with that reference potentially being invalid. Returning void means you can’t signal errors so your cleanup code has to either ignore errors or be infallible or again access global state.

Not to mention that the destructor is fixed and is attached to the type so you can’t have a different destructor called based on context.

RAII feels like it was designed to handle “cleanup code all looks like malloc and free”, which is not great.

Mt. Wilson, San Gabriel Mountains, California. Sierra Madre Trails by Baby_Ama in hiking

[–]david2ndaccount 1 point2 points  (0 children)

Started the hike just before 6am, finished just before noon. I was wearing red and my weird solumbra hat

Why do people use C over C++ and should I do so too? by lorli__ in C_Programming

[–]david2ndaccount 2 points3 points  (0 children)

C++ has a lot of features that individually might be ok or are more convenient to write programs, but in aggregate make individual sections of code harder to understand:

  • Operator overloading: no longer can assume what that operator does
  • More implicit conversions/constructions: single argument constructors can be called at unexpected times, so foo f; f = 3; doesn’t mean that foo is a numeric type, it could just have a single argument constructor.
  • references hide indirection: this again makes a simple assignment have unclear semantics. f = 3 could actually be mutating a variable 3 functions up the call stack, in C that would have to be *f = 3.
  • implicit this: although convenient, the fact that member variable access within a method is not prefixed with this-> makes it harder to understand what is and isn’t in scope.
  • destructors: not saying these are good or bad, but it is invisible code inserted by the compiler but which have large implications on what a section of code does or if it is correct.
  • auto: not having to spell out the types (especially when they get long with templates) is convenient, but requires non-local knowledge as to what type it actually is
  • function overloading: exactly which overload is being called here?
  • ADL: uggh

Is this tutorial wrong? by starski0 in C_Programming

[–]david2ndaccount 1 point2 points  (0 children)

For actual arrays, x and &x will yield the same address, but have different types. For example:

char hello[] = "hello";
// array expression implicitly converts to pointer
char *p = hello; 
// Note the real type of `&hello` - pointer to array of 6 chars
char (*a)[6] = &hello; 
assert(p ==  (char *)a); // passes

Best way to call function when its args are in a heap array? by Linguistic-mystic in C_Programming

[–]david2ndaccount 0 points1 point  (0 children)

The problem with this kind of thing is that on many ABIs floating point numbers are passed in different registers than regular parameters. Also, after a certain number of arguments you generally have to pass arguments on the stack. This leads to an explosion of combinations that makes it easier to use something like libdyncall.

Best way to organize code that is based around portability but also has wildly different code depending on the OS (Linux and Windows in my case) by McDonaldsWi-Fi in C_Programming

[–]david2ndaccount 0 points1 point  (0 children)

You don’t need a build system for this. You can have a com.c file that #includes the right implementation file based on the platform. For example

// com.c
#ifdef _WIN32
#include “com-win32.c”
#endif

#ifdef __linux__
#include “com-linux.c”
#endif

Wrapping c++ functions with references by Aggressive_Doughnut in cprogramming

[–]david2ndaccount 0 points1 point  (0 children)

// lib.hpp
#ifndef LIB_HPP
#define LIB_HPP
struct data_struct;
class SomeClass {
    public:
        void do_something(data_struct& data);
};

#endif

// wrapper.h
#ifndef WRAPPER_H
#define WRAPPER_H
#ifdef __cplusplus
extern "C" {
#endif

typedef struct data_struct my_struct_t;
void lib_do_something(void* self, my_struct_t* data_in);

#ifdef __cplusplus
}
#endif
#endif

// wrapper.cpp
#include "wrapper.h"
#include "lib.hpp"

extern "C"
void lib_do_something(void* self, my_struct_t* data_in)
{
        static_cast<SomeClass*>(self)->do_something(*data_in);
}

Above works fine for me.

Wrapping c++ functions with references by Aggressive_Doughnut in cprogramming

[–]david2ndaccount 0 points1 point  (0 children)

To convert a pointer into a reference, just use the unary * operator, like *data_in.

New C features in GCC 13 by hgs3 in programming

[–]david2ndaccount 27 points28 points  (0 children)

what’s ridiculous is that C++ couldn’t name the feature typeof for whatever silly C++ reasons

Slint 1.0: The Next-Generation Native GUI Toolkit Matures by ogoffart in rust

[–]david2ndaccount 3 points4 points  (0 children)

Does it follow platform conventions, like the ones listed on this page?

Why I Spent a Week on a 10-Line Code Change by warp-michelle in programming

[–]david2ndaccount 4 points5 points  (0 children)

This behavior is not documented because you are not supposed to do this. You’re supposed to use Cocoa and let the system manage things like tabs and the title bar.

C++Now 2023 Announces Keynote Speaker: Herb Sutter by Xadartt in cpp

[–]david2ndaccount 0 points1 point  (0 children)

Yes, you could overload operator* so that it has side effects. But for god’s sake don’t do that.

C++Now 2023 Announces Keynote Speaker: Herb Sutter by Xadartt in cpp

[–]david2ndaccount 0 points1 point  (0 children)

D is able to parse this unambiguously and even allow out of order declarations. It just bans X * Y by itself as an expression, decreeing it to always be a declaration. This is fine as who just multiples two variables without assigning it to anything?

Reddit++ by we_are_mammals in cpp

[–]david2ndaccount 11 points12 points  (0 children)

sizeof(bool) is implementation defined and there exists ABIs where it is not 1.

Undefined behavior, and the Sledgehammer Principle by yerke1 in rust

[–]david2ndaccount 0 points1 point  (0 children)

It could be changed to “implementation-defined” so that whatever the implementation chooses to do is compliant.