Simple ide use memcpy multicore
Mart1970
Posts: 23
Hello
I am trying to copy array to use with multicog.
memcpy(&(a[0]), &(a[i - 1][0]), 100 * sizeof(char));
It works when i'm using it in one cog.
But when i'm make the array a volatile unsigned char a[50]128]; array
i get an error
warning: passing argument 1 of 'memcpy' discards 'volatile' qualifier from pointer target type [enabled by default].
Hope somone can help me with this
I am trying to copy array to use with multicog.
memcpy(&(a[0]), &(a[i - 1][0]), 100 * sizeof(char));
It works when i'm using it in one cog.
But when i'm make the array a volatile unsigned char a[50]128]; array
i get an error
warning: passing argument 1 of 'memcpy' discards 'volatile' qualifier from pointer target type [enabled by default].
Hope somone can help me with this
Comments
It occurs because the signature of memcpy is: void *memcpy(void *dest, const void *src, size_t n);
Note that there is no "volatile" only "void*" and "const void*".
The point about volatile is that it tells the compiler that something else other than the code it is compiling may access the variable at any time, say another thread or interrupt or hardware. In our case another cog. This causes the compiler to not apply any optimizations that may cause problems with that shared data.
This warning is because the memcpy does not know about your volatile so potentially it may be compiled in such away as to mess up your variable sharing.
I would just use a cast to get rid of the warning: memcpy((void*)a, (const void*)a[i - 1], 100 * sizeof(char));
Notice I also got rid of the "&" and "[0]". We can do this because "&a[0]" is the same as just "a". You have a two dimensional array so "&(a[0])" is the same as just "a"
Edit:
Of course in general casting away the volatile when passing arguments to functions may well cause problems with the handling of the shared data because the called function knows nothing of your volatile and may well optimize things badly. I fairly confident that you will be OK with memcpy though.
But why have volatile at all? Presumably other COGs will not be reading and writing to that array whilst the memcpy is happening, you will have semaphores or be using locks to prevent that. In that case volatile is not required.