C++ Index variable changing radically in Debug Version [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have found a lot of answers that have to do with release version problems but none with the exact opposite.
I have a loop similar to the following:
while(index < 7 && FlagIsUp)
{
// process
Inner Loop
Inner Inner Loop
Array[index] = number;
++index;
}
Problem is that index changes radically from 6 (the last iteration) to 17209 for an int16_t and 1133165442 for size_t. NOTHING IN THE LOOP changes the index except the ++index. I replaced the while with a for and it still happens.
It only happens in debug mode, in release version it does finish without issues.
I also added volatile to the index and results were the same, it still overflowed.
Any ideas, pointers, would be appreciated. I can't provide a working copy of the bug so any theories are welcomed, I want to exhaust my options to find the problem.
EDIT:
Yes I'm sorry. I gave to little information.
First off I'm working with QNX Momentics Version: 4.6.0 and my debugger is part of the GNU Compiler Collection 4.3.3.
Now the inner loop is this:
cSignalNoIndex = 0;
while ((cSignalNoIndex < (2 * NO_PHASES + 1)) && !ShutDownFlag)
{
wSF0 = 0;
wExtSF = 0;
dwSFAcc = 0;
dwExtSFAcc = 0;
std::string SignalNo= " Waveform number " + Tool::toString(cSignalNoIndex);
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Prepare Input vectors for FFT compute
cComponent = 0;
while (cComponent < (HCYCLE_SAMPLES << 1))
{
awReal[cComponent] = static_cast<int>(Results[cSignalNoIndex].WaveForm[cComponent/64][cComponent % 64]);
awImg[cComponent] = 0;
pwSource++;
cComponent++;
}
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Get FFT (forward)
// Changed the wPwr from 7 to something else
iFft(&awReal[0], &awImg[0], wPwr, 1);
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Compute magnitudes
//fMult = pInBlock3->fMult[cSignalNoIndex]; // Get Multiplier
fMult = 1;
for (cComponent = 0; cComponent < HCYCLE_SAMPLES && !ShutDownFlag; cComponent++)
{
int64_t dlOp = static_cast<int64_t>(awReal[cComponent]) * awReal[cComponent] + static_cast<int64_t>(awImg[cComponent]) * awImg[cComponent];
dlOp <<= 1; // Apply sqrt(2) term to result
dlOp = static_cast<int>(fMult * isqrt64(dlOp));
// Store into FFT object
oFFTMag3.wFFT[cSignalNoIndex][cComponent] = static_cast<int16_t>( dlOp );
// Set Base frequency magnitude and accumulate harmonics
if (cComponent == 1) // Base
{
wSF0 = dlOp;
if(cSignalNoIndex == 6)
{
wRefMagnitude = static_cast<int16_t> ( 0.4 * wSF0 );
}
if(awReal[1] != 0) // Also get phase for Base
{
dfPhase = std::atan((double)((float)awImg[1]/awReal[1])) * 180.0 / PI;
}
else
{
if(awImg[1] >= 0)
{
dfPhase = 90.0;
}
else
{
dfPhase = -90.0;
}
}
if(awReal[1] < 0) // convert to 2*PI range
{
dfPhase += 180.0;
}
else if(awImg[1] < 0)
{
dfPhase += 360.0;
}
//// THIS IS THE LINE
fPhase[cSignalNoIndex] = dfPhase; ////////// WTF! cSignalNoIndex = 6 - cComponent = 2
/// HERE cSignalNoIndex is overflown
}
}

You haven't really posted enough code but my best bet is Array[index] = number overwrites index at some point. The fact that it only happens sometimes (in your case, when debugging) is a good example of "undefined behavior".

Related

Overlapping multidimensional array corruption [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a very insidious issue with two dynamically allocated and reallocated multidimensional arrays, defined as follows:
int *pRawVals[20][181];
and
int *pMaxPlaneRawPlanes[20][46];
These are allocated in a single function: (code condensed for clarity)
...
// iNlat and iNVer bounds checked and OK
for (int lat = 0; lat < iNlat; lat++)
{
pData->pBlendedRawPlanes[lat] = new long long[1 * (iNver + 1)];
for (int sensor = 0; sensor < 20; sensor++)
{
pData->pRawVals[sensor][lat] = new int[1 * (iNver + 1)];
for (int vert = 0; vert <= iNver; vert++)
{
pData->pRawVals[sensor][lat][vert] = SENSOR_UNREAD;
} // end for
} // end for
} // end for
...
for (int sensor = 0; sensor < 20; sensor++)
{
delete[] pData->strayVals[sensor];
pData->strayVals[sensor] = new int[1 * (iNver + 1)];
for (int vert = 0; vert < iNver + 1; vert++)
{
pData->strayVals[sensor][vert] = SENSOR_UNREAD;
} // end for
for (int lat = 0; lat < 46; lat++)
{
pData->pMaxPlaneRawPlanes[sensor][lat] = new int[361];
// for (int iFillMem = 0; iFillMem < 361; iFillMem++)
for (int vert = 0; vert < iNver; vert++)
{
pData->pMaxPlaneRawPlanes[sensor][lat][vert] = SENSOR_UNREAD;
} // end for
} // end for
} // end for
... and deallocated in another function:
for (int lat = 0; lat < pData->iNUserLats; lat++) // iNUserLats <= 181
{
for (int sensor = 0; sensor < 20; sensor++)
{
if (pData->pRawVals[sensor][lat] != NULL)
{
delete[] pData->pRawVals[sensor][lat];
pData->pRawVals[sensor][lat] = NULL;
} // end if
} // end for
}
...
for (int sensor = 0; sensor < 20; sensor++)
{
for (int lat = 0; lat < 46; lat++)
{
if (pData->pMaxPlaneRawPlanes[sensor][lat] != NULL)
{
delete[] pData->pMaxPlaneRawPlanes[sensor][lat];
pData->pMaxPlaneRawPlanes[sensor][lat] = NULL;
} // end if
} // end for
} // end for
Hope I got enough of the code posted. At any rate, when deallocating pMaxPlaneRawPlanes on a second pass through the code, it turns out that certain elements of that array overlap elements of pRawVals and the deallocation causes a crash - appears that those locations may have been freed previously but I can't seem to point the finger at exactly how it's happening.
Any hints on how to narrow that kind of thing down? I run development on Windows 7 Ultimate 64 bit, but the app is compiled for 32 bit.
Thanks
You may go out of range in any of your array, or you may go out of range elsewhere.
If you are using Visual Studio 2019, or you are using gcc or clang, you can give ASan (Address Sanitizer) a try. (VS2019 link)
Otherwise you can try Application Verifier with Full Heap check enabled.
It is also possible to use custom allocation and allocate memory the way that it will access violation on out of bounds, even if off by one. It is essentially duplicating Application Verifier for particular allocations.
Using std::array or std::vector recommended in comments will also help making fewer errors that lead to heap corruption in the first place. vector manages deallocations automatically, and also in debug builds may have checks for out-of-range errors.

Increment the value of a map

need your help and better if you can help me fast. It is very trivial problem but still can't understand what exactly i need to put in one line.
The following code i have
for (busRequest = apointCollection.begin(); busRequest != apointCollection.end(); busRequest++)
{
double Min = DBL_MAX;
int station = 0;
for (int i = 0; i < newStations; i++)
{
distance = sqrt(pow((apointCollection2[i].x - busRequest->x1), 2) + pow((apointCollection2[i].y - busRequest->y1), 2));
if (distance < Min)
{
Min = distance;
station = i;
}
}
if (people.find(station) == people.end())
{
people.insert(pair<int, int>(station, i));
}
else
{
how can i increment "i" if the key of my statation is already in the map.
}
}
Just briefly what i do , i take the first busrequest go to the second loop take the first station and find the minimum distance. After i go over the second loop , i add that station with minimum distance to my map . After i proceed with all my loops and if there is the same station , i need to increment it , so it means that that station is using two times and etc.
I need the help just give me hint or provide the line that i need to add.
I thank you in advance and waiting for your help.
And I think you meant Min Distance instead of i? Check and let me know.
for (busRequest = apointCollection.begin(); busRequest != apointCollection.end(); busRequest++)
{
double Min = DBL_MAX;
int station = 0;
for (int i = 0; i < newStations; i++)
{
distance = sqrt(pow((apointCollection2[i].x - busRequest->x1), 2) + pow((apointCollection2[i].y - busRequest->y1), 2));
if (distance < Min)
{
Min = distance;
station = i;
}
}
if (people.find(station) == people.end())
{
people.insert(pair<int, int>(station, i)); // here???
}
else
{
// This routine will increment the value if the key already exists. If it doesn't exist it will create it for you
YourMap[YourKey]++;
}
}
In C++ you can directly access a map key without inserting it. C++ will automatically create it with default value.
In your case, if a station is not present in people map and you will access people[station] then people[station] will automatically be set to 0 ( default value of int is 0 ).
So you can just do this:
if (people[station] == 0)
{
// Do something
people[station] = station; // NOTE: i is not accessible here! check ur logic
}
else
{
people[station]++;
}
Also: In your code i cannot be accessed inside IF condition to insert into people map.

No idea where seg fault is occuring [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
For some reason, when I run this code, I get a seg fault when the value of i in the for-loop is 7654319. However the strange thing is that when I am not checking if the value is pan-digital, it works normally without a segfault. It also works when I am checking if it is just pandigital; but not for both. I used gdb to step through the code and here is the output I get:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004007d3 in main () at Pand.cc:81
81 if (isPandigital(i) && Primes[i])
6: Primes[i] = <error: Cannot access memory at address 0x7ffefffffff4>
5: i = <error: Cannot access memory at address 0x7ffefffffff4>
4: Primes[7654317] = <error: Cannot access memory at address 0x7ffefffffff8>
3: Primes[7654321] = <error: Cannot access memory at address 0x7ffefffffff8>
2: Primes[7654319] = <error: Cannot access memory at address 0x7ffefffffff8>
1: Primes = <error: Cannot access memory at address 0x7ffefffffff8>
From the output, it seems that by manipulating the value of i in the isPandigital(int) function, this also affects value of i in main. This didn't make any sense to me, but I went ahead and used a different variable to represent i in the isPandigital(int) function, but I still get the same error.
Can someone help me please? These kind of errors are so annoying because everything seems like it should be working, but it's not and the solution is just hiding itself under layers of implementation. Any help is appreciated!
#include <cstdio>
#define MAX 7700000
typedef unsigned int uint;
bool* GetPrimes()
{
const int Need = MAX;
bool* Sieve = new bool[Need];
for (int s = 0; s < Need; ++s)
Sieve[s] = 1;
bool Done = false;
uint w = 3;
while (!Done)
{
for (uint q = 3, Prod = w * q; Prod < (uint)Need ; q += 2, Prod = w * q)
Sieve[Prod] = false;
Done = (w > (Need >> 1) ? true : false);
w+=2;
}
return Sieve;
}
bool isPandigital(int num)
{
int arr [] = {1,2,3,4,5,6,7}, G, count = 7;
do
{
G = num%10;
if (arr[G-1])
--count;
arr[G-1] = 0;
} while (num/=10);
return (!count);
}
int main()
{
bool* Prime = GetPrimes();
int i;
for (i = 7654321 ;i > 2; i-=2)
{
if (Prime[i] && isPandigital(i))
break;
}
printf("%d\n", i);
return 0;
}
In your isPandigital function. Notice that if num is a multiple of ten or congruent to 8 or 9 mod 10, you'll have a few problems. Out-of-bounds array accesses often lead to segfaults.
The first prime for which this occurs is 19 (or 7654319 if you go backwards from 7654321):
bool isPandigital(int num)//num is (76543)19
{
int arr [] = {1,2,3,4,5,6,7}, G, count = 7;
do
{
G = num%10; //G is 9
if (arr[G-1]) //G-1 is 8; G is only indexed from 0 to 6.
--count;
arr[G-1] = 0; //G-1 is 8; G is only indexed from 0 to 6.
} while (num/=10);
return (!count);
}
Note that though the solution will not have an 8 or 9 in it, any prime you test might.
Look at:
G = num%10;
if (arr[G-1])
So, what if G is zero? This would also trash your stack, making debug hard.
On the face of it, isPandigital works nicely in the case when the number passed is pan-digital, else has an array bound under/overrun?

Why is my int a pointer? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My code produces this error:
ISO C++ forbids comparison between pointer and integer [-fpermissive]
I have commented out the body of my code which doesn't have an impact what is causing the error. The code is a section of a program I wrote to calculate Newton's Method. I really do not know very much about pointers. I am trying to avoid them right now. All I want to do is stop my while loop if it runs too many times.
int iter = 0;
while (abs(nextValue - currValue) > 0.00000000001) and iter < 100000;
{
// currValue = nextValue;
//
// double polyValue = 0;
// int n3;
// for (n3 = degree; n3 >= 0; n3--)
// {
// polyValue += coef[n3] * pow(currValue, n3);
// }
// double polynomial = polyValue;
//
// polyValue = 0;
// int n4;
// for (n4 = degree; n4 >= 1; n4--)
// {
// polyValue += coef[n4] * n4 * pow(currValue, n4 - 1);
// }
// double polyPrime = polyValue;
//
// nextValue = currValue - (polynomial / polyPrime);
iter += 1;
}
The while condition should probably read as:
while (abs(nextValue - currValue) > 0.00000000001 && iter < 100000)
Note that
There is no semicolon at the end.
The entire condition must be in parentheses.
and is replaced by && - this is not strictly necessary because and is valid in C++ as far as I know, but I have never seen and being used in production code so far.

Professionalise this code [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I have been programming for about 3 years now and feel confident in my skills. But recently I began working alongside embedded systems and working on other peoples code and have begun to question how good my code is.
I see all these complex answers on SO and think I would have done that with a vector and if statements and wonder if I am any more than a beginner as I was self taught and don't really know my level.
So I was wondering if more experienced programmers could show me ho to do things better.
This is code I wrote to for an rpg game to pick a target to attack. using it as an example could you show me better/more advanced/professional ways of doing it.
int FindTarget(Player &player);
{
int aimPoint[5] = 0;
for(int i = 0; i <= 5; i++)
{
if(player.team[i].exist == true)
{
// set random starting point between 1 - 3
aimPoint[i] = random /3;
// add a point if you hve an elemental advantage minus if not
if(player.team[i].type == weak)
{
aimPoint += 1;
}
else if(player.team[i].type == strong)
{
aimPoint -= 1;
}
//add for front row minus for back
if(i == 1 || i== 3)
{
aimPoint += 1;
}
else
{
aimPoint -= 1;
}
}
}
return 0;
}
EDIT: If you don't have the time or effort to show me examples I would appreciate a good book that a beginner/intermediate could understand.
Comment #1:
This line starts at 0, and goes exactly once? Why is it a loop?
for(int i = 0; i <= 0; i++)
Comment #2:
Don't compare a boolean against true.
if(player.team[i].exist == true)
Just re-write it to:
if(player.team[i].exist)
Comment #3:
Professionals rarely use hardcoded values.
Why is this value 5?
int aimPoint[5] = 0;
Instead, make it clear:
int aimPoint[TARGET_SIZE] = 0;
Similarly, change
aimPoint[i] = random /3;
to:
aimPoint[i] = random /INITIAL_TARGET_VALUE;
This is pointless; it's not a loop, it's a single pass.
for(int i = 0; i <= 0; i++)
ahem since nobody mentioned it, let me point this one out:
int FindTarget(Player &player);
{
int aimPoint[5] = 0;
...
return 0
}
to roughly
void FindTarget(const Player &player)
{
std::vector<int> aimPoint(5, 0);
...
}
Also, since there is no knowing what the code should do (and how 'aimPoint' is related to teams; guessing doesn't help because none of it is used, aimPoint is discarded?), I don't have anything else than fixing the obvious breakage that was above
---- Edit from a comment
The handling of random seems misguided. Someone suggested that you might have meant random %3 + 1;
I noted that too but decided there is nothing to base the assumption on. Perhaps random is already an int in the range [3, 12).
Also, random % 3 won't yield a uniform distribution, so you'd need to do something else
For many applications, rand() will perform admirably when used correctly, but with the current sad state of affairs, rand() is very rarely used correctly.
The problem is that of distribution
Here is how I would write it.
void FindTarget(Player &player);
{
int aimPoint[5];
for(int i = 0; i <= 5; ++i)
{
if(!player.team[i].exist)
continue;
// set random starting point between 1 - 3
aimPoint[i] = random / 3;
// add a point if you hve an elemental advantage minus if not
switch(player.team[i].type) {
case weak:
++aimPoint[i];
break;
case strong:
--aimPoint[i];
break;
}
//add for front row minus for back
if(i == 1 || i== 3)
++aimPoint[i];
else
--aimPoint[i];
}
}
That piece of code:
// set random starting point between 1 - 3
aimPoint[i] = random /3;
don't get you random number between 1 and 3, try this:
aimPoint[i] = random % 3 + 1