在哪里放置动态变量的运算符

时间:2018-02-12 03:48:11

标签: c++ pointers memory-leaks delete-operator

我想弄清楚将delete pointer语句放在下面的程序中的哪个位置。我想清除pointer指向的内存空间,以避免内存泄漏。似乎无论我把它放在哪里,我都会收到错误声明:

main(8282,0x7fff95d823c0) malloc: *** error for object 0x7fff582d3960: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

我不知道如何解决这个问题。任何帮助表示赞赏。

完整代码:

#include <iostream>
#include <vector>
using namespace std;

int main() {

    //initialize vector
    vector<int> historyValues;

    //initialize pointer and int variable
    int *pointer;
    pointer = new int;
    pointer = 0;
    int currentValue;

    //make pointer point to the address of currentValue
    pointer = &currentValue;

    //increment pointer by 1 for a total of 10 times.
    //since pointer is pointing at currentValue, currentValue should change also.
    //push back the current value of currentValue into the vector.

    for (int i = 0; i < 10; i++) {

        *pointer += 1;
        historyValues.push_back(currentValue);
    }

    //print final results
    cout << "currentValue: " << currentValue << endl;
    cout << "*pointer: " << *pointer << endl;

    cout << "History of integers stored in currentValue: ";

    for (int i = 0; i < historyValues.size(); i++) {

        cout << historyValues[i] << " ";
    }

    cout << endl;
    cout << "Program finished" << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:3)

此程序中$ identify pd0.pdf **** Error reading a content stream. The page may be incomplete. **** File did not complete the page properly and may be damaged. **** Error reading a content stream. The page may be incomplete. **** File did not complete the page properly and may be damaged. **** Error reading a content stream. The page may be incomplete. **** File did not complete the page properly and may be damaged. **** This file had errors that were repaired or ignored. **** Please notify the author of the software that produced this **** file that it does not conform to Adobe's published PDF **** specification. pd0.pdf[0] PBM 612x792 612x792+0+0 16-bit Bilevel Gray 61KB 0.000u 0:00.000 pd0.pdf[1] PBM 612x792 612x792+0+0 16-bit Bilevel Gray 61KB 0.000u 0:00.000 pd0.pdf[2] PBM 612x792 612x792+0+0 16-bit Bilevel Gray 61KB 0.000u 0:00.000 引用可释放的动态内存块的唯一位置是$ gsnd -dPDFDEBUG pd0.pdf GPL Ghostscript 9.18 (2015-10-05) Copyright (C) 2015 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. << /Root 1 0 R /Size 12 >> %Resolving: [1 0] << /Type /Catalog /Pages 2 0 R >> endobj %Resolving: [2 0] << /Kids [ 3 0 R 6 0 R 9 0 R ] /Type /Pages /Count 3 >> endobj %Resolving: [3 0] << /Parent 2 0 R /Contents [ 5 0 R ] /MediaBox [ 0.0 0.0 612.0 792.0 ] /Resources << /Font << /F1 4 0 R >> /ProcSet [ /PDF /Text ] >> /Type /Page >> endobj %Resolving: [6 0] << /Parent 2 0 R /Contents [ 8 0 R ] /MediaBox [ 0.0 0.0 612.0 792.0 ] /Resources << /Font << /F2 7 0 R >> /ProcSet [ /PDF /Text ] >> /Type /Page >> endobj %Resolving: [9 0] << /Parent 2 0 R /Contents [ 11 0 R ] /MediaBox [ 0.0 0.0 612.0 792.0 ] /Resources << /Font << /F3 10 0 R >> /ProcSet [ /PDF /Text ] >> /Type /Page >> endobj %Resolving: [1 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [1 0] %Resolving: [1 0] %Resolving: [1 0] %Resolving: [1 0] %Resolving: [2 0] Processing pages 1 through 3. Page 1 %Resolving: [1 0] %Resolving: [2 0] %Resolving: [3 0] %Resolving: [3 0] %Resolving: [3 0] %Resolving: [3 0] %Resolving: [3 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [5 0] << /Length 15660 >> stream %FilePosition: 471 endobj BT F1 10.0 Tf %Resolving: [4 0] << /Type /Font /SubType /Type1 /BaseFont /Palatino-Roman >> endobj **** Error reading a content stream. The page may be incomplete. **** File did not complete the page properly and may be damaged. Page 2 %Resolving: [1 0] %Resolving: [2 0] %Resolving: [3 0] %Resolving: [6 0] %Resolving: [6 0] %Resolving: [6 0] %Resolving: [6 0] %Resolving: [6 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [8 0] << /Length 31667 >> stream %FilePosition: 16474 endobj BT F2 10.0 Tf %Resolving: [7 0] << /Type /Font /SubType /Type1 /BaseFont /Palatino-Roman >> endobj **** Error reading a content stream. The page may be incomplete. **** File did not complete the page properly and may be damaged. Page 3 %Resolving: [1 0] %Resolving: [2 0] %Resolving: [3 0] %Resolving: [6 0] %Resolving: [9 0] %Resolving: [9 0] %Resolving: [9 0] %Resolving: [9 0] %Resolving: [9 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [1 0] %Resolving: [2 0] %Resolving: [11 0] << /Length 8335 >> stream %FilePosition: 48487 endobj BT F3 10.0 Tf %Resolving: [10 0] << /Type /Font /SubType /Type1 /BaseFont /Palatino-Roman >> endobj **** Error reading a content stream. The page may be incomplete. **** File did not complete the page properly and may be damaged. **** This file had errors that were repaired or ignored. **** Please notify the author of the software that produced this **** file that it does not conform to Adobe's published PDF **** specification. GS> pointer之间如果您在pointer = new int;之间移动pointer = 0;你会没事的。

改变的结果是:

delete pointer

但是,您也可以删除所有这三行,然后从pointer = new int; delete pointer; pointer = 0; 开始,因为您的代码从不使用动态分配的pointer = &currentValue;

另外,你的评论&#34;增加指针&#34;是不正确的。您正在递增指针的目标,而不是指针。