Bizarre error with dynamic allocation

Asked

Viewed 101 times

0

I wrote this simple program:

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

int main(void) {
  char *nome;

  nome = (char *)malloc(10 * sizeof(char));
  if(nome == NULL) {
    puts("Erro de alocacao!");
    exit(1);
  }

  scanf(" %s", nome);

  nome = (char *)realloc(nome, strlen(nome));
  if(nome == NULL) {
    puts("Erro de realocacao!");
    exit(2);
  }

  printf("%s\n", nome);

  free(nome);

  return 0;
}

Apparently you don’t have any mistakes, do you? But the truth is, depending on mine input, an execution error occurs called:

corrupted size vs. prev_size Aborted (dumped core)

  • I just did the tests here and sizeof(char) returns 1, ie 1 byte and not 4 bytes.

  • I’m using gcc, not g++.

  • 3

    The above comments are completely mistaken. The specification of C and C++ is clear that the type size char is always 1 so it makes no sense to use sizeof(char), just as it is bad, and even wrong to do cast in malloc(). gcc and g++ are not the same compiler.

  • Yes I also found strange the answers of the friend, I have read in several places about gcc and g++, and no one talked about the two being the same thing. Speaking of malloc(), I’ve also read several codes where casting is done, but when I don’t do the compiler doesn’t issue any Warning, so I think without casting is also correct.

  • 2

    It is correct without the casting, Working is different than being right, which is why I said that’s not how you learn, you’ll learn a lot of wrong things just because it works. In certain languages this may not even be such a big problem, in C believing that working is the same as being right is a huge problem, so I reaffirm that I can not learn on the basis of trial and error.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

Show 1 more comment

1 answer

4

Clearly there’s a mistake there.

You’re allocating 10 bytes. Then it allows someone to enter data freely, that is, someone can type something larger than 10 bytes without any restriction, which will already corrupt the memory and in general there will be no complaint pain part compiler or Runtime (it is possible to connect certain options that catch this kind of problem), then for some reason I have no idea takes this data that is already doing something wrong and tries to change it from place to get any advantage. I mean, this code is bizarre.

This specific error occurs because the realloc() needs the size information allocated to know what to do, in some cases it can identify that this data is invalid and shows you this.

C is so, if you think these behaviors are bizarre (they are not because they are documented) then C is not a language for you, look for one that does automatic memory management and does not allow this type of behavior (at the expense of performance and flexibility).

  • Changing language is not an option, I love learning about the C language. It is not only because of an error that I will abandon the whole system! It’s the mistakes you learn. But one thing I found strange was that this code is basically an adaptation of the book I’m using to learn the language, it’s called Luis Damas' C Language.

  • How would you write a code to reallocate the size of a string?

  • 3

    So start learning C, the way you’re doing is deluding yourself that you’re learning. It is with study that one learns, in trial and error one does not learn, especially C. If the book tells to do this throw the book away. By the style of code you’re using I realized you’re learning a lot of wrong things, it may be the book’s fault (but I can’t say).

  • I believe it’s from the book, because I study it very carefully, I always reread it several times, even understanding the content. Which books you recommend me to learn C in the best way possible?

  • 2

    I do not recommend because everyone has their reality, can have problems that I do not know, what I trust is not very suitable for most people, some people have recommended some in https://answall.com/tags/c/info. The best way to learn C is to have the right attitude, more than reading a book.

  • 1

    Luis Damas' book has some mistakes. I myself have asked a question here because I thought his explanation was not correct. Worse than the explanation was really wrong. https://answall.com/questions/398659/tamanho-do-tipo-int-com-os-prefixos-short-e-long-em-c<a>

Show 1 more comment

Browser other questions tagged

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