我以数组的名称进行操作,没有出现错误或警告为什么?

发布于 2025-01-25 16:55:04 字数 517 浏览 1 评论 0原文

我编写了一个期望收到错误以及描述表达式必须是可修改的值的代码,但是我不知道可以动态分配的数组可以修改吗?

{
    int* x;
    x =(int*) malloc(3 * sizeof(int));
    if (x == 0)
    {
        printf("sorry met an error");
        exit(1);
    }
    x[0] = 0;
    x[1] = 1;
    x[2] = 2;
    printf("%p\n", x);
    printf("%d\n", x[0]);
    printf("%d\n", sizeof(x));
    x++;
    printf("%p\n", x);
    printf("%d\n", x[0]);
    printf("%d\n", x[1]);
    printf("%d", sizeof(x));
    free(x);
    return 0;
}

顺便说一句,这里的免费功能也触发了断点的任何想法?

I wrote a code expecting to receive error along with the description expression must be a modifiable value but i didn't, I don't understand can arrays that were dynamically allocated be modified?

{
    int* x;
    x =(int*) malloc(3 * sizeof(int));
    if (x == 0)
    {
        printf("sorry met an error");
        exit(1);
    }
    x[0] = 0;
    x[1] = 1;
    x[2] = 2;
    printf("%p\n", x);
    printf("%d\n", x[0]);
    printf("%d\n", sizeof(x));
    x++;
    printf("%p\n", x);
    printf("%d\n", x[0]);
    printf("%d\n", x[1]);
    printf("%d", sizeof(x));
    free(x);
    return 0;
}

By the way the free function here is also triggering a breakpoint any ideas why?

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

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

发布评论

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

评论(2

溺深海 2025-02-01 16:55:04

x指针到数组中的第一个元素。 (x不是数组。)

由于x不是const,您可以自由修改它。 X ++将其修改为指向数组中的 second 元素。

然后,当您尝试释放动态分配的数组(free(x)),您将通过free()一个值malloc 没有给您。因此,调试器引发休息。

在尝试释放它之前,要么还原x到其先前的值,要么使用其他指针变量播放算术,并仅留下x

x is a pointer to the first element in an array. (x is not an array.)

As x is not const, you are free to modify it. x++ modifies it to point to the second element in the array.

When you then attempt to free the dynamically-allocated array (free(x)) you are passing free() a value that malloc did not give you. Hence the debugger triggering a break.

Either restore x to its prior value before attempting to free it, or use a different pointer variable to play arithmetic and leave x alone.

水中月 2025-02-01 16:55:04

用“ GCC”命令行编译:

gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" -o "untitled.o" 

与命令行链接:

gcc -ggdb3 -o "untitled" "untitled.o" 

修改的代码,并带有

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

int main( void )
{
    int* x;
    x = malloc(3 * sizeof(int));
    // in C,  the memory allocation functions (malloc, calloc, realloc)
    // returned type is 'void*' which can be assigned to any pointer
    // so casting just clutters the code and is error prone.

    if (!x)
    {
        perror("sorry met an error");
        // the 'perror' function outputs to 'stderr' the text meaning of 
        // the 'errno' variable plus the text supplied by the parameter.
        // note: in the current scenario 'errno' was set by 'malloc'

        exit( EXIT_FAILURE );
        // EXIT_FAILURE is defined in stdlib.h'
    }
    
    x[0] = 0;
    x[1] = 1;
    x[2] = 2;
    
    printf("%p\n", (void*)x);
    // %p requires a 'void*' parameter
    
    printf("%d\n", x[0]);
    printf("%zu\n", sizeof(x));
    // sizeof returns a 'long unsigned int' value
    // which is printable via '%zu'
    
    x++;
    // this modifies the pointer returned via 'malloc'
    // it does not change where the array is located in memory
    
    printf("%p\n", (void*)x);
    printf("%d\n", x[0]);
    printf("%d\n", x[1]);
    printf("%zu", sizeof(x));
    
    free(x);   
    // the pointer to the array has been changed
    // so this will fail at runtime
    
    return 0;
}

带有修改的代码的注释,以下是输出

0x562e604882a0
0
8
0x562e604882a4
1
2
free(): invalid pointer
Aborted (core dumped)

compiling with 'gcc' command line:

gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" -o "untitled.o" 

linked with the command line:

gcc -ggdb3 -o "untitled" "untitled.o" 

modified code, with comments

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

int main( void )
{
    int* x;
    x = malloc(3 * sizeof(int));
    // in C,  the memory allocation functions (malloc, calloc, realloc)
    // returned type is 'void*' which can be assigned to any pointer
    // so casting just clutters the code and is error prone.

    if (!x)
    {
        perror("sorry met an error");
        // the 'perror' function outputs to 'stderr' the text meaning of 
        // the 'errno' variable plus the text supplied by the parameter.
        // note: in the current scenario 'errno' was set by 'malloc'

        exit( EXIT_FAILURE );
        // EXIT_FAILURE is defined in stdlib.h'
    }
    
    x[0] = 0;
    x[1] = 1;
    x[2] = 2;
    
    printf("%p\n", (void*)x);
    // %p requires a 'void*' parameter
    
    printf("%d\n", x[0]);
    printf("%zu\n", sizeof(x));
    // sizeof returns a 'long unsigned int' value
    // which is printable via '%zu'
    
    x++;
    // this modifies the pointer returned via 'malloc'
    // it does not change where the array is located in memory
    
    printf("%p\n", (void*)x);
    printf("%d\n", x[0]);
    printf("%d\n", x[1]);
    printf("%zu", sizeof(x));
    
    free(x);   
    // the pointer to the array has been changed
    // so this will fail at runtime
    
    return 0;
}

with the modified code, the following is the output

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