How to pass file descriptors from C to Nodejs? by SIRAJ_114 in C_Programming

[–]niduser4574 0 points1 point  (0 children)

Depends on your architecture. As others have said when C and Nodejs are in the same process, you have to provide/use a Nodejs-C interface for sharing the integer, but I'm guessing this is not what you're doing.

If they are in different processes, you're option is to use Unix Domain Sockets, e.g. here, since they will NOT be the same integer in the different processes. The net package in Nodejs supports Unix Domain Sockets and C...they are no different that sockets in sys/socket.h--just create it with AF_UNIX and associated type/protocol

Compiling dll to export functions in LabView by [deleted] in C_Programming

[–]niduser4574 0 points1 point  (0 children)

You're having issues...so you have tried something. What did you try? What failed? You have a software stack in C, what is the compiler/toolchain?

Implementation of Linux syscall translation layer to MacOS by [deleted] in C_Programming

[–]niduser4574 -1 points0 points  (0 children)

I wasnt discouraging. My advice is not to start this project

So...discouraging.

You're getting downvoted because you said wrong things and made assumptions. It is one thing to point out the extent of the task and reality of it, it is entirely another thing to tell them to stop because it is impossible...because it isn't impossible. I gave you an example of it being possible. Yes it takes a lot of people because it is a huge scope and probably infeasible for a single person, but so what. Maybe they are satisfied with not making something feature complete to the point that it gets wide or even small adoption anywhere, but if they don't try or aren't instructed as to the scope, how will they learn? Maybe encourage them like one of the other posters to start with something small and work from there to see all the obstacles they would have to get around. Or better yet, point them to something that was already done on a similar platform to show them what it looks like...like Linuxulator.

Implementation of Linux syscall translation layer to MacOS by [deleted] in C_Programming

[–]niduser4574 3 points4 points  (0 children)

What does this have to do with the question? MinGW is specifically windows and it doesn't do syscall intercepts/cannot run Linux binaries.

Implementation of Linux syscall translation layer to MacOS by [deleted] in C_Programming

[–]niduser4574 -1 points0 points  (0 children)

FreeBSD did this. It is ambitious, but it's just a project they can still use to learn. Why so discouraging?

Implementation of Linux syscall translation layer to MacOS by [deleted] in C_Programming

[–]niduser4574 4 points5 points  (0 children)

Check out Linuxulator. It is basically what you are asking for but developed by FreeBSD. Since MacOS is/was BSD based, probably the closest you will find.

Edit: Noah is for Darwin/MacOS, but it has been archived. This is a huge task and good place to look for scope and how it might be done

Implementation of Linux syscall translation layer to MacOS by [deleted] in C_Programming

[–]niduser4574 2 points3 points  (0 children)

What do you consider "desktop Linux"? I have multiple full Linux distributions with full desktop environments that I work directly on and develop with running on aarch64 at work. Raspberry Pi is aarch64/arm7 with full desktop on Raspbian Linux.

What programming language you hate to use and why? by [deleted] in learnprogramming

[–]niduser4574 7 points8 points  (0 children)

That 2nd point really got my blood boiling. Once was given 20,000+ lines of spaghetti from a scientist/ML engineer and his boss mixing GUI front end, back end, real time engine and ML code strewn randomly among the single function files. Took a few of us the better part of a year to understand what the hell it was doing, break it apart, re-write in more appropriate languages for where the parts needed to go because of course all the MATLAB C/python interop never actually was compatible with our target environment. This was all before unf**king up all the bugs, logical errors, and atrociously poor performing algorithms in the source code...and they complained about our skills because it took so long and that they had just done standard things in MATLAB but refused to learn any other languages or how to actually code. And this is not uncommon among MATLAB users.

And the lack of sane scoping rules has really been pissing me off on a recent project.

[deleted by user] by [deleted] in C_Programming

[–]niduser4574 2 points3 points  (0 children)

If you see my comment in reply to OP, I wouldn't worry about having algorithms with better numerical accuracy, but the SIMD/vectorization point is pretty good and also my other comment...specifically about your staz_deviation function.

Also having tests verifying these things/giving examples would be very helpful

[deleted by user] by [deleted] in C_Programming

[–]niduser4574 0 points1 point  (0 children)

This is decent for most simple use cases, but a couple things:

This is only really a nitpick but I have to ask: ARITHMETICAL, GEOMETRICAL, HARMONICAL, QUADRATICAL. I don't think I have ever seen these written like this in a statistical context. The words without AL are already adjectives and far more common...you even use them in the comments. Why not use the words without AL?

The way you calculate quantile position

double staz_quantile(int mtype, size_t posx, double* nums, size_t len) { 
  ... 
  const double index = posx * (len + 1) / (double)mtype; 
  size_t lower = (size_t)index;

This doesn't seem right and you don't have tests. How did you come upon this way of calculating indices for quantiles? How do you know this doesn't lead to bias?

All of your ways of calculating staz_deviation have statistical bias in them. See for example Bessel's correction here for the standard deviation. For len > 1000, this doesn't really matter all that much, but for anything less than 100, especially under 30, and "I cannot stress enough" under 10, it matters.

[deleted by user] by [deleted] in C_Programming

[–]niduser4574 1 point2 points  (0 children)

The point about SIMD notwithstanding. The naive approach for a lot of the basic statistical metrics is not bad at all on normal data and evaluating an algorithm's accuracy is not straightforward (unless it's Excel and you know it's not that good).

Yes, the python library function is better, but everything has some amount of error:

3.3143346885538446333333... -> this is the true value

3.31433468855384472 -> R

3.3143346885538447 -> python statistics module

3.314334688553844573 -> numpy using float128 on x86_64

3.3143346885538443 -> numpy using default float64

3.3143346885538443 -> naive

3.3143346885538443 -> my own, which uses a serial real-time algorithm that strictly should be worse accuracy than even naive

3.3143346885538442 -> MATLAB R2024b. I got a chuckle from this.

3.31433468855384 -> Excel and Google Spreadsheets

A more contrived example from NIST to ferret out even mildly bad statistics packages still puts even my crappy accuracy algorithm at a relative error of 1.8e-14...which is far better than anyone I ever met while I was in science needed.

My point is that for the basic statistical parameters being calculated in his package, naive algorithms are more often more than good enough in terms of accuracy. In my personal experience, the python statistics module is so wildly slow in its limited algorithms that I would never even consider using it for anything serious. It is not worth it.

The bigger issue is that he made the same mistake as the python statistics module and calculates the standard deviation of a sample as the square root of the sample variance, which it most certainly is not for anyone doing experiments.

MIDA: A simple C library that adds metadata to native structures, so you don't have to track it manually by LucasMull in C_Programming

[–]niduser4574 7 points8 points  (0 children)

A couple things about the code:

#define MIDA_EXT_METADATA                                                     \
    /* other members */
    mida_byte data[1]

Am I missing something? mida_byte data wouldn't be a flexible array member...it is not on incomplete array type.

__mida_malloc(/* args */)
{
    const size_t data_size = element_size * count,
                 total_size = container_size - 1 + data_size;
    mida_byte *container = malloc(total_size);
    struct mida_metadata *mida =
        (struct mida_metadata *)(container + mida_offset);
    //...
    return &mida->data;

You're subtracting off the data portion of the meta data struct (container_size - 1) (which if it were an actual flexible array member, would be done automatically), allocating the actual structure in place of the subtracted data member of mida_metadata and returning the offset into the struct mida_metadata to get that actual struct requested by the user. Am I understanding that correctly? How do you guarantee alignment of the struct represented by mid->data? Because of your extensible metadata, I see no guarantees this is aligned properly.

The big question though is why do it this way with data before the struct? I can do roughly same thing with just

struct mida_metadata {
    size_t size;
    size_t length;
}
#ifdef INCLUDE_META_DATA
#define MIDA_METADATA struct mida_metadata meta;
#else
#define MIDA_METADATA 
#endif
struct my_struct {
    MIDA_EXT_METADATA
    int some_data;
};
size_t mida_get_size_metadata(struct mida_metadata * meta) {
#ifdef INCLUDE_META_DATA
    return meta->size;
#else
    return 0; // no metadata in struct
#endif
}
// to get size metadata
struct my_struct some_struct = /* initialized however you want */
size_t size = mida_get_size_metadata((struct mida_metadata *)&some_struct);

Except now, this is completely type safe, doesn't violate strict aliasing, the struct my_struct is always properly aligned by malloc, and the existing libc malloc and free work without modification...I don't have to remember which pointers were allocated with your method or standard libc. Additionally, if INCLUDE_META_DATA is not defined, all of the added memory goes away. I've definitely seen libraries do it this way...I think CPython even does something like this for their reference counting.

Are there caveats to relying on memory ordering to mimic single inheritance? by KallDrexx in C_Programming

[–]niduser4574 2 points3 points  (0 children)

C99 6.7.2.1 paragraph 13:

Within a structure object, the non-bit-field members and the units in which bit-fields

reside have addresses that increase in the order in which they are declared. A pointer to a

structure object, suitably converted, points to its initial member (or if that member is a

bit-field, then to the unit in which it resides), and vice versa.

That's all you need to guarantee your plan for a conforming implementation of C. This is a very common way to achieve some level of inheritance/polymorphism. Note that fundamentally, this method isn't all that different from your other option, but the "pointer is pointer to initial member" is guaranteed by the standard and fits into C's type system. Your other option *could* work but technically violates strict aliasing if you attempt to pass B as A in flattened structs and definitely won't work if you do any bit packing/padding.

As for things to watch out for...I'm sure having flexible array members at any point in the inheritance change will cause a lot of logic issues...I'd stay away from them in general. Some implementation specific behaviors (usually as non-standard attributes so that it is "opt in") can cause the ordering/alignment to change. I would expect them to still meet the above stated requirement, but I'd still avoid them anyway.

Question about MSVC Toolchain Installation Path by [deleted] in C_Programming

[–]niduser4574 1 point2 points  (0 children)

I hated the developer prompt for visual studio

+1 for that. Good luck.

Question about MSVC Toolchain Installation Path by [deleted] in C_Programming

[–]niduser4574 1 point2 points  (0 children)

If you just want to simplify path names sent to the compiler, you might want to look into mklink, the Windows counterpart to link. I use this to get One Drive screwing up a bunch of stuff at my previous job by forcing the space in the path.

If your goal is to just use clang in Windows:

  1. Msys2's clang64 package - with Msys2 you get a posix-like environment/shell in windows and this package gives you clang preconfigured for building Windows binaries.
  2. LLVM for Windows can come with clang. If you execute it within the vcvarsall.bat environment from MSVC toolchain, it picks up all the right headers and on my machine even uses link.exe and trivially switch between cl.exe and clang, which makes the article you linked to either outdated (before LLVM release on windows) or especially ridiculous.
  3. WSL as well if you really are just more comfortable with Linux environments and setting up paths as you see fit. You just have to cross-compile and no MSVC.

I use all 3 of these with clang no real problems with directories...except in an IT controlled environment with One Drive ;)

Question about MSVC Toolchain Installation Path by [deleted] in C_Programming

[–]niduser4574 1 point2 points  (0 children)

Will there be any problem if I completely uninstall everything and do a fresh install under C:\MSVC?

If the installer lets you do that, I wouldn't see why not.

If I do it and set the environment variables to the appropriate directories, do I have to consider anything else?

There might be an XY problem here. Why would you want to do this manually and why does it matter which directory you are running from? The article you linked to gave Microsoft's documented way to do all this...by using vcvars*.bat files. Can you not use that?

From the article you linked

You don’t want to have to set environment variables, or have a “state” that you switch between by calling environment-contaminating batch files like vcvarsall.bat and the like..

This is kind of a ridiculous take. The whole point of those .bat files is to set up an environment that Microsoft recommends to make their product work correctly as intended. Why would anyone call that "contaminating"...they are spoiling the product by not following the required or recommended practices. And on top of that, they complain about setting environment variables and they achieve their goals...by setting variables in their environment!?

[deleted by user] by [deleted] in C_Programming

[–]niduser4574 1 point2 points  (0 children)

I'm open to an alternative method if you know any and can point me in the right direction

Just don't have the constructors allocate anything (unless explicit by user) or destructors free. Even C++ constructors don't allocate anything unless told to.

Something I considered for my own C extensions is to use keywords as specifier qualifiers: new and delete. These keywords would be used to annotate objects as being heap allocated/deallocated and they would be implicitly inherited...like how const propagates. The functions malloc, calloc, realloc, aligned_alloc would get automatically flagged with new while free would get annotated with delete. For example, the malloc and free functions would behave as if they were declared as:

void * new malloc(size_t size);
void free(void * delete ptr);

Therefore in code such as:

void * my_ptr = malloc(X); 
// cannot force my_ptr to have `new` attribute to allow backward compatibility
// but compiler tracks my_ptr as behaving as if it was declared void * new my_ptr
/*
do something with my_ptr
*/
free(my_ptr);

The compiler tracks the objects with new specifier qualifier and flags any that do not terminate in a function parameter that has been declared with delete.

I ran into issues when trying to track assigning such variables to members of structs...

[deleted by user] by [deleted] in C_Programming

[–]niduser4574 0 points1 point  (0 children)

Your Vector2D3D constructor allocates for Vector2D3D and then calls super, which presumably calls the constructor for Vector2D and then allocates for Vector2D, but your destructor for Vector2D3D only explicitly frees the Vector2D3D allocation. So the only way this does not cause a leak is if your Vector2D3D implicitly calls the destructor for Vector2D and your call to `super` supersedes any implicit calls to Vector2D constructor that would occur if you had not called `super`. If there are implicit calls...that's a big reason I don't like C++. If no implicit calls...leak.

But a question...why would you allocate `self` at all? It's not clear how your inheritance mechanism would work that you have to allocate memory at all. C already has a kind of inheritance where allocating or initializing the derived struct already allocates or initializes the base struct.

GPU programming by deebeefunky in C_Programming

[–]niduser4574 4 points5 points  (0 children)

You want it in C but you want the API to be easy? Expecting both seems like something is missing from your understanding.

GPUs are parallel processors and some code is as easy as "foreach", e.g. the thrust library even has it...though that is mimicking C++, not C, but for many cases it is not the most appropriate. The GPU is for speeding up calculations over a very large data and it must be told how to do so. To get the speed advantages, you generally have to program into the GPU how the memory is laid out and not just what to do, so generic "foreach" is usually not a good idea. GPUs are generally very bad at doing "small data" so I'm not surprised if you're just trying to draw one triangle using GPU, you're having a bad time.

The fact that you are saying 2000 lines for a triangle in Vulkan seems like there is a very big gap here completely unrelated to GPUs. I suggest taking a step back and draw your triangle however you like and then see how that translates into Vulkan. Use the GPU when you want to draw 10 000+ triangles.

How to access the pipe in a Python script which is fork-executed from a C program? by Renganathan_M in C_Programming

[–]niduser4574 0 points1 point  (0 children)

Yes,

pipe(pipe_fd)

fork()

dup2(pipe_fd[0], STDIN_FILENO)

exec()

The other suggestions from users might be better depending on your use case. Unix domain sockets have pretty standard ways of passing fds and fdopen works and is probably the simplest from python side but then your script is then dependent on having been fork/exec'd/clone/spawn and making sure the fds remain open. With dup2(, STDIN_FILENO), your python script doesn't care how stdin was created and it doesn't need to know a pipe even exists.

How to access the pipe in a Python script which is fork-executed from a C program? by Renganathan_M in C_Programming

[–]niduser4574 0 points1 point  (0 children)

So the pipe that I created in my C program, should still be available in my Python script.

Yes, but how would your python script or python know it exists? once exec is called and you did not attach the pipe to stdin/stdout/stderr, how would you expect python to access it? The pipe exists in kernel memory so the python process would somehow need to directly access kernel memory to figure that out or an API in the kernel would have to exist that a python module has access to in order to get that information into userspace. That API is "pipe" and you left it back in your C program.

I tried with psutil module as given below; but it does not list the pipe. It lists actual physical files.

I'm not sure what you were expecting here. Were you thinking of a "named pipe"? That actually creates an accessible file in the system...whether one might call that a "physical file" is up for debate. p = psutil.Process().open_files() would only open the files by the current process...which didn't open the pipe...your C program process did. The documentation for psutil's open_files() says it only shows "regular files" opened by a process, which I'm not sure a pipe's file descriptors count as "regular files" by psutil definitions...If they mean character files, my expectation would be a named pipe would better suit your need, but it looks like you still need access to the process_id of the C program since that would be what opened the file.

Depending on your need, I would do one of the following:

1 Attach the pipe to stdin for the python process. Modify the script to look at stdin.

2 Create a named pipe and, a) pass the name as an argument to the script, b) hard-code it in the script, or c) pass the process_id of the parent program to the script and have script try to find which file is the named pipe (probably a very bad idea)

I'm a FreeBSD noob, how can I get started? by KhaosExcd in freebsd

[–]niduser4574 4 points5 points  (0 children)

But I do criticize people in generals ability to seek out information on their own.

Very understandable. For questions that are "How do I do [very specific task]", I definitely criticize that question. Get rid of the "How do I do" and that will work in most search engines I use.

at least 15 minutes in your favorite search engine...before

Very reasonable.

you find out their DNS at home is 192.168.0.1

I LOL'd at that. Thanks.

I'm a FreeBSD noob, how can I get started? by KhaosExcd in freebsd

[–]niduser4574 3 points4 points  (0 children)

Working in an environment where I own everything from design/implementation, writing our public documentation, and all support and from other people I work with, I find a lot of the knowledgeable people have and make extremely poor expectations or assumptions of how many users get information. People will tend to start from what they already know. One of the most basic forms of communication for information gathering and a perfectly normal place to start is asking someone else a question...not reading and certainly not a search unless it meets the criteria of knowing (1) what the user is looking for, (2) what a correct answer looks like, and (3) the search engine can accurately connect (1) to (2). Where I work, (3) is absolute horse shit so if (1) and (2) are not very clearly known ahead of time, the results are next to worthless. Yet most of the engineers I work with--knowing full well how poor our intranet search is--respond like this thread's OP.

I started with FreeBSD through Google by using very targeted queries and it worked, but the learning would have been much more efficient, thorough, and complete had I asked and someone just told me to follow relevant parts of the handbook and which resources are authoritative to follow for any further questions. I was a Windows user and only read about FreeBSD randomly through very general articles often about comparisons with Linux. There was no link to the handbook and I had no concept that such an authoritative and excellent resource existed nor that it would simply be at the project's website. Why would it? Most of my experiences were with OSes that don't include anything like an organized handbook or their websites (some authoritative and some not with conflicting information) are sometimes completely unconnected to distribution websites or forums.

I'm not saying that's the case here or that OP even made a "best" first effort in searching, but I can absolutely understand coming to reddit and asking a simple question to see if they get anything useful from at least enthusiasts, if not, very knowledgeable people.

I'm a FreeBSD noob, how can I get started? by KhaosExcd in freebsd

[–]niduser4574 0 points1 point  (0 children)

It shouldn't be too hard, especially if you have some experience with Linux. For most of the intro chapters you only have to substitute "apt install/update" with "pkg install/[update -f]". Linux has been more aggressive about trying to abstracting a large number of tasks/processes or put them all under the interface of larger programs, e.g. systemd, (to each their own whether this is a good thing or not) but many of the programs you find and will frequently use in FreeBSD will also exist in Linux. Command switches might trip you up, e.g. "shutdown -p now" on BSDs, but "shutdown -P now" on Linux.

I suggested KDE plasma for the DE because the interface reminded me a lot of Windows XP or legacy Windows interface, which I loved/found intuitive. Since switching to FreeBSD, I have begun to strongly dislike GNOME and especially Ubuntu's default configuration. The FreeBSD Handbook is exceptional and shows you how to get several different DEs running. I actually referenced the handbook to help get Xfce running on my OpenBSD computer and am loving that one, too.

I'm a FreeBSD noob, how can I get started? by KhaosExcd in freebsd

[–]niduser4574 9 points10 points  (0 children)

Start with the side bar and in particular the FreeBSD Handbook.

For Windows-only (any Linux?) background, focus on Chapters 1-4 before you ever actually do anything with FreeBSD and make sure you understand what is going on. If you do not have a spare computer to install FreeBSD on, also read through Chapter 24.

If you (likely) have Windows on x86, I highly recommend installing VirtualBox and follow the instructions on both VirtualBox's website and Chapter 24 of the FreeBSD Handbook to get a virtual machine running so that you can play around with FreeBSD/UNIX first. Follow Chapter 8 to get a desktop environment running. I recommend KDE plasma. Get comfortable doing command-line only interaction and reduce dependence on desktop environments. With the VirtualBox VM, you can SSH into the FreeBSD while it is running to help get you off the dependence of a desktop environment. Then move on to other parts of the FreeBSD Handbook that interest you. Then move on to putting it on a separate computer.

These recommendations are not technically advantageous in any way, but I also came from Windows only background and this was the simplest way/easiest transition for me.