How can you swap two fixed-size opencl-c arrays - arrays

I want to swap two arrays within an opencl-c call in that manner:
uint tmp_a[L] = {...};
uint tmp_b[L] = {...};
swap(tmp_a, tmp_b); // (tmp_a, tmp_b) <- (tmp_b, tmp_a)
I would like to do it just by swapping the base addresses of the two pointers, but unfortunatley
void swap(uint num_a[L], uint num_b[L]) {
uint* tmp = num_a;
num_a = num_b;
num_b = tmp;
}
doesn't work.
In reality I do it now this way:
for(int i = 0; i < L; ++i) {
const uint tmp = num_a[i];
num_a[i] = num_b[i];
num_b[i] = tmp;
}
I would like to reduce this unnecessary work.

An array is just a pointer to a chunk of memory. When you pass an array to a function (as in your code snippet), you are passing the value of that pointer to the function. Passing an argument to a function is much like an assignment operation. Consider this simple example:
void foo(uint arg[L])
{
arg = NULL;
}
uint bar[L] = {...};
foo(bar);
This is essentially the same as:
uint bar[L] = {...};
uint arg[L] = bar;
arg = NULL;
The pointer stored in bar hasn't been affected by the assignment that happened inside the foo function. If we did want to modify the pointer stored in bar inside the foo function, we should instead pass the address of bar:
void foo(uint *arg[L])
{
*arg = NULL;
}
uint bar[L] = {...};
foo(&bar);
Which is equivalent to:
uint data[L] = {...};
uint *arg[L] = &bar;
*arg = NULL;
This time, arg points to the memory that stores the data contained in bar, which is the pointer to the array values. This means that if we modify the stuff that arg points to, then we will also be modifying bar.
Applying this to your code snippet would look like this:
void swap(uint *num_a[L], uint *num_b[L])
{
uint *tmp = *num_a;
*num_a = *num_b;
*num_b = tmp;
}
...
uint tmp_a[L] = {...};
uint tmp_b[L] = {...};
swap(&tmp_a, &tmp_b); // (tmp_a, tmp_b) <- (tmp_b, tmp_a)

Related

Void pointer value doesn't change when changed in function

So, I am trying to test if my void* value is correct, but it keeps saying it's NULL although I know it does change in my function.
TestCode:
void test_mystack_push_and_pop(void)
{
void* obj;
mystack_pop(1, &obj);
TEST_ASSERT_EQUAL(12, (intptr_t)obj);
}
Mystack_pop:
int mystack_pop(int handle, void* obj)
{
pStackMeta_t tmpStackList = gStackList;
obj = tmpStackList->stack->obj;
tmpStackList->stack = tmpStackList->stack->next;
tmpStackList->numelem -= 1;
DBG_PRINTF("handle: %d, obj: %p\n", handle, obj);
return 0;
}
So, if I check the value of obj in mystack_pop it is not null but then in the test it is still null. I've tried it all but can't get it to work.
If you want to update a pointer parameter, you need to pass it as **ptr.
By writing **Ptr you state that your output parameter Ptr is a pointer on a pointer. So a pointer on a variable of type pointer.
Try:
int mystack_pop(int handle, void **obj)
{
pStackMeta_t tmpStackList = gStackList;
*obj = tmpStackList->stack->obj;
tmpStackList->stack = tmpStackList->stack->next;
tmpStackList->numelem -= 1;
DBG_PRINTF("handle: %d, obj: %p\n", handle, *obj);
return 0;
}

Correct use of Callback for same operation and different type

I'm developing in C and I need ask an info about callback use.
Suppose I defined 3 callback with 3 different type in input for callback, example:
typedef void (*CB_1) (const struct paramType_1 *p);
typedef void (*CB_2) (const struct paramType_2 *p);
typedef void (*CB_3) (const struct paramType_3 *p);
Ok I have 3 array of callback, each for type of callback:
static CB_1 CB1List[10] ;
static CB_2 CB2List[10] ;
static CB_3 CB3List[10] ;
So I have defined 3 list of callback to call (in maybe different situation) and each list is of specific type of callback (CB_1 ,CB_2 or CB_3) that have a specific callback param (paramType_1,paramType_2 or paramType_3).
Suppose now that I need to perform an operation that is IDENTICAL for each callback ... I must copy paste 3 time the function due different specific param... suppose for example that i need to add a callback to array I need this;:
static void CBAdd_1(CB_1 _cb) {
CB1List[i] = _cb
}
static void CBAdd_2(CB_2 _cb) {
CB2List[i] = _cb
}
static void CBAdd_3(CB_3 _cb) {
CB3List[i] = _cb
}
what is a correct way to use a generic funcion "void CBAdd" for not replicate three time the function for the three callback? maybe using (void*) parameters or other?
thanks
Yes, you need a void* parameter like below. Alternatively (depends on your need) you might use a union instead of struct and store all the members in that single union type. Having that, a union type parameter will be passed to all functions and the function definition may decide which member to use.
#include <stdio.h>
typedef struct paramType_1
{
int p;
} paramType_1;
typedef struct paramType_2
{
int p;
} paramType_2;
typedef struct paramType_3
{
int p;
} paramType_3;
typedef void (*CB) (const void* p);
static CB CBList[10];
void CB_1(const void* p)
{
const paramType_1* pt = (paramType_1*)p;
printf("p1: %d\n", pt->p);
}
void CB_2(const void* p)
{
const paramType_2* pt = (paramType_2*)p;
printf("p2: %d\n", pt->p);
}
void CB_3(const void *p)
{
const paramType_3* pt = (paramType_3*)p;
printf("p3: %d\n", pt->p);
}
static int c = 0;
static void CBAdd(CB cb_)
{
CBList[c++] = cb_;
}
int main()
{
int i = 0;
paramType_1 p1;
paramType_2 p2;
paramType_3 p3;
for (i = 0; i < 3; i++)
{
CBAdd(CB_1);
}
for (i = 0; i < 3; i++)
{
CBAdd(CB_2);
}
for (i = 0; i < 3; i++)
{
CBAdd(CB_3);
}
p1.p = 1;
p2.p = 2;
p3.p = 3;
for (i = 0; i < 3; i++)
{
(CBList[i])((void*)&p1);
}
for (i = 3; i < 6; i++)
{
(CBList[i])((void*)&p2);
}
for (i = 6; i < 9; i++)
{
(CBList[i])((void*)&p3);
}
return 0;
}

Pass local variable of a function back to it's parameter

I'm wanting to pass a local variable within a function, back through it's pointer parameter (not returned).
My assignment uses a stack data structure, and one criteria that must be used is the Pop() function must have a pointer parameter that is used to return the top-most item on the stack. I have used this before. My program became more complex with a data struct, I started getting either segmentation faults, or the data not being saved after the function's frame popped.
// Definitions
typedef char * string;
typedef enum { SUCCESS, FAIL } result;
typedef enum { INTEGER, DOUBLE, STRING } item_tag;
// Result Check
static result RESULT;
// Item_Tag
typedef struct {
item_tag tag;
union {
int i;
double d;
string s;
} value;
} item;
// Declarations
int STACK_SIZE = 0;
const int MAX_STACK_SIZE = 1024; // Maximum stack size
item stack[1024];
// Pop
result Pop(item *ip){
item poppedItem;
item * pointerReturn = malloc(sizeof(item));
// Check stack size is not 0
if(STACK_SIZE == 0){
return FAIL;
}
// If stack size is only 1, creates a blank stack
else if(STACK_SIZE == 1){
item emptyItem;
// Initialize
emptyItem.tag = INTEGER;
emptyItem.value.i = 0;
// Check top item's tag
poppedItem = stack[0];
// Store top item data based on tag
switch(stack[0].tag){
case STRING:
poppedItem.value.s = stack[0].value.s;
case DOUBLE:
poppedItem.value.d = stack[0].value.d;
default:
poppedItem.value.i = stack[0].value.i;
}
poppedItem.tag = stack[0].tag;
// Allocate memory for parameter, and have it point to poppedItem
ip = malloc(sizeof(poppedItem));
*ip = poppedItem;
// Store empty stack to top of stack
stack[0] = emptyItem;
// Decrease stack size
STACK_SIZE--;
}
// Grab top Item from stack
else{
// Check top item's tag
poppedItem = stack[0];
// Store top item data based on tag
switch(stack[0].tag){
case STRING:
poppedItem.value.s = stack[0].value.s;
case DOUBLE:
poppedItem.value.d = stack[0].value.d;
default:
poppedItem.value.i = stack[0].value.i;
}
poppedItem.tag = stack[0].tag;
// Allocate memory for parameter, and have it point to poppedItem
ip = malloc(sizeof(poppedItem));
*ip = poppedItem;
// Reshuffle Items in Stack
for(int idx = 0; idx < STACK_SIZE; idx++){
stack[idx] = stack[idx + 1];
}
STACK_SIZE--;
}
return SUCCESS;
}
My knowledge with pointers is alright, and memory location/management. But I can't claim to be an expert by any means. I don't exactly know what happens in the background when you're using the function's own pointer parameter as a means of passing data back.
What is the correct syntax to solve this problem?
How can a parameter pass something back?
Thanks in advance!
EDIT*
Since many people are confused. I'll post some snippets. This is an assignment, so I cannot simply post all of it online as that'd be inappropriate. But I think it's okay to post the function itself and have people analyze it. I'm aware it's a bit messy atm since I've edited it several dozen times to try and figure out the solution. Sorry for the confusion. Keep in mind that not all the code is there. just the function in question, and some of the structure.
The function should receive a pointer to a valid object:
item catcher;
myFunc(&catcher); // Pass a pointer to catcher
and the function should modify the object it received a pointer to:
void myFunc(item *itemPointer)
{
itemPointer->variable = stuff;
// or
*itemPointer = someItem;
}
Update:
You're overcomplicating things immensely – there should be no mallocs when popping, and you're leaking memory all over the place.
(Your knowledge of pointers and memory management is far from "alright". It looks more like a novice's guesswork than knowledge.)
It should be something more like this:
result Pop(item *ip){
if (STACK_SIZE == 0){
return FAIL;
}
else {
*ip = stack[0];
for(int idx = 0; idx < STACK_SIZE; idx++){
stack[idx] = stack[idx + 1];
}
STACK_SIZE--;
}
return SUCCESS;
}
but it's better to push/pop at the far end of the array:
result Pop(item *ip){
if (STACK_SIZE == 0){
return FAIL;
}
else {
*ip = stack[STACK_SIZE-1];
STACK_SIZE--;
}
return SUCCESS;
}
Response to the originally posted code:
typedef struct{
variables
}item;
void myFunc(item *itemPointer){
item newItem;
newItem.variable = stuff;
}
int main(){
item * catcher;
myFunc(catcher);
printf("%s\n", catcher.variable);
}
A few issues.
Your program will not compile. variable has to have a type.
void myFunc(item *itemPointer){
item newItem;
newItem.variable = stuff;
}
stuff is not defined; item *itemPointer is not used.
item * catcher pointer has to point to allocated memory. It is not initialized.
Pass arguments via pointers and modify member of the structure like this:
void myFunc(item *itemPointer, const char *string){
itemPointer->variable = string ;
}
Solution like:
void myFunc(item *itemPointer)
{
itemPointer->variable = stuff;
// or
*itemPointer = someItem;
}
is possible, but it assumes that stuff or someItem is a global variable which is not the best programming practice IMO.
Retrieve value from pointer via -> not . operator.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char * variable;
}item;
void myFunc(item *itemPointer, const char *string){
itemPointer->variable = string ;
}
int main(){
item * catcher;
char *new_string = "new string";
catcher = malloc(sizeof(item));
myFunc(catcher, new_string);
printf("%s\n", catcher->variable);
free(catcher);
return 0;
}
OUTPUT:
new string

How could I know an Uncertain type parameters' size

const static int VECTOR_BASIC_LENGTH = 20;
struct m_vector
{
void* my_vector;
size_t my_capacity;
size_t my_head;
};
typedef struct m_vector Vector;
Vector creat_Vector(size_t size,void *judge)
{
Vector _vector;
size = size?size:VECTOR_BASIC_LENGTH;
_vector.my_capacity = size;
_vector.my_head = 0;
//How I write the following two lines
_vector.my_vector = malloc(sizeof(*judge) * size);
return _vector;
}
The type of judge is uncertain,so I pass a void pointer as a parameters.I need the size of *judge to allocate memory to _vector.my_vector,for example if I use:
int *a;
creat_Vector(5,a);
I want the following line:
_vector.my_vector = malloc(sizeof(*judge)*size);
is equal to:
_vector.my_vector = malloc(sizeof(*a)*5);
How could I achieve this function.Using pure C
There is a forbidden thing done in your code.
You statically (at compile time) allocate/declare a local _vector of type Vector in your function creat_Vector. Then you return this object to the outside world. However, when you are exiting your function, all local data is dead. So, you should absolutely rethink this.
One suggestion would be:
int init_Vector(Vector* _vect, size_t size, unsigned int ptr_size)
{
size = size?size:VECTOR_BASIC_LENGTH;
_vect->my_capacity = size;
_vect->my_head = 0;
_vect->my_vector = malloc(size*ptr_size);
if (_vect->my_vector) {
return 0;
}
return 1;
}
Then:
Vector _vector;
char *a;
if (init_Vector(&_vector, 5, sizeof(char)) == 0) {
printf("Success!\n");
}
else {
printf("Failure!\n");
/* treat appropriately (return error code/exit) */
}
/* do whatever with a (if needed) and _vector*/

C Pointer and Memory Allocation: Realloc Arrays and Pointer Passing

For those experienced with C, this will be a simple memory allocation/referencing problem:
Here are my data structures:
struct configsection {
char *name;
unsigned int numopts;
configoption *options;
};
typedef struct configsection configsection;
struct configfile {
unsigned int numsections;
configsection *sections;
};
typedef struct configfile configfile;
Here are my routines for initializing a configsection or configfile, and for adding a configsection to a configfile:
// Initialize a configfile structure (0 sections)
void init_file(configfile *cf) {
cf = malloc(sizeof(configfile));
cf->numsections = 0;
}
// Initialize a configsection structure with a name (and 0 options)
void init_sec(configsection *sec, char *name) {
sec = malloc(sizeof(configsection));
sec->numopts = 0;
sec->name = name;
printf("%s\n", sec->name);
}
// Add a section to a configfile
void add_sec(configfile *cf, configsection *sec) {
// Increase the size indicator by 1
cf->numsections = cf->numsections + 1;
// Reallocate the array to accommodate one more item
cf->sections = realloc(cf->sections, sizeof(configsection)*cf->numsections);
// Insert the new item
cf->sections[cf->numsections] = *sec;
}
I believe my problem originates in my init_sec() function. Here is an example:
int main(void) {
// Initialize test configfile
configfile *cf;
init_file(cf);
// Initialize test configsections
configsection *testcs1;
init_sec(testcs1, "Test Section 1");
// Try printing the value that should have just been stored
printf("test name = %s\n", testcs1->name);
Although the printf() in init_sec() successfully prints the name I just stored in the configsection, attempting the same thing in the printf() of main() produces a segmentation fault. Further, addsec() produces a segmentation fault.
This routine should be
void init_file(configfile **cf) {
*cf = malloc(sizeof(configfile));
(*cf)->numsections = 0;
(*cf)->sections = NULL; // You forgot to initialise this.
}
i.e. called by init_file(&myconfigfilepointer); so the malloc return value gets passed back.
Need to do the same trick for init_sec
This function is incorrect - here is a corrected version
void add_sec(configfile *cf, configsection *sec) {
// Increase the size indicator by 1
// Reallocate the array to accommodate one more item
cf->sections = realloc(cf->sections, sizeof(configsection)*(1 + cf->numsections));
// Insert the new item
cf->sections[cf->numsections] = *sec; // Since arrays start at 0
cf->numsections = cf->numsections + 1;
}
You then need to adjust the calls in main
At no point do you initialise cf->sections, which means when you try to realloc it the first time, you're passing rubbish. Adding:
cf->sections = NULL;
to init_file should help.
You're also not checking any return codes, but you knew that yes?
You need to pass a pointer of the value to be updated... eg:
// Initialize a configfile structure (0 sections)
void init_file(configfile **cf) {
*cf = malloc(sizeof(configfile));
(*cf)->numsections = 0;
}
configfile *var;
init_file(&var);
printf("%d\n", var->numsections);
Otherwise you are just updating the local pointer *cf and not the original passed in value
You need to really rethink how function arguments are passed in C and what pointers are. Your problem has nothing to do with memory allocation. Rather, your code is assigning a pointer to dynamically allocated memory only to a local variable, of which the calling code knows nothing.
While you could solve the problem by passing a pointer to the caller's pointer (i.e. a double pointer), this is not necessarily the most elegant or most usual way of handling things. Rather, you should return the result of the allocation from the function. While you're at it, you should also use calloc to zero out the memory right away. Wrapping it all up:
typedef struct substuff_
{
int a;
double b;
} substuff;
typedef struct stuff_
{
unsigned int n;
substuff * data;
} stuff;
substuff * init_substuff()
{
substuff * const p = malloc(sizeof *p);
if (p) { p->a = 5; p->b = -0.5; }
return p;
}
stuff * init_stuff()
{
substuff * const p = init_substuff();
if (!p) return NULL;
stuff * const q = malloc(sizeof *q);
if (q) { q->n = 10; q->data = p; }
return q;
}
As an exercise, you should write the corresponding functions void free_substuff(substuff *) and void free_stuff(stuff *).
Yes, there is a problem in init_sec
// Initialize a configsection structure with a name (and 0 options)
void init_sec(configsection *sec, char *name) {
sec = malloc(sizeof(configsection));
sec->numopts = 0;
sec->name = name;
printf("%s\n", sec->name);
}
You're just copying the name pointer here, which means, that it points to the original storage of name. If you'd call init_sec like this
configsection foobar()
{
configsection sec;
char name[80];
get_name(name);
init_sec(sec, name);
return sec;
}
The name pointer became invalid the moment foobar returned. You need to duplicate the string and keep your private copy around. In init_sec:
sec->name = strdup(name);
But there's more. In the very first line of init_sec you're overwriting the pointer that was passed to init_sec with the one of malloc. So the new pointer never gets passed back to the calle. Either use a pointer to a pointer, don't take a configsection pointer at all (after all, you're allocating), but just return the allocated pointer: Complete corrected function:
// Initialize a configsection structure with a name (and 0 options)
configsection* init_sec(char *name) {
configsection *sec = malloc(sizeof(configsection));
sec->numopts = 0;
sec->name = name;
printf("%s\n", sec->name);
return sec;
}

Resources