未初始化的局部变量并帮助纠正

时间:2012-02-16 16:13:06

标签: c++ sorting pointers dynamic

我正在学习指针和课堂上的new运算符。

在我的readArray函数中,我将读取一个大小。使用大小动态创建整数数组。然后将数组指定给指针,填充它,并返回大小和数组。

我相信我已经纠正并修复了该部分但是当我尝试对数组进行排序时,我得到错误“未初始化的局部变量temp。”

问题是,当我尝试初始化它时,我得到了这个错误。 任何帮助表示感谢谢谢。看到我的错误对我很有帮助。

#include <iostream>
using namespace std;

int* readArray(int&);
void sortArray(int *, const int * );

int main ()
{
    int size = 0;
    int *arrPTR = readArray(size);
    const int *sizePTR = &size;
    sortArray(arrPTR, sizePTR);

    cout<<arrPTR[1]<<arrPTR[2]<<arrPTR[3]<<arrPTR[4];

        system("pause");
        return 0;
}


int* readArray(int &size)
{
    cout<<"Enter a number for size of array.\n";
    cin>>size;
    int *arrPTR = new int[size];

    for(int count = 0; count < (size-1); count++)
    {   
         cout<<"Enter positive numbers to completely fill the array.\n";
         cin>>*(arrPTR+count);
    }

    return arrPTR;
}

void sortArray(int *arrPTR, const int *sizePTR)
{
    int *temp;
    bool *swap;

    do
    {
        swap = false;
        for(int count = 0; count < (*sizePTR - 1); count++)
        {
            if(arrPTR[count] > arrPTR[count+1])
            {
                *temp = arrPTR[count];
                arrPTR[count] = arrPTR[count+1];
                arrPTR[count+1] = *temp;
                *swap = true;
            }
        }
    }while (swap);
 }

4 个答案:

答案 0 :(得分:3)

你使temp成为一个int指针(uninitiialized),然后将它指向的东西(任何/没有)设置为arrPTR [ccount]。由于您只使用temp进行交换,因此它应该与被交换的类型相同,在这种情况下:int。

如果它绝对必须是一个指针(没有这个很好的理由,它很慢,令人困惑,增加了错误的可能性,并增加了内存泄漏的可能性) :

int *temp = new int; //make an int for the pointer to point at
bool *swap = new bool; //make an bool for the pointer to point at
do
{
    //your code
}while (swap);
delete temp;
delete swap;

答案 1 :(得分:2)

您将temp声明为指针。在解除引用并稍后分配之前,您需要在堆上分配它。但是,堆栈中的变量可能更可取?

仅供参考:您应该知道readArray中的内存泄漏,这会让呼叫者负责呼叫delete []

编辑:我希望这有助于解决其他一些问题。

#include <iostream>

int* readArray(int&);
void sortArray(int*, int);

int main ()
{
    int size(0); // use stack when possible
    int *arrPTR = readArray(size);
    sortArray(arrPTR, size);

    // arrays are zero based index so loop from 0 to size
    for (int index(0); index < size; ++index)
        std::cout << arrPTR[index];

    delete [] arrPTR; // remember to delete array or we have a memory leak!
    // note: because we did new[] for an array we match it with delete[]
    //       if we just did new we would match it with delete

    system("pause");
    return 0;
}

int* readArray(int& size)
{
    std::cout << "Enter a number for size of array.\n";
    std::cin >> size;
    int *arrPTR = new int[size]; // all news must be deleted!

    // prefer pre-increment to post-increment where you can
    for(int count(0); count < size; ++count)
    {   
         std::cout << "Enter positive numbers to completely fill the array.\n";
         std::cin >> arrPTR[count];
    }

    return arrPTR;
}

// passing size by value is fine (it may be smaller than pointer on some architectures)
void sortArray(int *arrPTR, int size)
{
    // you may want to check if size >= 2 for sanity

    // we do the two loops to avoid going out of bounds of array on last iteration
    for(int i(0); i < size-1; ++i) // the first to compare (all except last)
    {
        for(int j(i+1); j < size; ++j) // the second to compare (all except first)
        {
            // do comparison
            if (arrPTR[i] > arrPTR[j]) // from smallest to biggest (use < to go from biggest to smallest)
            {
                // swap if needed
                int temp(arrPTR[i]); // put this on stack
                arrPTR[i] = arrPTR[j];
                arrPTR[j] = temp;
            }
        }
    }
 }

答案 2 :(得分:1)

temp是一个“指向int的指针,你正在初始化。当你说*temp = ...时,你实际上正在分配temp发生的任何事情指点,但既然你没有告诉它指向什么,它可以写在程序的地址空间的任何地方。

由于您使用它们的方式,似乎tempswap根本不应该是指针,只有普通intbool

答案 3 :(得分:0)

当您取消引用它时,您没有初始化临时指针,而是在写入内存的随机部分。 Temp不需要是指针,它可以只是一个int。只需用temp。

替换* temp的每个实例