Jump to content

Trying to achieve generics without using pointers in C.

Gat Pelsinger

I am coding an array list in C. In my add function, it accepts a void pointer called element currently. The reason it is a void pointer is because I don't know what type is the element going to be. But if I do so, then when calling the function, I always have to first create a new variable and pass it's value by reference, which is not ideal. And if use a normal variable instead of a pointer, then I cannot use void and it has to be tied to a specific data type.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

Any reason you're not using a language that supports generics out of the box, like C++?

 

The whole idea behind generics is to have type safety. Which requires language support. So even if you find a workaround for your issue, it's going to be an ugly hack since you lose type information.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Using void pointers is the standard way to implement generics in C. If you really don't want to use void pointers, then you could use macros like shown here: https://stackoverflow.com/questions/16522341/pseudo-generics-in-c

 

However, this generally isn't recommended. C++ has support for generics built in and will be much less of a headache to use.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

@Eigenvektor @dcgreen2k

 

The reason I am using C is because I am just planning to actually learn and master it before hopping onto other languages. I am actually coding a custom array list for another program that I am programming, but I am keeping all the abstraction and independence so I can use it outside my program in other programs. So, can I code my array list in C++ and still make it compatible with the rest of my C program?

 

And to be honest, don't I just need 1 variable of that same type to be able use my Array List? For example when I first call my add function, I pass the value of say, char x[] = "12345" by reference. And the add function adds the element, and now doesn't even need my char x[]. So can I not use that same variable to change it's data and pass that to another call to the add function by reference?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Gat Pelsinger said:

So, can I code my array list in C++ and still make it compatible with the rest of my C program?

There are ways to mix C and C++, but you need to keep your code compatible with C, so "real" generics are out.

 

To make the code reusable, ideally you would turn that array list into a generic class (in C++) that can be instantiated and reused wherever needed (of course, if it wasn't for learning purposes you'd use std::list instead of rolling your own)

 

24 minutes ago, Gat Pelsinger said:

And to be honest, don't I just need 1 variable of that same type to be able use my Array List?

If your list creates a copy, then yes you can reuse your original variable. You just need to be careful if you're dealing with pointers. If the list stores a pointer instead and you change/reuse the value it is pointing to, then your list would now be pointing to the changed value instead.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×