How can I get rid of type-punned pointer warnings in my MATLAB coder generated C code?

12 visualizaciones (últimos 30 días)
How can I change my code or settings to remove warnings about type-punned pointers? I am having problems with C code that is auto-generated with MATLAB Coder. The code runs fine on the embedded controller in debug mode but crashes when the C optimizer is enabled. I think the problem may be related to the way the C code is being created and the warnings may point to the problem. I would like to know if there are settings in Coder that I can change to fix this.
The C warnings look like this:
'warning: dereferencing type-punned pointer will break strict-aliasing rules'
and the C code line that generates the error is:
C = norm(*(real_T (*)[3])&Dx[0]);
The original line of MATLAB .m code is:
C = norm(Dx(1:3));
I am using MATLAB R2011b and VxWorks 6.7. Thanks for your help.
Update Feb 26: Thank you for your comments. I am currently trying to isolate where the crash occurs versus where the warning appears. I have a smaller set of routines which contain the warning and do not crash with the C optimizer, so these two issues may not be related. I would still like to find a way to rewrite the MATLAB code so that the auto-generated code doesn't produce warnings.
  1 comentario
Walter Roberson
Walter Roberson el 25 de Feb. de 2013
Turning off the warnings is not likely to fix any code problems.
Have you tried telling the compiler to generate code with various optimizations turned off, to see if you can isolate the circumstances under which the crash occurs?

Iniciar sesión para comentar.

Respuestas (2)

Arnab De
Arnab De el 26 de Feb. de 2013
It appears that the problem is caused by the strict aliasing optimization. Please try compiling the generated C code with -fno-strct-aliasing flag (assuming you are using gcc. Other compilers should have a similar flag.) It's hard to tell how the generated code is breaking strict aliasing rules without seeing more of it. Please provide us the signature of the function norm and the generated code of other calls of norm.

Mike Hosea
Mike Hosea el 26 de Feb. de 2013
I don't know how to change that generated code by manipulating options. However, you might be able to rewrite it and avoid having that code. It's not clear to me that the crash is related since an overrun might be tolerated in debug mode and not when the code is optimized. Try replacing the call
C = norm(Dx(1:3))
with
C = norm3(Dx(1),Dx(2),Dx(3))
where
function y = norm3(x1,x2,x3)
% 2-norm of the 3-element real vector [x1,x2,x3]
% taking care to avoid unnecessary overflow.
a = abs(x1);
b = abs(x2);
c = abs(x3);
if b > a
tmp = a;
a = b;
b = tmp;
end
if c > a
tmp = a;
a = c;
c = tmp;
end
if a > 0
b = b/a;
c = c/a;
end
y = a*sqrt(1 + b*b + c*c);

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by