Questions tagged [void-pointers]
A void pointer (void *) in C and C++ is a pointer that points to a memory location with no specified type.
1,372
questions
0
votes
1
answer
53
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
83
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
201
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, ...
0
votes
1
answer
81
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
174
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 ...
0
votes
0
answers
7
views
fetch integer value from void pointer in cpp
i have below code use din one of project and value of members["Delay"] is modified in many places so at last just i wanted to print the value of members["Delay"] but i am ...
-1
votes
1
answer
83
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
42
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
83
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
47
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
66
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
49
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
90
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
86
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
134
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
102
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
47
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
133
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 = ...
2
votes
2
answers
313
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
102
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 ...
4
votes
3
answers
191
views
The validity of casting in the 'function pointer' version of K&R's qsort
This question is about the 'function pointer' version of qsort from K&R (2e), section 5.11 (p118-121). There are a number of places where I don't understand why or how the casts work, and I ...
18
votes
6
answers
5k
views
Is every null pointer constant a null pointer?
From the C17 draft (6.3.2.3 ¶3):
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.67) If a null pointer constant is ...
0
votes
3
answers
170
views
Why is a cast of uintptr_t used here?
Here it is, in the context of pthread.h and stdint.h:
struct arguments {
uint32_t threads;
uint32_t size;
};
void *run_v1(void *arg) {
uint32_t thread = (uintptr_t) arg;
for (uint32_t ...
1
vote
2
answers
78
views
void* pointer arithmetic, cannot swap float values
recently, i was curious on how qsort() sorts without passing specific type, thus i'm trying to reverse an array without knowing / caring it's type.
i was told you can do pointer arithmetic if you cast ...
2
votes
1
answer
198
views
What are the correct types for memset function?
On trying to recreate "memset" function I have error if I try to dereference a void pointer, or have a different type for the second argument than the first. To make the function work I had ...
0
votes
0
answers
84
views
esp-idf calling method from a thread passed by void pointer, incorrect class variable values
I have a class in my code which has a function that initializes some class variables. Another method in the class can print those variable values. One last method in the class contains a while loop to ...
0
votes
1
answer
133
views
Store void function pointer with arguments to be passed a called back
I'm working on a algorithm in C++ that sorts a list like linear structure without using any aid from any external data structure. My plan is to find the minimum value first and put it at the beginning ...
0
votes
1
answer
45
views
Is it possible to access void * pointers that point to different types when parsing through void *? [closed]
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *str = "hi";
char *str2 = "bye&...
-1
votes
1
answer
58
views
How to get a Memory Address (Pointer) from a Number Literal?
Background:
I have decided to work with neutral void pointers (void*)
in my programme because it is uniform, efficient and I can
cast them to whatever is needed in the context.
When calling a ...
0
votes
0
answers
15
views
Cannot modify the array passed as parameter [duplicate]
I have this function that has to read from a csv. It does but when i try to print the value in the main after read_csv without returning a void**, throw segv. Instead, if i return the array and print ...
1
vote
4
answers
168
views
Auto cast void pointer to pointer to double in function call
I have a question, and it might already be answered somewhere else, and I could not find it, so I apologize if it already exists.
I have the following code:
#include <stdio.h>
void foo(double *...
3
votes
1
answer
85
views
Why whould i call a pointer with no obvious function
In an example from Keil uVision for a STM32 Development Board i find the following function:
__NO_RETURN static void thrLED(void *argument) {
...
(void)argument;
...
}
What is the meaning of (...
-3
votes
1
answer
146
views
Is this conversion valid in C
As direct conversion to void ** of other types is not valid is this conversion valid in C?
void swapValues(void **av, void **bv, size_t size)
{
int *a = *av;
int *b = *bv;
char array[...
0
votes
0
answers
239
views
How can I use xTaskCreate in class and use its arguments in static task method
I'm working on a personnal project to learn about FreeRTOS. I have several esp32s2 with PSRAM and native USB.
I made a small example to go deeper in my problem: I can not manage to manipulate the ...
0
votes
1
answer
129
views
Why is the size of (void *) 8 bytes? [duplicate]
#include <stdio.h>
int main()
{
printf("%ld", sizeof(void *));
return 0;
}
The output for the program was 8, but I am unable to figure out how.
0
votes
2
answers
131
views
Is it legal to type cast a void** to a void*?
I've implementated something like this in my testing scripts recently, when messing with standard C, and I got curious about it. This is no homework, its just some hobby of my own.
#include <stdio....
0
votes
1
answer
116
views
Omitting void return type not working as void* argument
I am trying to pass a uintptr_t and a void* to a lambda using std::pair.
This is how my function looks like that accepts the uintptr_t and void* as pair:
void call( std::pair<std::uintptr_t, void*&...
0
votes
2
answers
406
views
Passing pointer to function to function and assign it in C
I have a program in which te type of function to be used has to be different according to a string (the mode). Suppose i want the functions in extfunc to be initiated according to the value i write ...
2
votes
3
answers
69
views
int pointer to void pointer to char array somehow?
I'm having trouble understanding the following code:
char arr[] = {0,1,2,3};
void *vp;
int *ip;
vp = arr;
ip = vp;
How is it possible that the int pointer ip can point to an element of arr if the ...
4
votes
2
answers
348
views
Is there a way to make a single function operate on different structures (having common members) in c
After passing a void* pointer as argument to a function, is there a way to specify the type to which it is cast as another parameter. If I have two structs like:
struct A{
int key;
char c;
}
...
0
votes
0
answers
60
views
Heap corruption with pthreads in C
I've been writing a C program to simulate the motion of n bodies under the influence of gravity. I have a working version that uses a single thread, and I'm attempting to write a version that uses ...
0
votes
0
answers
26
views
Rust bindgen use of pointer addresse `*mut i8` for allocating memory for camera, getting "cannot be sent between threads safely" [duplicate]
tl&dr: How to work with *mut i8 so it is thread safe for use in lazy_static! Mutex ?
I have a function that generates a pointer address in the form of *mut i8, which I want to store in a struct ...
0
votes
1
answer
54
views
Copying void-pointer element into void-pointer array causes multiple copies of element in array
I'm trying to code a TreeSort function in C89 for practice with a qsort parameters i.e. (void* base, size_t num, size_t size, int (*compare)(const void*, const void*)). I've implemented sort function, ...
0
votes
1
answer
136
views
invalid conversion from 'void*' to 'Guess*' [-fpermissive] from trying to realloc a structure
I am trying to program my own hangman game without refrencing other programs as a way to get back into programming.
Right now I am trying to program a structure that will hold all of the letters that ...
1
vote
1
answer
93
views
How can a void pointer hold data meant for non pointer variables? [duplicate]
I was practicing with void pointers when this code successfully compiled:
#include <stdio.h>
int main()
{
void *x = (void*) 576;
int *y = x;
printf("%d\n", y);
return ...
-2
votes
1
answer
143
views
Is `void *` a dynamic type?
I am always tempted to refer to void * as dynamic typing for C & C++, usually as a throwaway joke.
I could not find a wikipedia entry or a dictionary entry for Dynamic Type. Perhaps the term is ...
-1
votes
2
answers
96
views
Dereferencing a pointer at runtime in c
I am trying to dereferencing a pointer, according to the data-type passed to the function, just to learn.
code 1: First Try. Oviously it didn't work.
typedef enum
{
bits_8,
bits_16,
...
0
votes
2
answers
263
views
How to access the content of a void pointer inside a struct pointer?
I have a struct pointer, that points to a struct that has a void pointer as it member, and I need to access the value of this member.
This is the struct with the void pointer I need access
typedef ...
0
votes
1
answer
112
views
How to decide a std::vector Type in runtime depends on loop elements type?
I have a list of my own data types, I want to convert it to std::vector of primitive type (int8_t/int/...).
I want to create a vector of primitive type (int32/int8) that will be choosen in runtime ...