Page 1 of 1

Why I can not return pointer in xC language?

Posted: Tue May 22, 2018 7:50 am
by Papayaved
uint8_t* alias RxUdpData(uint8_t eth_pack[]) {
uint8_t* alias ip_pack = &eth_pack[14];
return &ip_pack[20];
}

error: possible return of alias pointer `ip_pack' with local scope

I wanna use it like this

uint8_t rx_pack[ETHERNET_MAX_PACKET_SIZE];
ethernet_packet_info_t eth_pkg_info;

i_rx.get_packet(eth_pkg_info, rx_pack, ETHERNET_MAX_PACKET_SIZE);
...
uint8_t* alias data = RxUdpData(rx_pack);

Re: Why I can not return pointer in xC language?

Posted: Tue May 22, 2018 9:09 am
by robertxmos
Hi Papayaved,

There are four types of pointer in xC: alias, restricted, movable, unsafe (unspecified will default to either alias or restricted depending upon situation).

Unfortunately the compilers analysis is failing to identify that you are not returning a pointer into a locally scoped object - sorry.
So you need to use an unsafe pointer (a normal C pointer viz raw).
You will also need to make sure the code that creates it (the return statement) is in an unsafe region (I've marked the whole function as unsafe below).

Code: Select all

unsafe
uint8_t * unsafe RxUdpData(uint8_t eth_pack[]) {
  uint8_t* alias ip_pack = &eth_pack[14];
  return &ip_pack[20];
}
Unsafe can become quite contagious - sorry.
robert

Re: Why I can not return pointer in xC language?

Posted: Tue May 22, 2018 1:15 pm
by Papayaved
Thank you Robert,

but I read
If a function takes pointer parameters or returns a pointer that may alias, it needs
to be explicitly written into the type of the function. For example the following
function’s return value may alias its argument:

Code: Select all

char * alias strchr ( const char * alias haystack , int needle );
Why I must move safe data to unsafe, if I have safe data array and I wanna return pointer to safe data in this array

Re: Why I can not return pointer in xC language?

Posted: Wed May 23, 2018 11:24 am
by robertxmos
You are quite right, you should not have to.
This has been logged as a bug and in future releases it will be reported as a 'possible' warning, rather than an error.

Unfortunately, the compiler is not tracing back through expressions to discover that the root object was not local but was indeed passed in as a parameter reference-type.