Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12589 Discussions

Struct Assignment from function not working.

veex-tmiller
Beginner
422 Views

In my code I am attempting to return a struct from a function for assignment to another struct. It is in the general form of :

typedef struct WaterMarkStats_t{
unsigned int MarkBegin;
unsigned int MarkEnd;
}WaterMarkStats_t;

WaterMarkStats_t WaterMarkAvailableMemory()
{
WaterMarkStats_t markStats;
markStats.MarkBegin = 10;
markStats.MarkEnd = 15;
return markStats;
}

int main()
{

WaterMarkStats_t startStats = {0, 0};
startStats = WaterMarkAvailableMemory();
printf("\n\rBegin:\t%u\n\rEnd:\t%u\n\r", startStats.MarkBegin, startStats.MarkEnd);
return 0;
}

 

This is not the actual code but just a test case I used to verify on a different C compiler that it should work. The test on the second compiler worked fine as expected, in my case though, after I assign the struct, when I access the fields both MarkBegin and MarkEnd fields are equal to what I set MarkBegin equal to. So in this case both fields would be equal to 10.  

So i guess have several questions, is this intended behavior? If it is are there documents such as a compiler reference manual that spells out where it deviates from c89 or c99? If not, is there more documentation/code that would help in tracking down the problem? Also fwiw, all code in question are in c files not c++.

 

Actual Code In Question:

WaterMarkStats_t WaterMarkAvailableMemory()
{
  uint32 markEnd =  GetStackCurrent();
  uint32* pData;
  //VEEX_MALLOC(20, (void**)&pData);
  pData = (uint32*)malloc(20);
  uint32* pDataBackup = pData;
  uint32 markBegin = (uint32)&pData[0];
  LOG_ALWAYS("\n\rMark Start:%lu\n\rMark End:%lu\n\r",markBegin, markEnd);
  for(;((uint32)pData != markEnd); pData++)
  {
    *pData = 0xDEADBEEF;
  }
  //VEEX_FREE(pData);
  free((void*)pDataBackup);
  WaterMarkStats_t markStats;
  markStats.MarkBegin = markBegin;
  markStats.MarkEnd = markEnd;
  return markStats;
}
WaterMarkStats_t CheckWaterMark(WaterMarkStats_t markedStats)
{
  uint32 markEnd =  GetStackCurrent();
  if (markedStats.MarkEnd < markEnd)
  {
    markEnd = markedStats.MarkEnd;
  }
  uint32* pData;
  uint32* pDataBackup;
  //VEEX_MALLOC(20, (void**)&pData);
  pData = (uint32*)malloc(20);
  pDataBackup = pData;
  while(markedStats.MarkBegin > ((uint32)pData))
  {
    pData++;
  }
  checkState = SEARCH_BEGIN;
  largeStats.MarkBegin = 0;
  largeStats.MarkEnd = 0;
  for(;((uint32)pData != markEnd); pData++)
  {
    switch(checkState)
    {
      case SEARCH_BEGIN:
        if (*pData == 0xDEADBEEF)
        {
          currentStats.MarkBegin = (uint32)pData;
          checkState = SEARCH_END;
        }
        break;
      case SEARCH_END:
        if (*pData != 0xDEADBEEF)
        {
          currentStats.MarkEnd = (uint32)pData;
          if((largeStats.MarkEnd - largeStats.MarkBegin) < (currentStats.MarkEnd - currentStats.MarkBegin))
          {
            largeStats = currentStats;
          }
          checkState = SEARCH_BEGIN;
        }
        break;
    }
  }
  free((void*)pDataBackup);
  WaterMarkStats_t markStats = largeStats;
  return markStats;
}
void memCheckHandler(CmdData* cmd)
{
  WaterMarkStats_t startStats = {0, 0};
  WaterMarkStats_t endStats = {0, 0};
  startStats = WaterMarkAvailableMemory();
  endStats = CheckWaterMark(startStats);
  sendFullResponse(cmd, 16,"\n\rMark Start:\t%lu\n\rMark End:\t%lu"
      "\n\rCheck Start:\t%lu\n\rCheck End:\t%lu\n\r", startStats.MarkBegin,
      startStats.MarkBegin, endStats.MarkBegin, endStats.MarkEnd);
}

 

Also worth noting, that the function CheckWaterMark() also returns a watermark struct and that one is just fine.

0 Kudos
3 Replies
hareesh
Employee
383 Views

Hi Rodney Miller

tested your code on Eclipse. It's working fine on Eclipse. I'm not facing any issues with whatever you mentioned in the post. Can you please elaborate your problem?

 

 

0 Kudos
hareesh
Employee
380 Views

Screenshot (48).png

0 Kudos
hareesh
Employee
360 Views

Hi Rodney Miller,

Since I had provided you the solution/answer and did not received any feedback from you, I shall set this case to close pending. If you still need further assistance, you are welcome reopen this case within 20days or open a new case, some one will be right with you.

 

I shall set this case to close pending,

 

If you happened to close this case you will receive a survey. If you think you would rank your support experience less than 10 out of 10, please allow me to correct it before closing or if the problem can’t be corrected, please let me know the cause so that I may improve your future service experience.

 

 

Thanks,

Best regards,

Hareesh B.


0 Kudos
Reply