尝试释放 C 中的内存时出现堆损坏错误

发布于 2025-01-15 15:00:04 字数 781 浏览 3 评论 0原文

我有这段代码:

#include <stdio.h>
#include<stdlib.h>

int* create_arr(int start, int end) {
    int length = end - start, i = start, j;
    int* arr;
    arr = (int*)malloc(sizeof(int) * length);
    if (arr == NULL) {
        printf("memory allocation problem");
        exit(1);
    }
    for (j=0; i <= end; i++, j++) {
        arr[j] = i;
    }
    return arr;
}
void main() {
    int num, num2;
    int *arr;
    scanf_s("%d %d", &num, &num2);
    arr = create_arr(num, num2);
    for (int i = 0; i <= num2 - num; i++) {
        printf("%d, ", arr[i]);
    }
    free(arr);
}

对于这段代码,我分配内存并将值设置到其中的部分工作正常,但是当我添加最后一行时:

free(arr);

它会弹出一条错误消息,内容如下: “调试错误,检测到堆损坏” 我想知道释放内存时我做错了什么,并且希望得到一些帮助。

i have this code:

#include <stdio.h>
#include<stdlib.h>

int* create_arr(int start, int end) {
    int length = end - start, i = start, j;
    int* arr;
    arr = (int*)malloc(sizeof(int) * length);
    if (arr == NULL) {
        printf("memory allocation problem");
        exit(1);
    }
    for (j=0; i <= end; i++, j++) {
        arr[j] = i;
    }
    return arr;
}
void main() {
    int num, num2;
    int *arr;
    scanf_s("%d %d", &num, &num2);
    arr = create_arr(num, num2);
    for (int i = 0; i <= num2 - num; i++) {
        printf("%d, ", arr[i]);
    }
    free(arr);
}

for this code the part where i allocate the memory and set values into it works fine but when i add the very last line:

free(arr);

it makes an error message popup that says:
"debug error, heap corruption detected"
im wondering what im doing wrong when freeing the memory and would love some help on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青丝拂面 2025-01-22 15:00:04

这个 for 循环

for (j=0; i <= end; i++, j++) {
    arr[j] = i;
}

是不正确的,因为根据变量 length 的值,数组具有结束 - 开始元素。

int length = end - start, i = start, j;

循环中的条件应为 j <<长度i <结束

for (j=0; j < length; i++, j++) {
    arr[j] = i;
}

或者您需要将 length 的值增加一。

一般来说,编写的循环会让代码的读者感到困惑。

您可以改为编写

for ( i = 0; i < length; i++) {
    arr[i] = start++;
}

此 for 循环中的条件 i <= num2 - num

for (int i = 0; i <= num2 - num; i++) {
    printf("%d, ", arr[i]);
}

也是不正确的,原因与上面指出的循环相同。必须是

for (int i = 0; i < num2 - num; i++) {
    printf("%d, ", arr[i]);
}

您应该在函数内检查参数end的值至少不小于参数start的值。如果动态分配的数组必须包含 [start, end] 范围内的值,则内存分配应类似于

int length = end - start + 1;
arr = malloc(sizeof(int) * length );

该函数可以通过以下方式之一定义,前提是该范围必须包括startend 的值。

int * create_arr( int start, int end ) 
{
    int *arr = NULL;

    if ( !( end < start ) )
    {
        int length = end - start + 1;

        arr - malloc( lengh * sizeof( int ) );

        if ( arr != NULL )
        {
            for ( int i = 0; i < length; i++ )
            {
                arr[i] = start++;
            }
        }
    }

    return arr;
}

或者

int * create_arr( int start, int end ) 
{
    if ( end < start )
    {
        int tmp = start;
        start = end;
        end = tmp;
    }

    int length = end - start + 1;

    int *arr - malloc( lengh * sizeof( int ) );

    if ( arr != NULL )
    {
        for ( int i = 0; i < length; i++ )
        {
             arr[i] = start++;
        }
    }

    return arr;
}

注意,根据 C 标准,不带参数的函数 main 应声明为

int main( void )

This for loop

for (j=0; i <= end; i++, j++) {
    arr[j] = i;
}

is incorrect because the array has end - start elements according to the value of the variable length

int length = end - start, i = start, j;

The condition in the loop shall be either j < length or i < end.

for (j=0; j < length; i++, j++) {
    arr[j] = i;
}

Or you need increase the value of length by one.

In general the loop as written confuses readers of the code.

You could write instead

for ( i = 0; i < length; i++) {
    arr[i] = start++;
}

Also the condition i <= num2 - num in this for loop

for (int i = 0; i <= num2 - num; i++) {
    printf("%d, ", arr[i]);
}

is incorrect by the same reason as the loop pointed to above. It must be

for (int i = 0; i < num2 - num; i++) {
    printf("%d, ", arr[i]);
}

You should check within the function that the value of the parameter end at least is not less than the value of the parameter start. And if the dynamically allocated array must contain values in the range [start, end] inclusively then the memory allocation should look like

int length = end - start + 1;
arr = malloc(sizeof(int) * length );

The function can be defined in one of the following ways provided that the range must include the both values of start and end.

int * create_arr( int start, int end ) 
{
    int *arr = NULL;

    if ( !( end < start ) )
    {
        int length = end - start + 1;

        arr - malloc( lengh * sizeof( int ) );

        if ( arr != NULL )
        {
            for ( int i = 0; i < length; i++ )
            {
                arr[i] = start++;
            }
        }
    }

    return arr;
}

or

int * create_arr( int start, int end ) 
{
    if ( end < start )
    {
        int tmp = start;
        start = end;
        end = tmp;
    }

    int length = end - start + 1;

    int *arr - malloc( lengh * sizeof( int ) );

    if ( arr != NULL )
    {
        for ( int i = 0; i < length; i++ )
        {
             arr[i] = start++;
        }
    }

    return arr;
}

Pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文