Need instruction, using realloc of struct array

Technical questions regarding the XTC tools and programming with XMOS.
fcvilla
Junior Member
Posts: 6
Joined: Fri Sep 09, 2016 6:47 pm

Need instruction, using realloc of struct array

Post by fcvilla »

I a c/c++ person, familiar with the use of realloc/malloc/Global_Alloc in windows enviro.

I this code will work,

unsigned char * movable array;

main()
{
int size = sizeof(mystruct)*5;
array = realloc( move(array) , size );

/*the problem, when I try to case 'array' a 'unsigned char *' to a mystruct */
/*hold_mystruct is a temporary var.*/
mystruct * movable hold_mystruct = realloc( move(array) , size ); // failed
mystruct * movable hold_mystruct = malloc( size ); // failed

mystruct * movable hold_mystruct = realloc( (unsigned char *)move(array) , size ); // failed
mystruct * movable hold_mystruct = (unsigned char *)malloc( size ); // failed
/* cast to pointer to type containing pointer type
Error history********************************************
- initializer has incompatible type (change of pointer target requires explicit cast)
- cast to pointer to type containing pointer type
- pointer cast to type of larger alignment
*/


}


Gothmag
XCore Addict
Posts: 129
Joined: Wed May 11, 2016 3:50 pm

Post by Gothmag »

I'm not entirely sure what you're trying to do but this will take the memory pointed to by array and let it be referenced by pointer hold_mystruct:

Code: Select all

mystruct * movable hold_mystruct = (mystruct* movable)move(array);
Alternatively if you are trying to take the pointer of array to cast it to the struct but are attempting to get a larger memory store afterwards, although size needs to be a larger value first, otherwise you're just move()'ing the array pointer:

Code: Select all

mystruct * movable hold_mystruct = (mystruct* movable)realloc(move(array), size);
Just rewriting the code into something that compiles(for a startkit):

Code: Select all

#include<stdlib.h>
unsigned char * movable array;
typedef struct mystruct {
    int ex1;
    int ex2;
} mystruct;


int main()
{
int size = sizeof(mystruct)*5;
array = realloc( move(array) , size );

/*the problem, when I try to case 'array' a 'unsigned char *' to a mystruct */
/*hold_mystruct is a temporary var.*/
mystruct * movable hold_mystruct = (mystruct* movable) realloc( move(array) , size ); // failed
hold_mystruct = (mystruct* movable)malloc( size ); // failed

hold_mystruct = (mystruct* movable)realloc( move(array) , size ); // failed
hold_mystruct = (mystruct* movable)malloc( size ); // failed
return 1;
}
There is some strangeness here though. The first realloc for array is basically just a call to malloc, since it was never initialized. The initialization of hold_mystruct could just be simplified to a

Code: Select all

mystruct* movable hold_mystruct = (mystruct* movable)move(array);
You're using the same variable for size you used when getting memory for array, this doesn't actually do anything. The next line is a memory leak. You're not freeing the memory you had allocated, you lost the address when you called realloc with move(array) as the first parameter, and you're now assigning the pointer an address to a new bit of free memory. The next line has the problem of again causing a memory leak. Array is no longer a pointer to a memory address, so the call acts like a call to malloc(size). And finally the same problem again. You're allocating more memory without freeing the old store.
fcvilla
Junior Member
Posts: 6
Joined: Fri Sep 09, 2016 6:47 pm

Post by fcvilla »

Thanks for your reply. All being say, the problem is based xmos compiling a "char *" to struct *.
Life would be so much easier if everyone could afford xmos official training.

I use malloc and realloc, to state the call fails regardless. I did look at the header files, I found two variants of proto type, one accepts a void *. Sorry for the mix up, some of the code was trial and error. I am taking array[x] creation control, so the line below, allocates sizeof(struct)*5
so, size = sizeof(struct)*5;
realloc( move(array) , size ); this part of the line will compile.
(mystruct* movable); this is an attempt at casting the ptr from realloc, this cast will not work
nor xmos cast (type,new type) for my use fails.
mystruct * movable hold_mystruct =; this should be ok, when compiled alone

mystruct * movable hold_mystruct = (mystruct* movable) realloc( move(array) , size );

this line works written like this

redefining Array as a 'char *' and not casting to struct.
char * hold = realloc( move(array) , size ); or array = realloc( move(array) , size );

Problem****
When I cast 'char *' pointer to a struct *, the code will not compile.

I also tried,

breaking it down to;
enum ptrVars {item=0,product=4.....};

not changing array from char *,

cause much the same problem casting again,
&(char *(array+item)) = (char)1; // I will have to retry this again. don't remember the error.
&(unsigned int *(array+item)) = 1; // this fails

So, does anyone know casting between types in a xmos environment, in c for windows nothing I wrote would fail.
Last edited by fcvilla on Fri Oct 28, 2016 5:08 am, edited 1 time in total.
Gothmag
XCore Addict
Posts: 129
Joined: Wed May 11, 2016 3:50 pm

Post by Gothmag »

If you want you can use unions so that you can access data items multiple ways.

Code: Select all

typedef struct mystruct {
    int ex1;
    int ex2;
} mystruct;

typedef union char_struct {
    mystruct value;
    char data[8];
} char_struct;
So you'd use char_struct in place of mystructs and use the value member to modify the struct directly. Then when you need to get the raw data you use the data member and subscript normally. Casting can be a bit cumbersome because xc has extra safety. Unions aren't much better but they can be easier to keep track of.

Alternatively...

Code: Select all

#include <stdlib.h>

typedef struct mystruct {
    int ex1;
    int ex2;
} mystruct;


unsafe int main()
{
    unsigned size = sizeof(mystruct) * 5;
    unsafe {
        char * unsafe array = (char * unsafe)malloc( size );
        mystruct * unsafe hold_struct = (mystruct * unsafe)malloc(sizeof(mystruct));

        char * unsafe cvalue = &((char*)hold_struct)[0];
        mystruct * unsafe pulled_struct = &((mystruct*)array)[0]; //Alias a specific subscripted struct from array
    }
    return 1;
}
This will work to access a char* as a mystruct* and a mystruct* as a char*. Hopefully one of these helps.

EDIT: I had to change the code to use unsafe pointers, you will get a runtime exception due to the movable pointers. When using memory access like this it's probably the right thing to do.

You can do it with movable pointer IF they're declared at global scope(basically avoid runtime check). This is more or less a hack to get around the safety of movable pointers though. You should probably just use unsafe and use unsafe blocks or functions where needed.

Code: Select all

#include <stdlib.h>
#include <stdio.h>

typedef struct mystruct {
    int ex1;
    int ex2;
} mystruct;

char * movable array;
mystruct * movable hold_struct;

int main()
{
    unsigned size = sizeof(mystruct) * 5;

    hold_struct = (mystruct * movable)malloc(sizeof(mystruct));
    array = (char * movable)malloc( size );

    mystruct * movable value = &((mystruct* movable)array)[0];
    char * movable cvalue = &((char* movable)hold_struct)[0];

    printf("Array mem: %i\nhold_struct mem: %i\nvalue mem(char* to mystruct*): %i\ncvalue mem(mystruct* to char*): %i\n", array, hold_struct, value, cvalue);
    return 1;
}
Last edited by Gothmag on Fri Oct 28, 2016 6:12 am, edited 11 times in total.
fcvilla
Junior Member
Posts: 6
Joined: Fri Sep 09, 2016 6:47 pm

Post by fcvilla »

that I will try. I will try tomorrow. Thanks. I'll post the out come.
fcvilla
Junior Member
Posts: 6
Joined: Fri Sep 09, 2016 6:47 pm

Post by fcvilla »

That is ok, I remembered. It's xc not c.
No solution from xc, I did not want to mix c in, but maybe I will.
Gothmag
XCore Addict
Posts: 129
Joined: Wed May 11, 2016 3:50 pm

Post by Gothmag »

Well XC is mostly C, few differences. Have you read the XMOS programming guide? It's very short, like a copy of K&R c programming language, and easy to understand. Good luck with your projects.