Why do I get garbage values from a structure initialized from void** - c

I test this code:
void foo (void* data[])
{
static struct structure
{
char character;
int integer;
} st1 = *((struct structure *)data);
printf("[%i][%c]", st1.integer, st1.character);
}
int main (void)
{
void* *data = calloc(2, sizeof(void* ));
char arg1 = 'b';
int arg2 = 20;
data[0] = (char* )&arg1;
data[1] = (int* ) &arg2;
foo(data);
return(0);
}
but I get garbage values instead of the intended
[20] [b]
I have tried many things, including st1 = *((struct structure *)&data);, (despite that AFAIK data is already an address) declaring and initializing separate structure pointer and then deref-ing it, but nothing works.
What could be the reason this happens and how should I do it properly approaching the same idea of obtaining function arguments ?

You generally and portably can't expect your struct to be the same size as two void*.
You can't expect any specific padding inside the struct itself. And you can't expect that casting a char to void* will produce the same valid char back when the memory region is reinterpreted as part of a struct.
The behavior of (the code I think I read) is as undefined as it can get.
I originally completely overlooked the fact that you assign addresses to the two void* (imagined you cast the char and int themselves to void*), so your reinterpretation of those values need to not result in what you expect at all. In fact, it may result in trap values. So your code is undefiend behavior.

Your problem is located in your structure itself.
In your data, in main, you wrote an int* and a char*. However, in your function, you define the struct as it contains a char and int, what you should write is :
void foo (void* data[])
{
static struct structure
{
char* character;
int* integer;
} st1 = *((struct structure *)data);
printf("[%i][%c]", *(st1.integer), *(st1.character));
}

Related

Passing char array to const void* param

I have a structure like this:
typedef struct {
float batch;
float length;
uint32_t test;
char val[0];
}
PARAM;
And am calling a function with this format:
void fillValues(float val, float len, uint32_t tmp, const void* prw);
I declare the structure and allocate some memory:
PARAM *first = calloc(10, sizeof(PARAM));
The problem I am having is if I print the values of first->data before calling the function, it contains as expected but if I call the function:
fillValues (test, first->batch, first->length,
first->test, &first->val);
and try and print prw inside, it contains nothing.
I think I am passing the value incorrectly because it is declared as a char array but the function parameter is a const void *.
P.S. I don't want to change the fucntion param type.
The only part where prw is used in fillValues is converting to a double:
double *value
value = (double*)first->data;
*value = *(const double*)pwr;
char val[0]; is nonsense, this is not valid C. Arrays cannot have size zero. The GCC compiler had a non-standard language extension that allowed this (as a 1990s fix to the old "struct hack"). This all turned obsolete in the year 1999 with the C99 standard, when flexible array members were added to the language.
Change your struct to this:
typedef struct {
float batch;
float length;
uint32_t test;
char val []; // flexible array member
}
PARAM;
Then allocate memory as
PARAM* p = malloc(sizeof(PARAM) + sizeof(n));
where n is the length you want the array char val[] to have.
Since your function has the format const void* prw, the last parameter in the call should be first->val and not &first->val.
This is a perfect example of why void pointers should be avoided when possible: they tend to hide bugs and block compiler warnings.

Clarifications about pointers use

I have a question for the exact meaning of a pointers phrase.
I have the following method:
myFunc(const void * p)
{
...
}
And it is being called by:
myStruct *st;
...
myFunc((char **)&st->arr);
I have experience with pointers, but in this case I still get lost with all these pointers and casting..
Can I get please an accurate explanation about how this case works?
Thanks
This seams to be bad quality code! Maybe not dangerous, as const appears in prototype.
myFunc(const void * p) accepts a pointer to anything and const should mean it won't touch it.
Now, st is a pointer to myStruct, so st->arr is the value of arr member and &st->arr is memory address of arr member. Assuming arr is an array, st->arr value is already a pointer.
So, (char**) is possibly the correct type of &st->arr, it is a pointer to a character array. And if it is the correct type, there is no need to cast!
You cast when you need to tell the compiler to handle your data as another data. It would make sense, in this case myFunc((const void *)&st->arr);
Anyway, without further information on myFunc, I belive that true programmer intention was myFunc((const void *) st->arr);
the address of st->arr is being converted to a char** (i.e. the address where to find a pointer to a char). myFunc accepts pretty much anything (void*) and you must be careful to pass it something it knows how to handle.
Internally, myFunc can do something like this:
struct myStruct{
char* arr;
};
void myFunc(const void* ptr){
char** cPtr = (char**) ptr;
(*cPtr)[1] = ...;
}
int main()
{
struct myStruct s;
...
myFunc((char **)&s.arr);
...
}
However, you should notice that this is extremely bad (as in "dangerouse") code.
You are calling the method myFunc with a double pointer char ** and the same you are catching with the const void *ptr . In C a void pointer can hold the other pointer types like(char */float */int */...), later on we can typecast them when we are using.
So here a void pointer can able to hold a double character pointer and you can typecast the void pointer to a double character pointer later on like
char **tempPtr = (char **)p;
Here const qualifier makes the pointer p as a constant data pointer, which means inside the function it wont change the data it is pointing to.

How do I correctly use a void pointer in C?

Can someone explain why I do not get the value of the variable, but its memory instead?
I need to use void* to point to "unsigned short" values.
As I understand void pointers, their size is unknown and their type is unknown.
Once initialize them however, they are known, right?
Why does my printf statement print the wrong value?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void func(int a, void *res){
res = &a;
printf("res = %d\n", *(int*)res);
int b;
b = * (int *) res;
printf("b =%d\n", b);
}
int main (int argc, char* argv[])
{
//trial 1
int a = 30;
void *res = (int *)a;
func(a, res);
printf("result = %d\n", (int)res);
//trial 2
unsigned short i = 90;
res = &i;
func(i, res);
printf("result = %d\n", (unsigned short)res);
return 0;
}
The output I get:
res = 30
b =30
result = 30
res = 90
b =90
result = 44974
One thing to keep in mind: C does not guarantee that int will be big enough to hold a pointer (including void*). That cast is not a portable thing/good idea. Use %p to printf a pointer.
Likewise, you're doing a "bad cast" here: void* res = (int*) a is telling the compiler: "I am sure that the value of a is a valid int*, so you should treat it as such." Unless you actually know for a fact that there is an int stored at memory address 30, this is wrong.
Fortunately, you immediately overwrite res with the address of the other a. (You have two vars named a and two named res, the ones in main and the ones in func. The ones in func are copies of the value of the one in main, when you call it there.) Generally speaking, overwriting the value of a parameter to a function is "bad form," but it is technically legal. Personally, I recommend declaring all of your functions' parameters as const 99% of the time (e.g. void func (const int a, const void* res))
Then, you cast res to an unsigned short. I don't think anybody's still running on a 16-bit address-space CPU (well, your Apple II, maybe), so that will definitely corrupt the value of res by truncating it.
In general, in C, typecasts are dangerous. You're overruling the compiler's type system, and saying: "look here, Mr Compiler, I'm the programmer, and I know better than you what I have here. So, you just be quiet and make this happen." Casting from a pointer to a non-pointer type is almost universally wrong. Casting between pointer types is more often wrong than not.
I'd suggest checking out some of the "Related" links down this page to find a good overview of how C types an pointers work, in general. Sometimes it takes reading over a few to really get a grasp on how this stuff goes together.
(unsigned short)res
is a cast on a pointer, res is a memory address, by casting it to an unsigned short, you get the address value as an unsigned short instead of hexadecimal value, to be sure that you are going to get a correct value you can print
*(unsigned short*)res
The first cast (unsigned short*)res makes a cast on void* pointer to a pointer on unsigned short. You can then extract the value inside the memory address res is pointing to by dereferencing it using the *
If you have a void pointer ptr that you know points to an int, in order to access to that int write:
int i = *(int*)ptr;
That is, first cast it to a pointer-to-int with cast operator (int*) and then dereference it to get the pointed-to value.
You are casting the pointer directly to a value type, and although the compiler will happily do it, that's not probably what you want.
A void pointer is used in C as a kind of generic pointer. A void pointer variable can be used to contain the address of any variable type. The problem with a void pointer is once you have assigned an address to the pointer, the information about the type of variable is no longer available for the compiler to check against.
In general, void pointers should be avoided since the type of the variable whose address is in the void pointer is no longer available to the compiler. On the other hand, there are cases where a void pointer is very handy. However it is up to the programmer to know the type of variable whose address is in the void pointer variable and to use it properly.
Much of older C source has C style casts between type pointers and void pointers. This is not necessary with modern compilers and should be avoided.
The size of a void pointer variable is known. What is not known is the size of the variable whose pointer is in the void pointer variable. For instance here are some source examples.
// create several different kinds of variables
int iValue;
char aszString[6];
float fValue;
int *pIvalue = &iValue;
void *pVoid = 0;
int iSize = sizeof(*pIvalue); // get size of what int pointer points to, an int
int vSize = sizeof(*pVoid); // compile error, size of what void pointer points to is unknown
int vSizeVar = sizeof(pVoid); // compiles fine size of void pointer is known
pVoid = &iValue; // put the address of iValue into the void pointer variable
pVoid = &aszString[0]; // put the address of char string into the void pointer variable
pVoid = &fValue; // put the address of float into the void pointer variable
pIvalue = &fValue; // compiler error, address of float into int pointer not allowed
One way that void pointers have been used is by having several different types of structs which are provided as an argument for a function, typically some kind of a dispatching function. Since the interface for the function allows for different pointer types, a void pointer must be used in the argument list. Then the type of variable pointed to is determined by either an additional argument or inspecting the variable pointed to. An example of that type of use of a function would be something like the following. In this case we include an indicator as to the type of the struct in the first member of the various permutations of the struct. As long as all structs that are used with this function have as their first member an int indicating the type of struct, this will work.
struct struct_1 {
int iClass; // struct type indicator. must always be first member of struct
int iValue;
};
struct struct_2 {
int iClass; // struct type indicator. must always be first member of struct
float fValue;
};
void func2 (void *pStruct)
{
struct struct_1 *pStruct_1 = pStruct;
struct struct_2 *pStruct_2 = pStruct;
switch (pStruct_1->iClass) // this works because a struct is a kind of template or pattern for a memory location
{
case 1:
// do things with pStruct_1
break;
case 2:
// do things with pStruct_2
break;
default:
break;
}
}
void xfunc (void)
{
struct struct_1 myStruct_1 = {1, 37};
struct struct_2 myStruct_2 = {2, 755.37f};
func2 (&myStruct_1);
func2 (&myStruct_2);
}
Something like the above has a number of software design problems with the coupling and cohesion so unless you have good reasons for using this approach, it is better to rethink your design. However the C programming language allows you to do this.
There are some cases where the void pointer is necessary. For instance the malloc() function which allocates memory returns a void pointer containing the address of the area that has been allocated (or NULL if the allocation failed). The void pointer in this case allows for a single malloc() function that can return the address of memory for any type of variable. The following shows use of malloc() with various variable types.
void yfunc (void)
{
int *pIvalue = malloc(sizeof(int));
char *paszStr = malloc(sizeof(char)*32);
struct struct_1 *pStruct_1 = malloc (sizeof(*pStruct_1));
struct struct_2 *pStruct_2Array = malloc (sizeof(*pStruct_2Array)*21);
pStruct_1->iClass = 1; pStruct_1->iValue = 23;
func2(pStruct_1); // pStruct_1 is already a pointer so address of is not used
{
int i;
for (i = 0; i < 21; i++) {
pStruct_2Array[i].iClass = 2;
pStruct_2Array[i].fValue = 123.33f;
func2 (&pStruct_2Array[i]); // address of particular array element. could also use func2 (pStruct_2Array + i)
}
}
free(pStruct_1);
free(pStruct_2Array); // free the entire array which was allocated with single malloc()
free(pIvalue);
free(paszStr);
}
If what you want to do is pass the variable a by name and use it, try something like:
void func(int* src)
{
printf( "%d\n", *src );
}
If you get a void* from a library function, and you know its actual type, you should immediately store it in a variable of the right type:
int *ap = calloc( 1, sizeof(int) );
There are a few situations in which you must receive a parameter by reference as a void* and then cast it. The one I’ve run into most often in the real world is a thread procedure. So, you might write something like:
#include <stddef.h>
#include <stdio.h>
#include <pthread.h>
void* thread_proc( void* arg )
{
const int a = *(int*)arg;
/** Alternatively, with no explicit casts:
* const int* const p = arg;
* const int a = *p;
*/
printf( "Daughter thread: %d\n", a );
fflush(stdout); /* If more than one thread outputs, should be atomic. */
return NULL;
}
int main(void)
{
int a = 1;
const pthread_t tid = pthread_create( thread_proc, &a );
pthread_join(tid, NULL);
return EXIT_SUCCESS;
}
If you want to live dangerously, you could pass a uintptr_t value cast to void* and cast it back, but beware of trap representations.
printf("result = %d\n", (int)res); is printing the value of res (a pointer) as a number.
Remember that a pointer is an address in memory, so this will print some random looking 32bit number.
If you wanted to print the value stored at that address then you need (int)*res - although the (int) is unnecessary.
edit: if you want to print the value (ie address) of a pointer then you should use %p it's essentially the same but formats it better and understands if the size of an int and a poitner are different on your platform
void *res = (int *)a;
a is a int but not a ptr, maybe it should be:
void *res = &a;
The size of a void pointer is known; it's the size of an address, so the same size as any other pointer. You are freely converting between an integer and a pointer, and that's dangerous. If you mean to take the address of the variable a, you need to convert its address to a void * with (void *)&a.

C Double Pointer to Structure

I am trying to work out a double pointer to a structure in C and cannot figure out what is going wrong... The simple source is below:
typedef struct
{
int member;
} mystruct;
void myfunc(mystruct **data)
{
(*data)->member = 1;
}
void main(int argc, char *argv[])
{
mystruct **data;
myfunc(data);
printf("member = %d\n", (*data)->member);
}
A similar question was asked here: How to work with pointer to pointer to structure in C? on how to modify a member of a structure through a double pointer. The solution was the syntax (*data)->member = 1; which makes sense. But in my little application here, I receive a seg fault when executing that line. What am I doing wrong?
Thanks
You need to point to something if you are going to dereference a pointer. Try this:
void main(int argc, char *argv)
{
mystruct actualThing;
mystruct *pointer = &actualThing;
mystruct **data = &pointer;
myfunc(data);
printf("Member: %d", (*data)->member);
}
You received a segfault because you did not allocate a struct.
The value of data is garbage, so it is pointing to some place in memory that is not owned by your process, or is otherwise inaccessible.
You need to first allocate an object of type mystruct. Here is a working example for you: http://ideone.com/XIdJ8
data is not initialized, and hence doesn't point to any sensible memory address. Moreover, there is no mystruct structure floating around, so there really isn't even any sensible data to point to. For your example, you want to:
Create a mystruct.
Make a pointer to it.
Make a pointer to that pointer.
If you only need to pass the double pointer to a library function, you don't need to create a variable for it. You make a normal pointer variable, initialize it to point to appropriate storage (if required by the function), then pass the address of the pointer (thus creating the double-pointer "on the fly").
I've never used libusb, so I'll give an example using a standard library function. From the manpage:
#include <stdlib.h>
long int strtol(const char *nptr, char **endptr, int base);
It only looks like a double-pointer. It's really a simulated-pass-by-reference single pointer. Allowing the function to return extra information besides its normal return value. strtol returns a long integer but it also can tell you at what point the string contents stopped looking like a number.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char *str = "99RED BALLOONS";
char *what;
long num;
num = strtol(str, &what, 10);
printf("Quantity: %ld; Description: %s;\n", num, what);
return 0;
}
Output:
Quantity: 99; Description: RED BALLOONS;
Or maybe you can try this:
void main(int argc, char*argv[])
{
mystruct *data;
myfunc(&data);
printf("member = %d\n", data->member);
}
This works for me in C++ and not needed to point another variable.
You're passing it a pointer, but the pointer isn't pointing at anything.
This may be more useful:
void main(int argc, char *argv[])
{
mystruct data;
mystruct *ptr = &data;
myfunc(&ptr);
printf("member = %d\n", (*ptr)->member);
}

c incompatible types in assignment, problem with pointers?

Hi I'm working with C and I have a question about assigning pointers.
struct foo
{
int _bar;
char * _car[SOME_NUMBER]; // this is meant to be an array of char * so that it can hold pointers to names of cars
}
int foofunc (void * arg)
{
int bar;
char * car[SOME_NUMBER];
struct foo * thing = (struct foo *) arg;
bar = thing->_bar; // this works fine
car = thing->_car; // this gives compiler errors of incompatible types in assignment
}
car and _car have same declaration so why am I getting an error about incompatible types? My guess is that it has something to do with them being pointers (because they are pointers to arrays of char *, right?) but I don't see why that is a problem.
when i declared char * car; instead of char * car[MAXINT]; it compiles fine. but I don't see how that would be useful to me later when I need to access certain info using index, it would be very annoying to access that info later. in fact, I'm not even sure if I am going about the right way, maybe there is a better way to store a bunch of strings instead of using array of char *?
EDIT: I didn't mean to use INT_MAX (maximum value of int), it's just some other int, which is about 20.
car and _car are both arrays, and you cannot assign arrays in C (except when the array is embedded in a structure (or union) and you do a structure assignment).
They are also arrays of pointers to char, rather than pointers to arrays of char. What you have written in the code is probably what you want - you could store pointers to up to MAXINT names in the array. However, you should describe the type correctly - as an array of pointers to char or char pointers.
A pointer to an array of characters would look like:
char (*car)[MAXINT];
And a point to an array of character pointers (thanks, Brian) would look like:
char *(*car)[MAXINT];
Be careful of MAXINT; that could be a very large array (on Linux, <values.h> defines MAXINT as INT_MAX, which is at least 231-1).
The code looks like:
struct foo
{
int _bar;
char * _car[MAXINT];
}
int foofunc (void * arg)
{
int bar;
char * car[MAXINT];
struct foo thing = (struct foo *) arg;
bar = arg->_bar; // this works fine
car = arg->_car; // this gives compiler errors of incompatible types in assignment
}
Neither the assignment to bar nor car should compile at all - arg is a void *. You presumably meant to use thing in some shape or form. As Brian noted, there are problems there, too:
You either want:
int foofunc(void *arg)
{
int bar;
char *car[MAXINT];
struct foo thing = *(struct foo *)arg;
bar = thing._bar; // this works fine
car = thing._car; // this is still an array assignment
...other code using bar and car...
}
Or you want:
int foofunc(void *arg)
{
int bar;
char *car[MAXINT];
struct foo *thing = (struct foo *) arg;
bar = thing->_bar; // this works fine
car = thing->_car; // this is still an array assignment
...other code using bar and car...
}
Or, indeed:
int foofunc(void *arg)
{
struct foo *thing = (struct foo *) arg;
int bar = thing->_bar; // this works fine
char *car[MAXINT] = thing->_car; // this is still an array assignment
...other code using bar and car...
}
Finally, dealing with the array assignment, in C you can reasonably use memmove() to do this:
int foofunc(void *arg)
{
struct foo *thing = (struct foo *) arg;
int bar = thing->_bar; // this works fine
char *car[MAXINT];
memmove(car, thing->_car, sizeof(car));
...other code using bar and car...
}
The similar function memcpy() does not have reliable semantics if the areas to be copied overlap, whereas memmove() does; it is simpler to always use memmove() because it always works correctly. In C++, you need to be cautious about using memmove() (or memcpy()). In this code, it would be safe enough, but understanding why is non-trivial.
You do need to be aware that you are just copying pointers here - you are not copying the strings that the pointers point at. If something else changes those strings, it affects both the values seen via car and the variable in the calling code.
One last point - for now: are you sure you need the argument to the function as a void *? It opens up the code to all sorts of abuse which can be prevented if the function is declared to take a 'struct foo *' instead (or even a 'const struct foo *').
You are creating a new array of size MAXINT. I think you want to create a pointer to an array of size MAXINT.
Creating a pointer to an array of char*'s:
The following is an array of size MAXINT to char* elements:
char * car[MAXINT];
The following is a pointer to: an array of size MAXINT to char* elements:
char* (*car)[MAXINT];
the following is how you set a pointer to: an array of size MAXINT to char* elements:
char* (*car)[MAXINT];
car = &arg->_car;
Other syntax errors in the question:
You need to have a semicolon after your struct definition.
You should be using foo* not foo. So it should be:
struct foo* thing = (struct foo *) arg;
You should be using thing not arg:
bar = thing->_bar;
car = thing->_car;
You can't assign to an array the way you're doing. You can do an element-wise copy.
for(int i = 0; i < MAXINT; i++)
{
car[i] = (arg->_car)[i]
}
Note that if if the strings aren't constant, you may need to use strcpy.
Array notation in C is legitimately confusing; your code doesn't mean what you think it means.
arg->_car means "the address of the array _car". Similarly, car means "the address of the array car". If you're trying to copy the contents of _car to car, then this will do it:
memcpy(car, _car, MAXINT);
But your real question, I think, is "what's the best way to store a list of strings?" This answer is: a dynamic list (one that grows automatically as you add items).
You'd declare it like this:
#define CARSIZE 65
int numCars = 0;
char **car; /* a list of addresses, each one will point to a string */
To add a car:
char *newCar = malloc(CARSIZE); /* make room */
strncpy(newCar, "Mercedes", CARSIZE); /* newCar has the address of the string */
car[numCars++] = newCar; /* store it */
To list the cars:
int n;
for (n = 0; n < numCars; ++n)
printf("%s\n", car[n]);
To remove the car at position n:
free(car[n]); /* release the memory */
/* condense the list of pointers */
for ( ; n < numCars - 1; ++n)
car[n] = car[n+1];
This is completely routine in C. NOTE: The above is off the top of my head and not copied from a working program, so I can't promise all the * are in the right place. I suspect this is a homework assignment, so I don't want to give you everything...

Resources