uint8_t* alias RxUdpData(uint8_t eth_pack[]) {
uint8_t* alias ip_pack = ð_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);
Why I can not return pointer in xC language?
-
- New User
- Posts: 3
- Joined: Tue May 22, 2018 7:39 am
-
- XCore Addict
- Posts: 169
- Joined: Fri Oct 23, 2015 10:23 am
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).
Unsafe can become quite contagious - sorry.
robert
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 = ð_pack[14];
return &ip_pack[20];
}
robert
-
- New User
- Posts: 3
- Joined: Tue May 22, 2018 7:39 am
Thank you Robert,
but I read
but I read
Why I must move safe data to unsafe, if I have safe data array and I wanna return pointer to safe data in this arrayIf 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 );
-
- XCore Addict
- Posts: 169
- Joined: Fri Oct 23, 2015 10:23 am
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.
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.