Is there realloc() in C++?

Asked

Viewed 179 times

0

The realloc() is unique to C? Would it have any function that would be equal in C++?

  • It’s from C but it’s not exclusive, it works perfectly on C++. https://www.cplusplus.com/reference/cstdlib/realloc/? kw=realloc

  • So wouldn’t have trouble using? By mixing C with C++

2 answers

5


Yes, realloc is unique to C. There is no equal function pq the programming style is different in C++. It is possible to use realloc/malloc/free in C++, but it is not recommended at all. For simple programs it may even work, but with more complicated techniques (polymorphism, templates, metaprogramming, etc.) it is almost guaranteed that it will give some kind of Undefined behavior. Example:

//Estrutura em C
struct Cor {
  unsigned char r;
  unsigned char g;
  unsigned char b;
};
void inicializar_cor(Cor* c, unsigned char _r, unsigned char _g, unsigned char _b) {
  c->r = _r;
  c->g = _g;
  c->b = _b;
}

//Estrutura em C++
struct Cor {
  unsigned char r;
  unsigned char g;
  unsigned char b;

  Cor(unsigned char _r, unsigned char _g, unsigned char _b) 
    : r{_r}, g{_g}, b{_b} {}
};

Dynamically allocating a red color variable:

//C
Cor* cor = malloc(sizeof(Cor));
inicializar_cor(cor, 255, 0, 0);

//C++
Cor* cor = new Cor{255, 0, 0};

To create and initialize a Color variable in C is more complicated than in C++. The fact that there are no constructors introduces the additional initializ_color method and this adds a weakness point, because if the user puts a null pointer or forgets to call initializ_color, the structure will be invalid. But that’s not the only problem, because the C boot rules are quite different from the C rules++.

struct CorAlfa {
  unsigned char r;
  unsigned char g;
  unsigned char b;
  unsigned char alfa;
};

//C
CorAlfa* cor = malloc(sizeof(Cor)); //Ok!

//C++
CorAlfa* cor = new Cor{255, 255, 255}; //Erro!

Assuming I decided to add opacity color support to my program, but during the implementation I forgot to update my allocation routine. In C, malloc allocates Color*, returns a void* pointer and implicitly converts to Coralfa*, silently generating a program that will give running problems because Coralfa is a 4-byte struct, while Color is a 3-byte struct only. In C++, this is something trivial that the compiler recognizes and shows the error.

Problems that in C would be solved with realloc, have other alternatives in C++:

  • Compose a data structure based on Std::vector, Std::forward_list, or Std::list to delegate dynamic allocation management (simple).
  • Manage dynamic allocation with Std::unique_ptr (simple).
  • Implement routines with move Semantics to reuse memory and avoid unnecessary copies (intermediary).
  • Implement custom (advanced) suppliers. Watch this lecture in English for an introduction to this technique.

C++ has a number of these safe Guard mechanisms, which add more confidence to the programs. With the latest updates (C++17 and C++20) compilers are getting smarter and better able to optimize these security structures, especially GCC and Clang. Currently, very little is lost in performance and sometimes quite complex codes for a C++ program generate the same Assembly for the same program written in C, except that C++ provides a safer, more resourceful and more productive programming environment.

  • Wow.. Mto thanks for the explanation! Helped me mto!!

2

It is not unique to C, it exists in C++ as well, but should almost never be used. There may be some cases when creating a structure and need to deal with memory management at a lower level. In C++ only use new and delete, and even then if something already existing in the library does not solve well for you.

So if you shouldn’t use malloc() and free() in C++, because it would make sense to use realloc()?

If using it is programming in C in C++, this is usually wrong, but it works.

  • Hmm ok! I’m familiar with the " new ". Obr!

Browser other questions tagged

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