简体   繁体   English

函数损坏了我在C中的数组

[英]the function corrupted my array in C

Hello I'm build a function are printing array with pointers on c with Visual Studio 2015. while i run the function this send me this massage: Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted. 您好,我正在构建一个函数,使用Visual Studio 2015在c上使用指针打印数组。当我运行函数时,向我发送此消息:运行时检查失败#2-变量'arr'周围的堆栈已损坏。

this the function: 这个功能:

void arrprint(int* arr, int size)//printing numbers:
{

    size = (int)arr + size*sizeof(int);// the last adress of the array
    int* firstAdress = arr;
    for (arr=firstAdress; arr < size; arr++)
    {
        printf("%2d", *arr); //printing
    }
    *arr = firstAdress;  //for not destroy the array
    printf("\n");
}

thanks for helpers 谢谢帮手

This line 这条线

*arr = firstAdress;  //for not destroy the array

destroys the array. 破坏数组。 You are writing into the memory when you dereference arr . 取消引用arr时,您正在写入内存。


Since in C, everything is passed by value, you do not have to worry about corruption when you change arr in the function. 由于在C语言中,所有内容都是按值传递的,因此您不必担心在函数中更改arr时的损坏。 So, you do not need firstAdress . 因此,您不需要firstAdress

void arrprint(int* arr, int size)//printing numbers:
{

    int* lastAddress = arr + size;
    int* firstAdress = arr;
    for (arr=firstAdress; arr < size; arr++)
    {
        printf("%2d", *arr); //printing
    }
    printf("\n");
}

After Updating, the code should look like this. 更新后,代码应如下所示。 You should notice that arr which is being changed here, is only being changed in this function, and the actual array pointer (in the caller function) is intact and safe. 您应该注意到,此处要更改的arr仅在此函数中被更改,并且实际的数组指针(在调用方函数中)是完整无缺且安全的。

You're trying to use size as an int * . 您正在尝试将size用作int * Use an actual int * instead. 改用实际的int *

Also, by setting *arr = firstAddress , what you're really doing is writing the address of the array into the first element in the array. 另外,通过设置*arr = firstAddress ,您实际上所做的就是将数组的地址写入数组的第一个元素 Also, since arr is a local variable, changes to it don't affect the variable in the calling function. 另外,由于arr是局部变量,对其进行更改不会影响调用函数中的变量。

void arrprint(int* arr, int size)//printing numbers:
{

    int *lastAddress = arr + size;
    int *firstAdress = arr;
    for (arr=firstAdress; arr < lastAddress; arr++)
    {
        printf("%2d", *arr); //printing
    }
    printf("\n");
}

Personally think the best solution is: 个人认为最好的解决方案是:

void arrprint(int* arr, int size)//printing numbers:
{
    int *lastAddress = arr + size    
    int *firstAddress = arr;
    for (firstAddress = arr; firstaddress < lastAddress; firstaddress++)
    {
        printf("%2d", *firstAddress); //printing
    }
    printf("\n");
}

Reason you do not alter the original pointer. 原因是您不更改原始指针。 You can also use const int* arr in the function declaration, then you will get a compiler error if you dereference the pointer. 您也可以在函数声明中使用const int * arr,然后如果取消引用指针,则会出现编译器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM