Compiler warning for div

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use Atmel Studio 7.0 to program a ATSAMC20J18A in C. And I have a compiler warning that I don't understand.

 

Because I need the quotient and the remainder of a division I want to use the "div" method and the "div_t" struct to perform just one division for both result parts.

I would assume that I use the "div" method in the same way as this tutorial mentioned:

https://www.tutorialspoint.com/c...

 

Here the code snippet from my program:

uint32_t iActualStreamClusterNumber = 1234;
uint8_t iFatEntrySize = 4;
uint16_t iBlockSize = 512;

div_t sDivResult = div(iActualStreamClusterNumber * iFatEntrySize, iBlockSize);

uint32_t iFATFirstSectorAddress = 12345678;
uint32_t iDataSectorToRead = sDivResult.quot * iBlockSize + iFATFirstSectorAddress;
uint16_t iFATSectorByteOffset = sDivResult.rem;

 

The line 5 with the "div" method call throws the compiler warning "function call has aggregate value".

I don't know what the compiler complains about and how to fix this.

Can someone explain whats wrong? I just copied the tutorial example?!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You did include <stdlib.h> I assume.

I don't get any error when compiling (MSVS) but there IS a problem:

 

div takes two ints as parameters but you're passing in a uint32_t (With your numbers however there is little chance of overflow)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I include <stdlib.h>

 

For testing I tried:

div_t sDivResult = div(4, 3);

and

int iTest1 = 4;
int iTest2 = 3;
div_t oDivResult2 = div(iTest1, iTest2);

 

and got the same warning each. The parameter mismatch is not the reason for the warning.

 

The overflow is mathmatical possible but logically not. The FAT file system numbers not use the full range of the integer types by specification.

The validity check is at an other place in the code.

 

But thanks for testing. I already assumed that this is false positiv of the compiler.
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the full warning message? Does it mention something like "[-Waggregate-return]"? That could be a non-issue because div does, in fact, return an aggregate object (a div_t is a struct). That warning might be useful for user-defined functions to avoid returning huge structs, for example. If you want to disable that warning, you could try passing -Wno-aggregate-return to the compiler.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, the compiler tells me of course that this warning belongs to the compiler switch [-Waggregate-return].

But I interprete each warning as failure of my own and take even warnings for serious.

 

Basically the compiler is right. The div_t struct is an aggregate.

But it complains about the standard struct "div_t" from the standard method "div" from <stdlib>.

Or in other words it complains about itselt. surprise

 

If the only reason of this warning is to tell me that the method returns more data then I would expect,

then I will suppress the warning for this line of code. wink

 

When I understand the syntax for suppression correct:

https://docs.microsoft.com/en-us...

I need a "number" for this warning. Where can I find this number? In the output line of the compiler is no id or similar.

And on the page I don't see a reference to a list or similar.

Last Edited: Sat. Mar 12, 2022 - 07:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

docs.microsoft.com is not relevant since gcc is the used compiler.

This would work I expect:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waggregate-return"
	div_t sDivResult = div(iActualStreamClusterNumber * iFatEntrySize, iBlockSize);
#pragma GCC diagnostic pop

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks