4,480 questions
0
votes
5
answers
147
views
If statement vs. function pointer (performance)
Consider the following:
while(running) {
if(logMode == true)
log();
bar();
}
VS
if (logMode == true)
func_ptr = log;
else
func_ptr = noop; //noop is empty function that ...
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
1
answer
41
views
Problems with "qualified-id in declaration before '(' token" and "expected type-specifier" errors [duplicate]
I need to build some template classes in C++17 but I'm getting the following error message:
When I do not define the macro __ALIAS__ I got an error on these lines:
using PF = one<_Return>::...
1
vote
3
answers
142
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
3
answers
179
views
Assign a non-static member function as a same class member variable, which is a function pointer
The example snippet:
#include <iostream>
#include <functional>
#include <vector>
// Simulated callback type
using CallbackType = void(*)(void*, uint32_t);
class DataProcessor{
...
2
votes
1
answer
63
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
1
answer
76
views
How can I alias std::make_shared?
I have a template wrapper ThreadSafeVar for convenient mutex-locking of variables, and I want to alias std::shared_ptr and std::make_shared so I can do e.g. ThreadSafePtr<int> a = ...
2
votes
2
answers
47
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:...
3
votes
3
answers
120
views
Function pointers point to identical address values? ...sometimes
I have an array of function pointers and I want to assign simple dummy functions.
The first thing that came to my mind was using lambda. That worked pretty well. The only thing that surprised me was ...
2
votes
1
answer
92
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 ...
3
votes
1
answer
217
views
How to create an array of pointers to functions with arbitrary arguments?
I need an array of pointers to functions with unspecified amount of arguments. The return value is defined and it's void.
I understand, that if the functions are going to be stored in an array of ...
0
votes
1
answer
100
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));
...
2
votes
2
answers
111
views
What is the correct way to assign function to a function pointer of a structure member
Can anyone please help me with how to pass a function to a function pointer inside struct in C?
I have a function as below
typedef struct LOAD_DRV_DATA
{
uint8_t LoadDrvId;
uint8_t dataLength;
...
1
vote
1
answer
61
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
3
answers
91
views
How to organise big array of structs with function references in it? [closed]
I am trying to make readable/maintanable file that contains big array of struct instances, which have function reference in it.
Example of application, where this can be used, is user line command ...