error out of Bounds in doctor memory

Asked

Viewed 52 times

-2

I am having an error out of Bounds on doctor memory, and at the time of printing the files. There is something wrong with this code?

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

typedef struct{
 char nome[30];
 int idade;
 int altura;
}Pessoa;

int main(void){
    int escolha=1,linhas=1,i=0;
    Pessoa *p;


    for(;;){
            puts("(0) para sair do programa (1) para inserir novo nome \n");
            scanf("%d",&escolha);
            switch(escolha){
            case 1:
                p = (Pessoa *) realloc(p,sizeof(Pessoa*)*linhas);
                 if(!p){
                    puts("nao foi possivel alocar memoria");
                    exit(-1);
                }

                puts("insira o nome da pessoa");
                scanf("%s", p[linhas].nome);
                puts("insira a idade da pessoa");
                scanf("%d", &p[linhas].idade);
                puts("insira a altura da pessoa");
                scanf("%d", &p[linhas].altura);
                linhas++;
                break;
            case 0:

                for(i=1;i<linhas;i++)
                    printf("Pessoa %d nome: %s\tidade: %d\taltura: %d \n", i,p[i].nome,p[i].idade,p[i].altura);
                free(p);
                exit(1);
            default:
                puts("Caractere invalido inserido!");
            }
    }
    return 0;
}


~~Dr.M~~ Error #1: UNADDRESSABLE ACCESS beyond heap bounds: writing 0x02a42010-0x02a42014 4 byte(s)
~~Dr.M~~ # 0 msvcrt.dll!ungetwc   +0x595c   (0x7715040c <msvcrt.dll+0x8040c>)
~~Dr.M~~ # 1 msvcrt.dll!scanf     +0x76     (0x77145097 <msvcrt.dll+0x75097>)
~~Dr.M~~ # 2 msvcrt.dll!scanf     +0x17     (0x77145038 <msvcrt.dll+0x75038>)
~~Dr.M~~ # 3 main                  [C:/Users/mathe/Desktop/prob/C/trabalho2/main.c:29]
~~Dr.M~~ Note: @0:00:05.087 in thread 6176
~~Dr.M~~ Note: prev lower malloc:  0x02a41fc8-0x02a41fcc
~~Dr.M~~ Note: instruction: mov    %eax -> (%ecx)
insira a altura da pessoa

The way out is corrupted

Pessoa 1 nome: Ó"┬      idade: -17891602        altura: -17891602
Pessoa 2 nome: ¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■¯■asdasd   idade: -17891602        altura: -17891602
Pessoa 3 nome: asdasd   idade: 123123   altura: 12312

1 answer

1


Notice this excerpt in your code:

Pessoa *p;

Here we have a pointer, okay. Now let’s go to the realloc():

p = (Pessoa *) realloc(p,sizeof(Pessoa*)*linhas);

The function realloc() will get the address of p and relocate. The question is: What is the address of p? I put the following printf to see what his address is:

printf("%p \n", p);

And I got this exit:

0x400590

As you can see, the pointer p is pointing to a place in memory. Where is this place I have no idea, but I don’t think it’s in the heap (I’m not sure if it’s in the heap or not, if someone reads and knows I’d be grateful if you could clear that up), so trying to relocate that part of the memory is not a good idea.

When we create a pointer it comes with a memory junk, equal a type variable int.

You made that comment in the other reply:

I had read on a forum that realloc() acted like a malloc() if the pointer was null, or unallocated, apparently it is not so

But in the case of your code the pointer p is not null, it already points to a memory location and this generates the error.

Solution

To solve the problem just leave the null pointer as follows:

Pessoa *p = NULL

Now the pointer is nulo and the realloc() will behave in the same way as malloc().

One tip I give you is to always let your unused pointers point to NULL, this can avoid mistakes.

Your program also has problems with garbage in the buffer, put some getchar() to solve.

Behold here your code working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.