需要帮助修复Segmentation Fault

时间:2016-04-21 06:38:29

标签: c pointers fault seed

我让它在一秒钟前工作但不小心弄坏了它。任何人都可以帮我修复它吗?我收到了分段错误,所以我想我在某些时候搞砸了指针。它应该根据用户输入生成一堆随机数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    unsigned int mySeed; // creates our variables
    unsigned int taps[2];
    unsigned int temp[2];
    unsigned int myToggle;

    FILE *fp;//opens the file
    fp = fopen("random.txt", "w");//sets the file to open equal to this file
    int TapInputs = 1;
    int count = 0;
    int tap;
    int myNewNumber = 0;
    mySeed = atoi(argv[1]);

    if(atoi(argv[1]) > 0) //Error checking for negative inputs.
    {
    printf("Please enter the taps you'd like to use : ");//prompts user to input the taps and then makes sure theyre in range
    while(TapInputs)
    {
        scanf("%d",&tap);
        if((tap > 0)&&(tap < 33))
        {
            *(taps+count)=tap;
        }
        else if(tap == -1) // when we find -1 we do this
        {
            TapInputs = 0;
        }
        else if(tap > 32)
        {
            exit(0);
        }
        count++;
    }
    printf("How many numbers do you want to generate: "); //prompts user to input the number of numbers to use
    scanf("%d", &myNewNumber);
    while (myNewNumber < 0)// error checking for positive inputs
    {
        printf("How many numbers do you want to generate: ");
        scanf("%d", &myNewNumber); 
    }
    printf("\nRandom Numbers:");
    while(myNewNumber)//creates number equal to the user input number in the previous step
    {
    temp[0] = mySeed; // makes temp1 the seed
    temp[1] = mySeed; // makes temp2 the seed
    temp[0] = (temp[0] >> taps[0]) & 1; // checks and sets the bit
    temp[1] = (temp[1] >> taps[1]) & 1; // checks and sets the bit
    myToggle = (temp[0] ^ temp[1]); // here we xor the temp1 and 2
    mySeed = (mySeed << 1) ^ myToggle; // use bittoggle to shift the seed and generate a new number

    fprintf(fp, "%d\r\n", mySeed); // wrties the generated number into the file

    printf("\n%d", mySeed); // prints the number
    myNewNumber -= 1;
    }
    fclose(fp); // closes file, creates a new line and returns 0 to the fucntion
    printf("\n");
    return 0;
    }
    else
    { // if the number the user input was 0 we will end our program
    exit(0);
    }
}

故障在执行时立即发生。

2 个答案:

答案 0 :(得分:0)

这段代码:

while(TapInputs)
{
    scanf("%d",&tap);
    if((tap > 0)&&(tap < 33))
    {
        *(taps+count)=tap;
    }
    else if(tap == -1) // when we find -1 we do this
    {
        TapInputs = 0;
    }
    else if(tap > 32)
    {
        exit(0);
    }
    count++;
}
执行

直到您发现TapInputsfalse,或者换句话说0。仅当您将-1作为scanf("%d", &tap)的输入时,才会发生这种情况。在此之前,您将继续阅读并 递增 count

但是上面的一些行,你已经声明了

unsigned int taps[2];

并在while循环中执行

*(taps+count)=tap;

因此,如果您已经阅读tap足够的时间并在033之间继续查找,那么在找到它-1之前,count将会增加到足以使你的阵列超出范围。

答案 1 :(得分:0)

我认为问题在于这一行,mySeed = atoi(argv [1]); 你必须做这样的事情, 你可以把它的代码放在if条件中,

if(agrc>1)
{
    mySeed = atoi(argv[1]);
    ---------------------
    --------------------     
}
I have tested...it is working


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
unsigned int mySeed; // creates our variables
unsigned int taps[2];
unsigned int temp[2];
unsigned int myToggle;

FILE *fp;//opens the file
fp = fopen("random.txt", "w");//sets the file to open equal to this file
int TapInputs = 1;
int count = 0;
int tap;
int myNewNumber = 0;
if(agrc>1)
{
mySeed = atoi(argv[1]);

if(atoi(argv[1]) > 0) //Error checking for negative inputs.
{
printf("Please enter the taps you'd like to use : ");//prompts user to input the taps and then makes sure theyre in range
while(TapInputs)
{
    scanf("%d",&tap);
    if((tap > 0)&&(tap < 33))
    {
        *(taps+count)=tap;
    }
    else if(tap == -1) // when we find -1 we do this
    {
        TapInputs = 0;
    }
    else if(tap > 32)
    {
        exit(0);
    }
    count++;
}
printf("How many numbers do you want to generate: "); //prompts user to input the number of numbers to use
scanf("%d", &myNewNumber);
while (myNewNumber < 0)// error checking for positive inputs
{
    printf("How many numbers do you want to generate: ");
    scanf("%d", &myNewNumber); 
}
printf("\nRandom Numbers:");
while(myNewNumber)//creates number equal to the user input number in the previous step
{
temp[0] = mySeed; // makes temp1 the seed
temp[1] = mySeed; // makes temp2 the seed
temp[0] = (temp[0] >> taps[0]) & 1; // checks and sets the bit
temp[1] = (temp[1] >> taps[1]) & 1; // checks and sets the bit
myToggle = (temp[0] ^ temp[1]); // here we xor the temp1 and 2
mySeed = (mySeed << 1) ^ myToggle; // use bittoggle to shift the seed and generate a new number

fprintf(fp, "%d\r\n", mySeed); // wrties the generated number into the file

printf("\n%d", mySeed); // prints the number
myNewNumber -= 1;
}
fclose(fp); // closes file, creates a new line and returns 0 to the fucntion
printf("\n");
return 0;
}
else
{ // if the number the user input was 0 we will end our program
exit(0);
}
}
}