I was trying a test and I wrote this program...
#include<iostream>
using namespace std;
main()
{
int arr[5]={1,2,3,5,3}, num=5;
for(int i=0; i< num; i++)
{
for(int j=(i+1); i< num; j++)
{
if (arr[i]==arr[j])
{
cout<<"test";
}
cout<<j<<endl;
}
}
}
hmmm...
In the below code I know I used "i" instead of "j",So just to create the problem I used this
for(int j=(i+1); i< num; j++)
And my problematic part of the program is-
for(int j=(i+1); i< num; j++)
{
if (arr[i]==arr[j])
{
cout<<"test";
}
cout<<j<<endl;
}
in the above code when I remove this if block-
if (arr[i]==arr[j])
{
cout<<"test";
}
the program goes infinite.........
But when I put this if block again the program terminates automatically after some execution.
I also read about this on this link but it shows example of java
And there they show the reason of negative value, but in my program there is no garbage value comes.
The loop condition is never false. Hence the program will continue running indefinitely.
However, with if (arr[i]==arr[j]) you access outside the bounds of arr. Hence the behaviour of the program is undefined. When the behaviour of the program is undefined, it doesn't necessarily behave as you might expect. For example, it might not continue running indefinitely.
Even without if (arr[i]==arr[j]), the loop will eventually have undefined behaviour when the loop counter overflows. But a program with undefined behaviour doesn't necessarily behave as you might not expect. For example, it might continue running indefinitely.
Without the if (arr[i]==arr[j]), you simply have an infinite loop, which is perfectly valid in this case. j will just keep getting incremented (eventually this will overflow, which in undefined behaviour, see [basic.fundamental]/2), and the condition will never be met, since i does not change.
However, with the if (arr[i]==arr[j]) in the code, j is being incremented, and you will go outside the bounds of the array (as you know). In C++, this is Undefined Behaviour, meaning anything can happen. In your case, it seems to be causing the program to crash (which is actually a good thing, since it indicates an error).
I think, the problematic part of code is
for(int j=(i+1); i< num; j++)
You are increasing j while you are checking i< num.
If you replace it with
for(int j=(i+1); j < num; j++)
it might start working.
Why it is terminating with if block, and not terminating without if block
if (arr[i]==arr[j])
{
cout<<"test";
}
Because int have finite amount of possible values. When you reach the maximal value of int variable, the value + 1 is the smallest possible int. (As #user17732522 pointed out, the behavior might be utterly different - program crashing or something nasty happening here - because behavior of int 'overflowing' is undefined. Therefore your programs memory might be already corrupted here and it need to perform some operations to see it happened...eg. by program crash.)
Now, let's think about, what arr[i] does. arr is basically a pointer to a memory, where array begins and [i] operation is the same thing as *(arr + i).
Now, there are two possibilities what might happen
Program crashes when i is positive and outside of the array
Program crashes when i is negative
In both cases, the program crashes, because you are trying to access protected or nonexistent zone of a memory. This depends on the architecture of the system - Eg. In negative zone on 64 bit AMD64 system, you are clearly accessing nonexistent memory, if you don't have 16 exabytes of RAM. The program must in such case cause processor interruption, which is given to your OS, which kills your application.
Why it doesn't crashes when you delete the block? Nobody is trying to access the *(arr + j) and therefore, j is repeating from minimal possible value to maximal possible value without any problem.
The problem is this if statement will always be true:
for(int j=(i+1); i< num; j++){infinity}
Try setting it to this instead:
for(int j=(i+1); j< num; j++){not infinity}
You can't pass a loop on a statement that will not eventually return false or it'll go on forever.
We added some new code in our PNG decoding routines for our game engine. The additional chunk defined is just there to read some values -- no big deal.
On Visual C++, it compiles just fine. On GCC, which is what we primarily use, we now get a strange issue that has never happened before:
compile_problems
This is the added code:
/* read grAb chunk */
png_unknown_chunk *unknowns;
int num_unknowns = png_get_unknown_chunks(png_ptr, info_ptr, &unknowns);
for (int i = 0; i < num_unknowns; i++)
{
if (!memcmp(unknowns[i].name, "grAb", 4))
{
png_grAb_t *grAb = reinterpret_cast<png_grAb_t *>(unknowns[i].data);
grAb->x = EPI_BE_S32(grAb->x) + 160 - width / 2;
grAb->y = EPI_BE_S32(grAb->y) + 200 - 32 - height;
img->grAb = grAb;
break;
}
}
Looks just fine to me. This is the only thing added to our original file. The complete file is here:
goto Line 59 of image_data.cc
And the function where this bombs out:
image_data_c *PNG_Load(file_c *f, int read_flags)
I don't understand what could be happening, as this worked perfectly fine before and never had issue with cross-initialization or our case handling.
If I could get some help, I would really appreciate it!
The errors seem pretty clear: there are jumps to label failed: from before the initialization of int num_unknowns to after it, so that int will have garbage values. This is forbidden in C++ ( but not in C).
One solution is to put
int num_unknowns = 0;
at the beginning of the function, and change the third line of the code sample you posted to just an assignment to num_unknowns.
Another solution is to instruct GCC to allow this, with the -fpermissive option, as the error itself indicates.
I was wondering how to customize my visual studio to code faster. I'm begginer, but saw few awesome things like autofilling for's or auto brackets when someone typed if/for/while etc.
Ex. When I'll type for it will fill it similar to this :
for(int i = size_T ; i< lengh ; i++)
{
}
I have a simple piece of code that generates random graphs by considering all possible edges and randomly selecting which ones to add to a file (based on a parameter p - the chance for each edge to be added). The algorithm looks like this:
for (unsigned int i = 0; i < nodeCount; ++i)
{
for (unsigned int j = i+1; j < nodeCount; ++j)
{
uniform_int_distribution<> d(0, 999999);
int randNum = d(randEngine);
if (randNum < p * 1000000)
{
//code that adds the edge to the file
}
}
}
I used to compile this code on vs2010, and when creating graphs with 50000 nodes and p=0.00002 it took ~26 seconds. I now upgraded to vs2012, compiled, and I get ~74 seconds! Almost triple the time!
I isolated the part that runs slower - it seems to be the random number generation (I commented out the code that is not included above, which writes to the file, and the same times were recorded).
The project definitions are exactly the same (except for the platform - vs100 vs. vs110). Any ideas why the random number generator has become so much worse in VS2012?? Or is it me that's doing something wrong?
Either way - what can I do except for going back to vs2010...?
Thanks.
I have been writing a code for a neural network using back propagation algorithm and for propagating inputs I have written the following code,but just for two inputs,its displaying segmentation fault.Is there any wrong withe code.I wan not able to figure it out....
void propagateInput(int cur,int next)
{
cout<<"propagating input"<<cur<<" "<<next<<endl;
cout<<"Number of nerons : "<<neuronsInLayer[cur]<<" "<<neuronsInLayer[next]<<endl;
for(int i = 0;i < neuronsInLayer[next];i++)
{
neuron[next][i].output = 0;
for(int j = 0;j < neuronsInLayer[cur];j++)
{
cout<<neuron[cur][j].output<<" ";
cout<<neuron[next][i].weight[j]<<"\n";
neuron[next][i].output += neuron[next][i].weight[j] * neuron[cur][j].output;
}
cout<<"out["<<i<<"] = "<<neuron[next][i].output<<endl;
}
cout<<"completed propagating input.\n";
}
for(int i = 0;i < neuronsInLayer[next];i++)...
neuronsInLayer[next] is a pointer. perhaps if i knew the type of neuronsInLayer i could assist you more.
That is not anywhere near enough information to debug your code. No info about line numbers or how the structures are laid out in memory or which ones are valid, etc.
So let me tell you how you can find this yourself. If you're using a Unix/Mac then use the GDB debugger on your executable, a.out:
$ gdb a.out
> run
*segfault*
> where
Visual Studio has a great debugger as well, just run it in Debug mode and it'll tell you where the segfault is and let you inspect memory.