1,400 questions
Advice
0
votes
11
replies
239
views
Casting functions with pointer arguments to function pointers with void* argument
The following code is accepted by GCC and the resulting binary prints the expected results. But is it standard conform and would always work on different systems using different compilers?
Minimal ...
1
vote
2
answers
176
views
Getting type of a pointer
I've created the function
void add_entry(char key[], void* value, char file[], ValueType type)
and I want to use my generic function to get the typeof the pointer
#define typeof(x) _Generic((x), \
...
3
votes
1
answer
77
views
Accessing any object type from multiprocessing shared_memory?
Suppose I create a shared memory object:
from multiprocessing import shared_memory
shm_a = shared_memory.SharedMemory(create=True, size=1024)
buffer = shm_a.buf
and put a generic object of a generic ...
1
vote
1
answer
79
views
Level of type casting of pointers?
I have an array of pointers to structs in C as
typedef struct{
int elem1;
char *elem2;
...
}example_t;
example_t *array[MAX_SIZE];
I did generic functions for add or delete pointers to ...
4
votes
1
answer
179
views
How to use buffer overflow to modify function pointer value?
I am currently trying to make a code more safe for a cybersecurity exercise. I was asked to make the flag contained in the secret_function() come out. The problem is that I can't modify the code and ...
3
votes
2
answers
125
views
How to avoid getting warning assignment of different pointer when passing function with specific type parameters?
I have written this example to try and explain the problem.
I have defined a function as follows:
// Return name from given user.
typedef char *(name)(void *user);
And then implemented this function ...
1
vote
1
answer
126
views
Generic Quad Tree implementation in C
Context
I've been trying to learn C and have become a little more comfortable in it recently. I am building a 2d space shooting game (kind of Galaga inspired, written using SDL) and decided to use ...
0
votes
1
answer
55
views
Using a function to allocate complex custom data structures
I am trying to allocate memory for certain data structures (array, matrix, custom_array, ...). The problem I think I am facing is that I am not able to cast the proper types to the passed pointers ...
0
votes
2
answers
121
views
Why do I get a SEGFAULT when I try to sort an array of integers using K & R code examples? [closed]
I'm trying to work my way through The C Programming Language, 2nd Edition, and I can't get my head around exactly what is happening in the following code.
The example from the book works just fine ...
1
vote
1
answer
497
views
How to fix "multi level implicit pointer conversion" warning in clang-tidy?
I have some legacy code that reads like this:
bool has_attribute(int kvidx) const {
void* v = nullptr;
int flag = 0;
MPI_Comm_get_attr(impl_, kvidx, &v, &flag);
return flag != ...
0
votes
5
answers
240
views
Disadvantages of using void * to hold generic objects?
I'm a C beginner, and I've got a question while I read the Chapter 19.5 of C Programming: A Modern Approach (2nd Edition) by K. N. King.
In this chapter, the author explains what is program design and ...
2
votes
3
answers
177
views
Why pass void pointer instead of custom type?
Setup
I'm taking over code from another person. I found a coding pattern that seems odd to me. This is for an embedded 32 bit PIC processor, in case that's relevant.
They have a custom typedef in a ...
0
votes
0
answers
50
views
Casting(?) void pointers [duplicate]
I'm using void pointers to handle data in arrays, and i was wondering if i could cast(?) the data in a way that doesn't pad the code.
For example i have a structure:
typedef struct Matrix{
int rows;
...
0
votes
0
answers
34
views
Void Typecasting - Middle of Structure
I'm attempting to write a program that takes data from main (member type structure), then passes it to (void *) within the function, placing it on the stack in a linked list. In order to use this ...
0
votes
0
answers
31
views
Passing Structure Directly to Void Pointer
I know that a void pointer is useful for setting up generic functions etc. In any case, I also understand that a void pointer can be used to directly point to things like integers etc. I suspect ...
3
votes
4
answers
132
views
Receiving a char array to a void pointer shows different behavior
I am attempting to develop a method to customize strcpy. I considered two approaches for this implementation. One method involves using memcpy, while the other involves incrementing the pointer and ...
1
vote
1
answer
126
views
Expression evaluation in printf with %p
I am studying pointers in C. Trying a bunch of things to make sure I understand how pointers work, I found something that I don't understand. Here's the snippet of the code in question:
int b = 30;
...
0
votes
1
answer
140
views
Passing and retrieving arbitrary data through a C void* from Fortran
I apologise in advance for the very long message.
I am seeking expertise in order to determine if what I am doing is safe/correct, or if alternatives exists.
I have a C library that can hold a user-...
0
votes
0
answers
86
views
How to make a Template Function in C
I'm writing a code for task which contain only two data structure(6 queues and 4 collections). But the problem is that I do not know exactly which data structures I will need until the moment of ...
0
votes
1
answer
82
views
How to make an instance in runtime, C
I need to create an instance of one of several data structures(for example: DynamicArray, BST, HashMap, Trie) on C language. The name of data structure read from file and then instance of data type ...
0
votes
1
answer
136
views
c programm work different ways but i didn't change it
I haved to realise dynamic array in c. Here is the struct of it:
typedef void (*PrintArrayElement)(const void* element);
typedef struct {
size_t size;
size_t capacity;
void** array;
...
2
votes
2
answers
154
views
Runtime cast of void pointer
I'm writing a C program that applies a different type of quantization to an array depending on an environmental variable.
The problem is that, I need to use the same function void foo(void* ptr, ...
0
votes
1
answer
112
views
Creation of generic less than function in C
I am looking to implement something similar to
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
For comparison of numerical types such as floats, doubles and integers
but with ...
0
votes
1
answer
93
views
Wrong generic less then function
I assumed that the following function can get any datatype such as float, double, long, and long long and return a correct answer. Unfortunately, it does not. Can anyone explain why? My aim is to ...
0
votes
1
answer
82
views
Appropriate use of `void **` for garbage-collected structs in C?
I need some help with void ** pointers.
In implementing a garbage collector in C (automatic reference counting), I've found I need a special assignment operator (similar to what C++ does with std::...
1
vote
1
answer
106
views
Is there a workaround for typecasting a function pointer to an object pointer in C?
I have a function called 'bench' which accepts a function pointer that returns a void pointer (for generics).
static inline unsigned long bench(void* (*funcPtr)(va_list), ...)
But if I pass it a ...
0
votes
0
answers
98
views
Unexpected behavior of c_void pointer in Rust FFI to C [duplicate]
I'm new to Rust and try to implement a foreign function interface which enables C code to interact with Rust structs. The functions save results within the structs for later use. I want to compile my ...
2
votes
4
answers
246
views
Casting a void pointer in struct
Here is what I got ..
typedef struct SysInfo_P
{
int some_P_int;
char some_P_char[10];
...
} SysInfo_P;
typedef struct SysInfo_S
{
int some_S_int;
char some_S_char[10];
...
} ...
-2
votes
1
answer
149
views
In what cases does the standard forbid taking pointers to functions/objects? [duplicate]
This article lists all the different categories of pointers. I have tested the explicit conversion of different types of pointers to const void* in the following snippet (live):
#include <print>
...
0
votes
0
answers
412
views
Member variable access in a C++ Zephyr RTOS Thread
Let me start by mentioning that I'm aware that my solution is neither thread safe nor clean at all... but that's why I'm writing this question:
I'm trying to implement a thread in a C++ class in ...
0
votes
1
answer
345
views
What does the usage of restrict mean in the man page for 'fwrite'?
I was looking a the man page for fwrite, and I was curious about the usage of restrict for the arguments as I have no seen this before.
size_t fwrite(const void ptr[restrict .size * .nmemb]...
what ...
0
votes
1
answer
112
views
SkipList segmentation fault when insert last element
I've developed a SkipList with void pointer, and probabilistic way to set the maximum level of it.
those are my structs:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
...
-1
votes
1
answer
260
views
What is the correct way to write pure virtual function without a specific return type?
I'm trying to write an abstract struct named Shape, and I need a function in derived classes that will be counting distance from a ray origin to my shape. I use functions from here, they return float, ...
1
vote
1
answer
279
views
Is it OK to cast an _Atomic Type * to void * and back?
Some libraries require a callback function takes an void *(not _Atomic void *). Is it OK to cast an _Atomic Type * to void * and cast back to use it? Like the following code:
#include <stdio.h>
...
-1
votes
1
answer
72
views
how to add pointers to functions (that have parameters) without parameters and call them later with custom parameters C++
I'm gonna try to keep this short. As much as I can.
I have a Spell class which is mostly duration, cooldown, damage, type of damage, etc..
I have SpellManager class which is supposed to instantiate ...
-2
votes
1
answer
219
views
Casting a void* pointer
i have a problem in my code when i try to use compelx structure in a generic sorting library on C lang: (you can find the complete code https://github.com/Tramontz/ASD_2023)
i have a binary-merge ...
-1
votes
1
answer
125
views
Union vs void pointer performance [closed]
TL;DR is union better in terms of performance than void pointer
When I was searching for Union vs void pointer performance I gone thorough this question Union versus void pointer.
Many suggested to ...
0
votes
0
answers
54
views
Calling a function pointed to by void(*vptr)() from an instance of that class
This is a public member in CSpellData.h:
void (CSpellData::* _vptr)();
It gets assigned a callback in the constructor, like this:
_vptr = GetCallbackFromID(spell_id);
where:
void (CSpellData::* ...
1
vote
3
answers
193
views
Import C function with void* parameter
I want to import a C function in Ada.
There is the C part :
void Register(const void *ctxt)
{
saved_ctxt = ctxt; // This is a global variable
}
void Send_Trace(const void *ctxt,
...
1
vote
1
answer
88
views
How can i fill and then display this void*** matrix in C?
Context: I need to create a board game with a 2D array using void***, in each slot i have to initially put a Struct "Tierra" and then in later turns i can put a Struct "Bomba"
The ...
0
votes
1
answer
88
views
assigning char* to a void* field
I need to make a program that reads file lines and saves them in a linked queue. I am using a library that uses a void* data field in each node.
When I try to print all the lines stored, the program ...
1
vote
1
answer
89
views
How to get the string from an unmanaged void* parameter?
[DllImport("SiUSBXp.dll", CharSet = CharSet.Auto, EntryPoint =
"SI_GetProductStringSafe")]
static extern SI_STATUS GetProductString(
int dwDeviceNum,
...
0
votes
1
answer
227
views
Is casting a pointer/address of object type T to void* and then back to the original type T always safe?
A lot of books say that it is safe and the ISO goes "A pointer to void may be converted to or from a pointer to any incomplete or object
type. A pointer to any incomplete or object type may be ...
2
votes
1
answer
227
views
Meaning of casting the address of a void pointer
Lately, while working in C, I was developing a generic function.
Here is the layout of structure:
typedef struct student_{
char name[32];
int roll_no;
unsigned int height;
} student_t;
...
0
votes
1
answer
393
views
Converting a void pointer to an unsigned long
I am working on a C program that takes in a void pointer that I know for a fact points to an integer but I cant figure out how to correctly cast it to such. For example, the following will produce a ...
-2
votes
2
answers
119
views
problem casting a void* to a (vector<void*>*)
I am currently working on a complex logic, where I have a map<string,void*> with these value:
string mystring[10] = {"key1","key2","keyN"};
for(string s: mystrings){...
0
votes
0
answers
58
views
How to use void pointers/typecasting for implementation of a doubly_linked_list class
I have an assignment in which I have to implement a doubly linked list ADT. I do not have issues with that, I created something like this:
class DoublyLinkedList{
struct Node{
int value;
...
0
votes
4
answers
222
views
Automatically set pointers to NULL after free
This post shows a way to use void**s to eliminate a category of bugs related to dangling pointers (use after free, double free, etc):
void freep(void **p) {
if (p) {
free(*p);
*p = ...
5
votes
2
answers
725
views
Casting a void pointer to a 2D String array pointer
I'm using a library which requires a function with a void* pointer as a parameter. I have a 2D string array and I want to pass that array through that parameter and extract it inside the function. I ...
2
votes
3
answers
147
views
Deallocating a data structure with a void* value
I'm a beginner in C programming, and I'm sure this question is done and solved, but I cannot find much of an answer looking around. In my program, I have this implementation of a BST (Binary Search ...