Keil Logo

C51: Data Overlaying Problem with Struct Parameters


Information in this article applies to:

  • C51 All Versions
  • CX51 All Versions
  • C251 All Versions

SYMPTOM

When a function receives two or more struct arguments, and one of these arguments is the return value of another function, the data overlaying may fail.

Example:

typedef struct
{
  float real;
  float imag;
} CFLOAT;


CFLOAT cfloat_mul (CFLOAT x, CFLOAT y)  {
  CFLOAT result;

  result.real = (x.real * y.real) - (x.imag * y.imag);
  result.imag = (x.real * y.imag) + (x.imag * y.real);
  return (result);
}


CFLOAT Get_val (int v)  {
  CFLOAT val;

  val.real = v;
  val.imag = 0;
  return val;
}


void main (void)  {
  CFLOAT temp;

  temp = cfloat_mul(Get_val(1), Get_val(2));  // error due to data overlaying
  while (1);
}

CAUSE

structs are returned in fixed memory addresses.

When the struct is used again as a parameter to another function, the parameter may overwrite the values in the return struct.

RESOLUTION

Functions that receive more than one struct parameter can be excluded from data overlaying using the overlay directive of the linker.

The following linker directive excludes the function cfloat_mul from data overlaying:

OVERLAY (* ! cfloat_mul)

Similar functions may be overlaid in a separate root. The following linker directive overlays the data of cfloat_mul and cfloat_add but ensures that the struct parameters of the individual functions are not overwritten:

OVERLAY (* ! (cfloat_mul, cfloat_add))

In µVision, the arguments of the OVERLAY directive are entered under Project - Options - Linker Misc.

MORE INFORMATION

  • Refer to OVERLAY in the BL51 User's Guide.

Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.