Вопрос по c, gcc – Почему я получаю разные результаты, когда разыменую указатель после его освобождения?

2

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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


int * ret_array(int n)
{
    int * arr = (int *) malloc(10 * sizeof(int));
    int i;
    for (i = 0; i < n; i++)
    {
        arr[i] = i*2;
    }
    printf("Address pointer in ret_array: %p\n", (void *) arr);
    return arr;
}

int * ret_oth_array(int n)
{
    int * arr = (int *) malloc(10 * sizeof(int));
    int i;
    for (i = 0; i < n; i++)
    {
        arr[i] = i+n;
    }
    printf("Address pointer in ret_oth_array: %p\n", (void *) arr);
    return arr;
}

int main(void)
{
    int *p = NULL;
    int *x = NULL;
    p = ret_array(5);
    x = ret_oth_array(6);

    printf("Address contained in p: %p\nValue of *p: %d\n", (void *) p, *p);

    free(x);
    free(p);
    printf("Memory freed.\n");
    printf("*(p+4) = %d\n", *(p+4));
    printf("*x = %d\n", *x);

    return 0;
}

Error: User Rate Limit Exceeded-ansi -Wall -pedantic-errorsError: User Rate Limit Exceeded

Address pointer in ret_array: 0x8269008
Address pointer in ret_oth_array: 0x8269038
Address contained in p: 0x8269008
Value of *p: 0
Memory freed.
*p+4 = 8
*x = 0

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

int main(int argc, char * argv[])
{
/* ... code ... */

    free(p);
    free(x);

    printf("Memory freed.\n");
    printf("*(p+4) = %d\n", *(p+4));
    printf("*x = %d\n", *x);

    return 0;
}

Error: User Rate Limit Exceeded

*p+4 = 8
*x = 142106624

Error: User Rate Limit ExceededxError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Vaibhav
Error: User Rate Limit Exceeded Markon
Error: User Rate Limit Exceeded Markon
Error: User Rate Limit Exceeded Niki
Error: User Rate Limit Exceeded Markon

Ваш Ответ

7   ответов
2

Error: User Rate Limit ExceededfreeError: User Rate Limit Exceeded

4

*(p+4) is 8 and *x is 0. Why does this happen? If *(p+4) is 8, shouldn't *x be 6, since the first element of the x-array is 6?

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

3

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

10

free()Error: User Rate Limit Exceededmalloc()Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededfree()Error: User Rate Limit Exceededmalloc()Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededmmap()Error: User Rate Limit Exceededrelease it back to the kernelError: User Rate Limit Exceededfree()Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Markon
13

Error: User Rate Limit ExceededfreeError: User Rate Limit Exceeded

free()Error: User Rate Limit ExceededfreeError: User Rate Limit Exceeded

Error: User Rate Limit ExceededNULLError: User Rate Limit ExceededfreeError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

9

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

2

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Markon

Похожие вопросы