pointer to function wont work - c++

I'm trying to learn how to use pointer to function properly. I was supposed to make a pointer to the function strcmp, inside the function check, but the program opens and closes immediately. As far as I understood, the pointer to function is correct on my code returnType (*pointer)(parameters)); So what is going wrong? Thanks in advance.
void check(char *a,char *b,int (*cmp)(const char*,const char*))
{
printf("Testing equality\n");
if(!(*cmp)(a,b)) printf("equals");
else printf("different");
}
int main(void)
{
char s1[80] = "daniel" ,s2[80] = "daniel";
int (*p)(const char*,const char*);
p = strcmp();
check(s1,s2,p);
return 0;
}

This line is incorrect:
p = strcmp();
Here you're calling strcmp with zero arguments, which is invalid. You should have gotten a very clear compiler error about this. For instance, gcc gives me:
error: too few arguments to function ‘int strcmp(const char*, const char*)’
You want to just assign strcmp:
p = strcmp;
Also, you don't need to dereference function pointers to call them, so !(*cmp)(a,b) could be simplified to !cmp(a,b).

Related

C vs. C++, handling of void** pointers

I found out that using a C compiler the code below works but not with a C++ compiler. I understand that casting to void** is the correct usage but I can't understand why it compiles with the C compiler even if I use the void* (commented out).
#include <stdio.h>
int fn(void **arg)
{
int *pvalue = *(int**)arg;
*pvalue = 200;
return 0;
}
int main()
{
int value = 99;
int *pvalue = &value;
// fn((void *)&pvalue); // works only in C
// error C2664: 'int fn(void **)': cannot convert argument 1 from 'void *' to 'void **'
fn((void **)&pvalue); // correct, works for both C/C++
printf("%d", value);
return 0;
}
Can someone explain why this is the case?
In C there is allowed to assign a pointer of the type void * to a pointer of other type. This takes place in this call
fn((void *)&pvalue)
where the argument has the type void * that is assigned to the function parameter that has the type void **.
int fn(void **arg)
{
int *pvalue = *(int**)arg;
*pvalue = 200;
return 0;
}
However such an assignment in general is unsafe. For example the value of a pointer of the type void * can not be properly aligned to be assigned to a pointer of other type.
So it was decided to not allow such an assignment in C++ to make programs more safer.
I can't understand why it compiles with the C compiler even if I use the void* (commented out).
It compiles because void* is implicitly convertible to other pointers in C.
fn((void **)&pvalue); // correct, works for both C/C++
This may be well-formed because of the cast, the standard doesn't technically give explicit guarantee that conversion to void** and back yields the same address.
While this may be likely to work in practice, there is no reason to not use void* as the function argument instead, which does have the guarantee. As a bonus, you won't need the cast in the call. Like this:
int fn(void *arg);
fn(&pvalue); // correct, works for both C/C++
Of course, this is assuming type erasure is needed in the first place. Avoid void* when it is not needed.
For avoidance of doubt, there is nothing correct in
fn((void **)&pvalue);
It is just as incorrect as
fn((void *)&pvalue);
The correct way to use the API is to do
int fn(void **arg)
{
int *pvalue = (int *)*arg;
*(int *)pvalue = 200;
return 0;
}
or
int fn(void **arg)
{
*(int *)*arg = 200;
return 0;
}
with
int main()
{
int value = 99;
void *pvalue = (void*)&value;
fn(&pvalue);
printf("%d", value);
return 0;
}
You're not allowed to access an object using any other pointer type, other than the declared type, compatible type, or a character type. Furthermore, while void * is used as a generic pointer type to all sorts of objects in C, there is no generic pointer to a pointer type in C - other than void *!
And this is the reason why the void ** is almost always a sign of a design error in APIs - most usages are just wrong.

error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]

Getting error:
error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
on g++
on the following code:
void* func(void *s)
{
int i = 0;
int self = (int *)s;
printf("Thread Entered: %d\n", self);
sm.lock(self);
// Critical section (Only one thread
// can enter here at a time)
for (i=0; i<MAX; i++)
ans++;
sm.unlock(self);
}
You need to change int self = (int *)s; to int self = *((int *)s); or to int * self = (int *)s;.
You need to think of these as two different things. One is a pointer to memory where the value is stored (int*) and the other is an actual value (int).
Looking at your function declaration void* func(void *s), your s parameter is of type void, this will need to be int if you wish to convert it.
Your data types seem a bit mixed, which just doesn't fly do well in C/C++. Can you clean it? If you are using this function with pthread_create(), as per the documentation with example for this function, try..
// your pthread_create call..
int SOME_INT = 123;
s = pthread_create(&thread_id, &attr, &thread_start, &SOME_INT);
//...and your function
void* func(void *s)
{
int self = (int*) s;
Pointers can be confusing. See if that above code looks similar, in particular to passing the last parameter for pthread_create as a pointer reference. And then try your original code. It might just be that it wasn't passed as a reference.
See what that yields you, else try storing it as a pointer and then converting on use.
void* func(void *s)
{
int *self = s;
sm.lock(*self); // but can give a potential race condition.
In the second line of the function body, I suspect you were trying obtain the value at s (as an int pointer), but you actually cast it directly to an int. That would yield the address in s rather than the value stored there.

Failure to pass pointer to const data as function template argument

I have a function which takes a function pointer as an argument, and then calls that function with its own arguments:
typedef int (*my_func_ptr)( int );
int foo( my_func_ptr f ) {
static int i = 0;
return i = f( i );
}
Sometimes, I need to pass functions to foo that depend on more than just integer input to spit out a result.
int add_strlen( int i, const char* s ) {
return i + strlen( s );
}
I could rework the above code to make use of std::function and then use std::bind, but it is preferable to me that these functions be created at compile time, so I'm using templates.
template<const char* S>
int add_strlen( int i ) {
return i + strlen( S );
}
/**
* Usage:
* char bar[] = "bar";
* foo( add_strlen<bar> );
*/
My problem arises when using pointers as template arguments. Whenever I use a pointer to constant data of any type as a template argument, it only manages to compile if the argument being passed is declared as a non-const array of that type.
char char_array[] = "works";
const char const_char_array[] = "error";
char *char_ptr = "error";
const char *const_char_ptr = "error";
The relevant error in Clang (ver. 3.0-6) (errors for char_ptr and const_char_ptr are the same):
func_ptr.cpp:29:9: error: no matching function for call to 'foo'
foo( add_strlen<const_char_array> );
^~~
func_ptr.cpp:6:5: note: candidate function not viable: no overload of 'add_strlen' matching 'my_func_ptr' (aka 'int (*)(int)') for 1st argument
int foo( my_func_ptr f )
Can anyone explain to me why this is? The way I see it, template parameter S is expected to be of type const char*, which in any other circumstance means I can pass in any const or non-const pointer or array of type char and expect it to work. I would like to be able to declare my arrays as const, because I don't want to even imply that they are meant to be modified at runtime. Is there any way to keep my arrays const and use them as template arguments?
Edit: Thanks to some help (and a newer version of Clang with better errors) I was able to determine that supplying a template argument with internal linkage is part of the problem. By declaring the above variables as extern, I am able to use add_strlen<const_char_array> without error. I've also created a simplified test case. It is included below:
#include <cstring>
typedef int (*my_func_ptr)( int );
int foo( my_func_ptr f ) {
static int i = 0;
return i = f( i );
}
template<const char* S>
int add_strlen( int i ) {
return i + strlen( S );
}
extern char char_array[];
extern const char const_char_array[];
extern char *char_ptr;
extern const char *const_char_ptr;
char char_array[] = "foo";
const char const_char_array[] = "bar";
// assigning to string literal is deprecated
char *char_ptr = char_array;
const char *const_char_ptr = "baz";
int main(int argc, const char *argv[])
{
foo( add_strlen<char_array> ); // works
foo( add_strlen<const_char_array> ); // works
//foo( add_strlen<char_ptr> ); // doesn't work
//foo( add_strlen<const_char_ptr> ); // doesn't work
return 0;
}
The error seems to be related to what you are and what you are not allowed to use as non-type template parameters, referring to IBM Linux Compilers documentation for Non-type template parameters they have this to say:
The syntax of a non-type template parameter is the same as a declaration of one of the following types:
integral or enumeration
pointer to object or pointer to function
reference to object or reference to function
pointer to member
The reason why char_array[] and const_char_array[] work when passed in is because they are constant at compile time and will never change underneath the program while it is running. Integral types can be passed in, pointers to integral types however can not be passed in.
The template is expecting a type of const char * a.k.a const char[x], but it is also expecting something that will never change, so the location where the pointer is pointing may never change. When passed in at compiler time your const_char_array it is being passed a char[6] ("error"). The location will never change and the contents will never change. However when passing in the const_char_ptr it is getting a const char *, while the pointer itself may never change, it is entirely possible the location where it points may change. It itself is not static.
char *_arr = new char[20];
const char* _ptr_arr = _arr;
We can agree here that my _ptr_arr is the exact same type as your const_char_ptr, yet the location where the contents are stored may change at run-time. In templates that isn't allowed since it may require a whole new instantiation of the template, and is non-deterministic from when templates are created. A char [6] is static and won't change.
foo( add_strlen<_ptr_arr> );
results in the following compiler error:
test.cpp:36:5: error: no matching function for call to 'foo'
foo( add_strlen<_ptr_arr>);
^~~
test.cpp:6:5: note: candidate function not viable: no overload of 'add_strlen' matching 'my_func_ptr' (aka 'int (*)(int)') for 1st argument
int foo( my_func_ptr f ) {
^
Which is not very helpful, we want to figure out why there is no valid overload, compiling the code with the function stand-alone without being passed as a function pointer we get the following:
add_strlen<_ptr_arr>(0);
will result in:
test.cpp:36:5: error: no matching function for call to 'add_strlen'
add_strlen<_ptr_arr>(0);
^~~~~~~~~~~~~~~~~~~~
test.cpp:16:5: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'S'
int add_strlen( int i ) {
^
So the explicitly-specified argument is invalid, specifically, we can't pass in an pointer to an integral.

Cast integer array to void pointer - pthread_create

for passing arguments, pthread requires void pointer. I want to pass two variables, int and long. To save myself some trouble, I will pass two long instead. So this is what I do:
int main(int argc, char *argv[])
{
/*SOCKET INIT*/
pthread_t socketTh;
long data[2] = {ip2l(IP),80};
pthread_create(&socketTh, 0, &serverConnect, (void*)data); //ERROR HERE
while(true) {
/*event loop*/
}
quit_thread(&sockerTh); //just an EXAMPLE! I don't know how to quit threads, I will google it later
return 0;
}
Error:
error: invalid conversion from 'void (*)(void*)' to 'void* (*)(void*)'
But it seems that this is not the correct way to retype long pointer to void pointer. I wonder not of threads Stackoverflow has suggested me didn't help, as well as any Google search. So maybe, try to explain it really slowly, because I'm beginner and I'm still lost in data types.
Thank you.
Update:
I create the unsigned long* in main(). This means this variable will last as long as the program itself.
The error message refers to the third argument. pthread_create wants a pointer to a function that takes a void* and returns void*. Apparently the function serverConnect is a function that takes a void* and returns void.

Trying to use c++ qsort with an inline function

I am trying to create an inline function for a comparer to qsort - something like this:
function<int(int,int)> comparesort = [smarkers, emarkers, strSearch] (int *arg1, int *arg2) { return 0; };
qsort(sortptrs, nKeywords, sizeof(int), comparesort);
It's giving me this error
IntelliSense: no suitable conversion function from "std::tr1::function" to "int (__cdecl *)(const void *, const void *)" exists
ok - I changed it to this
auto comparesort = [sortptrs, smarkers, emarkers, strSearch] (int arg1, int arg2)
{
int a = 0;
.
.
.
return a;
};
std::sort(sortptrs, sortptrs + nKeywords, comparesort);
and it's giving an error:
error C3499: a lambda that has been specified to have a void return type cannot return a value
[edit on 7/30 3:55 pm]
I actually needed a pointer sort - I've got an array of start and end bytes of words (found in a string passed in from VB.Net managed code). I also have a pointer-array that contains "1,2,3..." and I needed to sort the pointer.
Didn't seem I could do that with std::sort so I implemented my own shell sort...
The signature of qsort takes a function pointer of type int(*)(const void*, const void*). You are trying to give it a function<int(int, int)>, which is not a function pointer, but an object encapsulating something (might be a function ptr, might be a functor) which is callable as int(int, int) (note that it wouldn't have the right signature, even if it was a function pointer).
qsort is a basically a legacy function for backward compatibility with c.
In c++ I would strongly suggest forgetting about it and using std::sort instead:
auto comparesort = [smarkers, emarkers, strSearch] (const int& arg1, const int& arg2) { return false; };
//directly store the lambda, avoiding the overhead of creating a `function<...>`
std::sort(sortptrs, sortptrs + nKeywords, comparesort);
qsort is the old C API that takes a function pointer directly. You can't use it with anything else such as lambdas. Instead, use std::sort.