单位化局部变量和帮助纠正 [英] Unitialized local variable and help correcting

查看:141
本文介绍了单位化局部变量和帮助纠正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习指针和类中的 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]

系统(pause);
return 0;
}


int * readArray(int& size)
{
cout<<输入一个数组大小的数字。 n。
cin>> size;
int * arrPTR = new int [size];

for(int count = 0; count<(size-1); count ++)
{
cout<<输入正数以完全填充数组。 \\ 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])$ ​​b $ b {
* temp = arrPTR [count];
arrPTR [count] = arrPTR [count + 1];
arrPTR [count + 1] = * temp;
* swap = true;
}
}
} while(swap);
}


解决方案

c> temp 作为指针。您需要在解除引用之前在堆上分配它,并在稍后分配给它。



FYI:你应该注意 readArray 编辑

我希望这将有助于清除其他一些问题。

  #include< iostream> 

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

int main()
{
int size(0); //可能时使用堆栈
int * arrPTR = readArray(size);
sortArray(arrPTR,size);

//数组是基于零的索引从0到大小
for(int index(0); index< size; ++ index)
std :: cout << arrPTR [index];

delete [] arrPTR; //记得删除数组或者我们有一个内存泄漏!
//注意:因为我们为一个数组做了新的[],我们将它与delete []匹配
//如果我们刚刚做了新的,我们就可以用delete

(暂停);
return 0;
}

int * readArray(int& size)
{
std :: cout< 输入数组大小的数字。\\\
;
std :: cin>>尺寸;
int * arrPTR = new int [size]; //所有新闻必须删除!

//喜欢预递增到后递增,你可以
为(int count(0); count< size; ++ count)
{
std :: cout<< 输入正数以完全填充数组。
std :: cin>> arrPTR [count];
}

返回arrPTR;
}

//通过值传递大小(可能小于某些架构上的指针)
void sortArray(int * arrPTR,int size)
{
//你可能想检查大小> = 2是否为sanity

//我们做两个循环,以避免在上次迭代时超出数组的边界
for(int i(0); i {
for(int j(i + 1); j < size; ++ j)//第二比较(除了第一个)
{
//比较
if(arrPTR [i]> arrPTR [j] /从最小到最大(使用<从最大到最小)
{
//如果需要,交换
int temp(arrPTR [i]); // put this on stack
arrPTR [i] = arrPTR [j];
arrPTR [j] = temp;
}
}
}
}


I am learning about pointers and the new operator in class.

In my readArray function I am to read in a size. Use the size to dynamically create an integer array. Then assign the array to a pointer, fill it, and return the size and array.

I believe I've gotten that part corrected and fixed but when I try to sort the array, i get the error "uninitialized local variable temp used."

The problem is though I get that error when I am trying to intialize it. Any help appreciated thank you. Seeing my errors is very helpful for me.

#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);
 }

解决方案

You declared temp as a pointer. You need to allocate it on the heap before dereferencing and assigning to it later. However perhaps a variable on the stack would be preferable?

FYI: You should be aware of the memory leak in readArray as well which is leaving callers responsible for calling delete []

Edit: I hope this will help clear up some of the other problems.

#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;
            }
        }
    }
 }

这篇关于单位化局部变量和帮助纠正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆