Dynamically assigning memory Topic is solved

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
marcoandarcia
Member++
Posts: 16
Joined: Wed Apr 15, 2015 9:31 am

Dynamically assigning memory

Post by marcoandarcia »

Hello everyone,

I have this code snipped I am trying to compile but I get the error "incompatible type for argument 1 of `_safe_realloc "

Code: Select all

short wall_determination(short buffer[])
{
   short  zeros_sets[MEMORY_SIZE];//stores size of zero sets
   short*  wall_open_sets = NULL;
   int  size=0;
   short dummy=0;
   short current_t=0;
   short wall_value = 0;

   for(int x = 1; x < MEMORY_SIZE; x++)
   {
       if(buffer[x] == 0 && buffer[x-1] == 0)
       {
           current_t++;
       }
       else
       {
           size++;
           wall_open_sets = (short*)realloc(wall_open_sets, size * sizeof(short));
           wall_open_sets[size-1] = current_t;
           current_t = 0;
       }
   }
}

I got this idea from http://www.cplusplus.com/reference/cstdlib/realloc/

Thanks in advance!
View Solution
User avatar
marcoandarcia
Member++
Posts: 16
Joined: Wed Apr 15, 2015 9:31 am

Post by marcoandarcia »

I got the code to compile doing the following modifications, the thing is that I need a short pointer, and this only seems to work with char type and I have no idea why.

Code: Select all

short wall_determination(short buffer[])
{
   char* movable wall_open_sets = NULL;
   //short* ghost_buffer = NULL;
   int  size=0;
   short dummy=0;
   short current_t=0;
   short wall_value = 0;

   for(int x = 1; x < MEMORY_SIZE; x++)
   {
       if(buffer[x] == 0 && buffer[x-1] == 0)
       {
           current_t++;
       }
       else
       {
           size++;
           wall_open_sets = realloc(move(wall_open_sets),size * sizeof(char));
           wall_open_sets[size-1] = current_t;
           current_t = 0;
       }
   }
}
peter
XCore Addict
Posts: 230
Joined: Wed Mar 10, 2010 12:46 pm

Post by peter »

Does this help:

Code: Select all

short wall_determination(short buffer[])
{
   char* movable wall_open_sets = NULL;
   short* unsafe ghost_buffer = NULL;
   int  size=0;
   short dummy=0;
   short current_t=0;
   short wall_value = 0;

   for(int x = 1; x < MEMORY_SIZE; x++)
   {
       if(buffer[x] == 0 && buffer[x-1] == 0)
       {
           current_t++;
       }
       else
       {
           size++;
           wall_open_sets = realloc(move(wall_open_sets),size * sizeof(short));
           unsafe {
            ghost_buffer = (short *unsafe)wall_open_sets;
            ghost_buffer[size-1] = current_t;
          }
           current_t = 0;
       }
   }
}