munmap_chunk():无效指针中止(核心转储)

问题描述 投票:0回答:1

运行此命令从 argv[1] 读取数字,该文件会导致“munmap_chunk():无效指针中止(核心转储)” 仅当第一行包含两位或更多位数字时才会出现此错误,例如,以下内容不会崩溃:

9

10

11

12

13

14

15

16

17

18

19

但这会崩溃 10

10

11

12

13

14

15

16

17

18

19

20

//-------------------------- READ------------------------------////-------------------------- READ------------------------------//
            if (command == "Read"){
                cout << command << endl;
                ifstream inputFile(argv[1]);

                if (!inputFile){
                    cout << "CANNOT READ" << endl;
                    continue;
                }

                else{
                    string line; 
                    getline(inputFile, line);
                    number_of_elements = stoi(line);
                    
                    int count = 1;
                    vector<int> key_values(number_of_elements);                    
                    pHeap= new HEAP;
                    pHeap->size = 0;
                    pHeap->capacity = number_of_elements;  
                    
                    while(getline(inputFile, line)){
                        if(count == number_of_elements + 1){
                            break;
                        }
                        else if(line.length() == 0){
                            if(count != number_of_elements)
                                continue;
                            else
                                break;
                        }
                        else if(line.length() > 0){
                            key_values[count] = stod(line);
                            ++count; 
                        }                
                    V = new ELEMENT*[number_of_elements];

                    for (int i = 1; i <= number_of_elements; i++){
                        V[i] = new ELEMENT;
                        V[i]->index = i;
                        V[i]->key = key_values[i];
                        V[i]->pos = 0;
                    }
                }
            }

改变

V = new ELEMENT*(number_of_elements * sizeof(ELEMENET));
解决了这个问题,但又产生了其他问题,所以我认为这是更容易解决的问题。

c++
1个回答
0
投票

使用

key_values
V
都可以循环索引 1..n,其中 n 是元素数量。

C++ 从 0..(n-1) 开始计算索引。

© www.soinside.com 2019 - 2024. All rights reserved.