4,482 questions
1
vote
1
answer
57
views
Call function by reference in PowerShell [duplicate]
Suppose I have a function like this. How can I get a reference to that function and then call it?
function Hello($name) {
Write-Output "Hello, $name"
}
1
vote
1
answer
127
views
Modernizing typedef for a function to std::function [duplicate]
I have legacy code which uses typedef for function pointers (and yes, void* as well). I reduced it to this:
#include <memory>
#include <functional>
typedef void (*funx_t)(const double *, ...
4
votes
1
answer
165
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 ...
30
votes
6
answers
5k
views
Why does K&R say that pointers are preferable to arrays as function parameters?
From K&R page 99:
As formal parameters in a function definition,
char s[];
and
char *s;
are equivalent; we prefer the latter because it says more explicitly that the variable is a pointer.
I ...
1
vote
3
answers
178
views
How to avoid lambda capture in loop
Let's say I want to store a collection of function pointers (from lambdas), where each functions body is very similar. Writing everything out by hand gets repetitive and hard to manage, so instead I ...
2
votes
1
answer
91
views
Why does this function pointer typedef behave differently when used with const?
#include <stdio.h>
typedef int (*func_t)(int);
int foo(int x) {
return x * 2;
}
int main() {
const func_t ptr = foo; // Works
//const func_t *ptr = &foo; // Fails: why?
...
2
votes
2
answers
64
views
Custom memory handling in postGIS
I am currently working on a c++ project leveraging the features of Postgis.
Postgis provides handles to use custom memory allocators for the memory management.
The allocator should be of the signature:...
2
votes
1
answer
106
views
How to Implement Universal Setter/Getter Functions for Interrupt-Driven Variables in Embedded C?
I had an interrupt functionality implementation in my embedded project with global variables. The interrupt checks PWM signal duty cycle and sets the value of a bool var to true if the value is ...
0
votes
1
answer
106
views
C struct pointers [duplicate]
#include <stdio.h>
#include <stdlib.h>
typedef struct node* Node;
struct node{
int a;
struct node* next;
};
void insertAtBeginning(Node head){
Node temp = malloc(sizeof(struct node));
...
1
vote
1
answer
63
views
GDB different function names and addresses than expected
I'm trying to capture when a child executes an abort.
The following is a MCVE that should give an idea of what I'm trying to do.
#include <iostream>
#include <cstdio>
#include <iomanip&...
1
vote
2
answers
71
views
How to set values of matrices with the help of methods in c?
Ive been trying to set the values of matrices with the help of methods that take pointers, But I am unable to do this. The setMatrixValue method stops working when the loop reaches the second row. The ...
3
votes
1
answer
97
views
C++Niche Syntax: Function Reference Type Declaration: const reference?
This is a niche question, but I'm struggling to find a sufficient answer.
Struct members can be const, but can a function pointer/reference member be declared const? Based off my reading of C++17 ...
1
vote
0
answers
104
views
How to specify (declval?) a function pointer argument to a method in a concept 'requires' block?
I have a scheduler class template that I'd like to use with one of several timer classes. I'd like to use a concept to verify a given timer class has what the scheduler needs.
// One of several that ...
2
votes
4
answers
208
views
Is it well defined to cast generic function pointers to specifc signatures with void* params pointing to different types
EDIT2:
It seems that the standard is not exactly clear, and appears to at least partially contradict itself in different parts on the usage of void* or struct* when casting function pointers.
The lack ...
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 ...
1
vote
2
answers
150
views
Any way to use a function pointer without declaring function elsewhere
I want to declare a function while setting the value of a function pointer.
A TypeScript equivalent:
let fp: () => void;
fp = () => {
console.log("Hello from inline function declaration!&...
1
vote
0
answers
55
views
Assign member function to base class function pointer [duplicate]
I have to implement a low-level interface for the library which exposes structure with a function pointers like this:
struct Interface {
int (*IterfaceFn) (int value);
};
Library then expects a ...
0
votes
5
answers
238
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
4
answers
175
views
How to place a function pointer in a c struct whereas the input parameter of the function pointer is the struct itself (i.e. "this" pointer in c)
I am trying to use a function pointer inside a struct, whereas the function pointer needs an instance of the struct itself as argument. My current code snippet looks like this:
#include "stdio.h&...
0
votes
1
answer
240
views
Can I get the device-side pointer to a CUDA kernel using its mangled symbol name?
Note: In case the post seems long, one can directly jump to the section starting with "I was wondering.." at the end, in case one wants to skip the buildup/context.
Buildup/Context:
For the ...
3
votes
1
answer
307
views
gcc unexpected C static_assert error: expression in static assertion is not constant
I have some C code which builds OK with gcc-11 and earlier but not gcc-12 and later.
This illustrates the problem (this isn't the actual code I'm working on):
#include <stdint.h>
#include <...
0
votes
1
answer
125
views
LLVM kCFI sanitizer with function of volatile arguments
Here is the minimal reproducible example:
// my_func.h
typedef volatile struct {
int a;
} my_vdata_t;
typedef struct {
int a;
} my_data_t;
extern void (*vfunc)(my_vdata_t* data);
extern void (*...
1
vote
2
answers
100
views
how to pass pointer to a method to a function which takes pointer to a function? [duplicate]
I have a class:
#include "pico/stdlib.h"
#include "hardware/gpio.h"
class ButtonsController
{
public:
ButtonsController(){};
void UpdateButtons(bool first_enable, bool ...
-3
votes
2
answers
140
views
C compilation error "called object is not a function or function pointer" [closed]
I got a snippet of C code from online, and I am trying to compile it. I am getting the following error.
error: expected declaration specifiers or '...' before ')' token
6 | extern FUNC(void, ...
-4
votes
4
answers
376
views
when you do pass by address, do you not need to 'always' pass a pointer?
In C language there are 2 ways to pass arguments to functions, right? One of them is Pass by address. Do you not need to always pass a pointer?
I haven't tried anything yet just wanted to know its ...