Home > Software engineering >  For loop not incrementing (possibly a malloc problem)
For loop not incrementing (possibly a malloc problem)

Time:11-28

I am porting code from an Intel XScale CPU architecture (ARM) to a i386 CPU Architecutre (Vortex86 CPU. I am at a point where the code compiles and runs, but i am experiencing very odd behaviour with a for() loop in the software startup.

I'm initialising data buffers for 4 different sampling frequencies. and 4 different channels, CH0, CH1, CH2, CH3, each with 100Hz, 20Hz, 1Hz, and 10Min sample rates.

sample = int. buffers sizes are:

  • 100Hz = 6000samples * sizeof(sample)

  • 20Hz = 1200samples * sizeof(sample)

  • 1Hz = 3600samples * sizeof(sample)

  • 10Min = 144samples * sizeof(sample)

  • The loop succeeds for ChanID=0

  • The loop succeeds for ChanId=1, BUT reports "CH0 Initialised"... and so on... repeat

This is the loop:

// THIS DOESN'T WORK.. I don't know why...
// Initialise() performs a malloc x 4 for each channel for the sizeof(buffer)
// Initialise() also opens a serial port DeviceNames[ChanId], with the FD returned

    for (ChanId=0; ChanId < NUM_CHANNELS; ChanId  ) 
    {
        if ((fds[ChanId] = Initialise(ChanId, DeviceNames[ChanId])) == -1) 
        {
            syslog(LOG_INFO, "Failed to initialise RT Ch%d", ChanId);           
            return -1;
        }
        else
        {   
            syslog(LOG_INFO, "Ch%d Initialised", ChanId);           
        }
    }

I have tried the following which seems to work without issue.

if ((fds[0] = Initialise(0, DeviceNames[0])) == -1) 
{
    syslog(LOG_INFO, "Failed to initialise RT Ch0");            
    return -1;
}
if ((fds[1] = Initialise(1, DeviceNames[1])) == -1) 
{
    syslog(LOG_INFO, "Failed to initialise RT Ch1");            
    return -1;
}
if ((fds[2] = Initialise(2, DeviceNames[2])) == -1) 
{
    syslog(LOG_INFO, "Failed to initialise RT Ch2");            
    return -1;
}
if ((fds[3] = Initialise(3, DeviceNames[3])) == -1) 
{
    syslog(LOG_INFO, "Failed to initialise RT Ch3");            
    return -1;
}

I have pushed the Malloc address pointer to the log, and it looks reasonable.

I'm wondering if the malloc process is corrupting the variables currently in the heap?

THe exact same code works fine on the ARM Architecture Intel XScale

CodePudding user response:

I suspect that this line

if ((fds[ChanId] = Initialise(ChanId, DeviceNames[ChanId])) == -1)

is causing issue with the compiler optimisation when run within a loop or timing (as you state that Initialise is interacting with serial ports). This may be indicated by

The loop succeeds for ChanId=1, BUT reports "CH0 Initialised"... and so on... repeat

Try:

for (ChanId=0; ChanId < NUM_CHANNELS; ChanId  ) 
{
    int result = Initialise(ChanId, DeviceNames[ChanId]);
    fds[ChanId] = result;
    if (result == -1) 
    {
        syslog(LOG_INFO, "Failed to initialise RT Ch%d", ChanId);           
        return -1;
    }
    else
    {   
        syslog(LOG_INFO, "Ch%d Initialised", ChanId);           
    }
}

Other than that, is it possible that ChanId is set eleswhere and so replacing it with a local variable may work?

  • Related