For loop running only one time


Following for loop is running one time only .can someone spot what is the issue?

g_pShmBuf value is 4 and both i and g_pShmBuf are unsigned int. Rest all variables are unsigned int

for (i = 0; i < g_pShmBuf->cpus; i++)
g_sCoreCpuStat[i].uliCurrent = g_pShmBuf->cpuUsage[i].current;

// check if need reset the statistics
if (g_sCoreCpuStat[i].bResetFlag)
// reset the statistics
g_sCoreCpuStat[i].uliCount = 1;
g_sCoreCpuStat[i].bResetFlag = false;

g_sCoreCpuStat[i].uliSum = g_sCoreCpuStat[i].uliCurrent;
g_sCoreCpuStat[i].uliMax = g_sCoreCpuStat[i].uliCurrent;
g_sCoreCpuStat[i].uliAve = g_sCoreCpuStat[i].uliCurrent;
It looks OK from the information given. The way I see it, probably one of two things is happening:
(1) That the number of CPUs is, in fact, 1 and not 4.
(2) You are going out of bounds of one the arrays you are accessing, causing some sort of undefined behavior.

This is where using a debugger is a vital skill. With a debugger, you can step through your code line by line and pinpoint exactly where behavior starts to diverge from what you expect. Then you can look at the state of variables at that moment and see what exactly what data differs from what you expect.

Also, g_pShmBuf is a pointer; if it has the value of 4, then something is probably wrong. I know you meant "g_pShmBuf->cpus" but it's good to be precise in your details.

Edit: ALSO,
"g_pShmBuf" I take it this means "global pointer to a buffer of shared memory".
If you are sharing memory between different threads or processes, then perhaps something is going wrong there.
Last edited on
Registered users can post here. Sign in or register to post.