Title: XC16 v1.20 -> v1.25 - memset issue with optimization -s Post by: Avataar120 on December 16, 2015, 03:57:21 15:57 Hello everybody,
I have an issue with all XC16 version from 1.20 to 1.25 with the memset instruction + optimization level s in a very typical situation ... I tried to simplify my code at maximum ... Code: /* After memset, the content of User is completely random. If I comment the line "Function1" -> no more issue If I don't make call to Function2 with User.Content parameter -> no more issue If I compile this code without optimization -> no more issue Has somebody an idea ? Thanks ! Title: Re: XC16 v1.20 -> v1.25 - memset issue with optimization -s Post by: FTL on December 17, 2015, 02:25:18 02:25 I think there is an issue here with passing by value vs. passing by reference.
When you do "function1 ( User );" you are passing User by value, so any change to User made in the function cannot be returned. You must pass by reference, or explicitly use a pointer to the data to be able to modify the data in the structure (instead of a copy). I agree that optimized and unoptimized code should do the same thing, but the compiler may be validly removing "useless" code. When I look at the "*User = 0;", that should change the first byte in a character array to zero. If it is interpreted as a string, that is the same as initializing the whole string to a zero length string, and the bytes following the first byte do not matter. Could the compiler have seen that and decided that since you are initializing a string to "", that the memset of the rest of the string to 0xFF could be optimized out? It has been a while since I've coded straight C with structs and unions, so I'm not absolutely sure, but I would look into what the C standard has to say about passing a struct by value. Edit - Fixed typo. |