【问题标题】:Complete Beginner: Null pointer issue?完全初学者:空指针问题?
【发布时间】:2012-03-03 09:40:10
【问题描述】:

我查看了所有论坛以尝试了解此问题。我无法完全理解这个问题以及我找不到解决方案的原因是因为我对 C++ 还很陌生,而且我不理解错误消息。

这是我的 C++ 代码,它从排列或组合公式中找到可能性的数量。每次我尝试编译和运行时,我都会收到这样的消息:

0x6a8613af (msvcr100d.dll) 中的第一次机会异常 Combinations_Permutations.exe:0xC0000005:读取访问冲突 位置 0x00000005。 0x6a8613af (msvcr100d.dll) 处未处理的异常 在 Combinations_Permutations.exe: 0xC0000005: 读取访问冲突 位置 0x00000005。

我在许多其他论坛上了解到“访问冲突读取位置 0x00...”肯定表示空指针。但是我看不到在哪里遇到了这样的空问题。也许我的变量正在全局访问,它们尚未初始化? 这是我的代码,我已经使用了一段时间......就像我说的我很新。所以请告诉我我的错误。谢谢。

我的代码:

    #include <iostream>
#include "conio.h";
using namespace std;

int run_combination(int n, int r);
int run_permutation(int n, int r);
int solve_factorial(int f);

int f_value = 1; //factorial value used recursively
int n_input, r_input;
char choice;
char order;
void main(){
            //if user types choice as 'q', while loop ends
    while(choice != 'q'){
        printf("How many values? (1-9) \n");
        printf("User: ");
        cin >> n_input;//user input for variable n
        printf("n_input: %i", n_input);

        printf("\nHow many will be chosen at a time out of those values? (1-9)\n");
        printf("User: ");
        cin >> r_input; //user input for variable r

        printf("\nDoes order matter? (y/n)\n");
        printf("User: ");
        cin >> order; //'y' if order is taken into consideration(permutation)
                            //'n' if order it NOT taken into consideration(combination)

        int solution = 0; //temporary variable that represents the solution after running
                          //n and r through the permutation or combination formula

        //if user input values for n and r are in between 1 and 9, then run 
                                                //combination or permutation
        if (n_input <= 9 && n_input >= 1 && r_input <= 9 && r_input >= 1){
            if (order == 'y')
                solution = run_permutation(n_input, r_input);
            else if (order == 'n')
                solution = run_combination(n_input, r_input);
            else
                printf("\nError. Please type 'y' or 'n' to determine if order matters.\n");

            //if n < r, run_permutation or run_combination returns 0
            if (solution == 0){
                printf("Error! You can't choose %i values at a time if there \n",
                    "are only %i total values. Type in new values next loop \n.", r_input, n_input);
            }
            else
                printf("Number of possibilities: %s", solution);
        }
        else{ //else error message if numbers are out of range...
            printf("Next loop, type in values that range from 1 to 9.\n");
        }

            //option 'q' to quit out of loop
            printf("Type 'q' to quit or enter any key to continue.\n");
            printf("User: ");
            cin >> choice;
    }

    _getch();
}

/*
Returns solved combination of parameters n and r
Takes the form: n! / r!(n-r)!
*/
int run_combination(int n, int r){
    if (n < r) //solution is impossible because you can't choose r amounnt at a time if r is greater than n
        return 0;
    int n_fac = solve_factorial(n); //n!
    int r_fac = solve_factorial(r); //r!
    int nMinusr_fac = solve_factorial(n-r); //(n-r)!

    int solve = ((n_fac) / ((r_fac)*(nMinusr_fac))); // plugging in solved factorials into the combination formula
    return solve;
}

int run_permutation(int n, int r){
    if (n < r)
        return 0;
    int n_fac = solve_factorial(n);
    int nMinusr_fac = solve_factorial(n-r); 

    int solve = ((n_fac) / (nMinusr_fac));  //plugging in factorials into permutation formula
    return solve;
}

int solve_factorial(int f){
    if (f-1==0 || f == 0){ //if parameter f is 1 or 0, return 1 
        int temp = f_value;
        f_value = 1; //reset f_value so that f_value remains 1 at the start of every new factorial
        return temp;
    }
    else{ //else multiply f_value by f-1
        f_value *= f;
        return solve_factorial(f-1);
    }
}

【问题讨论】:

  • 您现在开始学习如何使用调试器!
  • 再说一句。混合 C 类型函数 (printf) 和 C++ ioclases (cin) 是一种不好的风格。除非非常必要,否则不要混合它们。如果你使用 cout 而不是 printf 你会避免你的错误并且代码看起来会更好
  • main 返回 int 而不是 void,请参见 SO question
  • 一般来说,你会希望避免发布这样的代码墙。您应该花一些时间将问题缩小到重现问题的小测试用例中。在本练习的过程中,您很可能会自己发现错误。只需开始删除或注释掉代码,直到问题消失。或者使用调试器单步执行代码。祝你好运,编程愉快。

标签: c++ exception pointers null


【解决方案1】:

这是一个错误:

printf("Number of possibilities: %s", solution);

solution 是一个int,而不是一个以空结尾的字符串:使用%d

使用类型安全的std::cout 而不是printf(),可以防止出现此错误:

std::cout << "Number of possibilities: " << solution;

【讨论】:

    【解决方案2】:

    有问题的行是:

    printf("Number of possibilities: %s", solution);
    

    您告诉printf solutionchar*,因此它会尝试取消引用(char*)solution 以打印“C-string”的内容(大概是当solution 具有值@ 987654327@ 在您的特定错误消息的情况下)。

    %s 更改为%d,或使用std::cout 而不是printf 以获得类型安全性并首先避免此类问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-24
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2020-11-23
      相关资源
      最近更新 更多