Dynamic allocation in C++

Asked

Viewed 647 times

1

I was learning a little bit more about dynamic allocation in C++ on the Internet, and a teacher’s code caught my attention.

It is a code made to spend only the necessary memory and not have "blank space", storing only the amount of characters used.


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main ()
{
    char *nome;
    nome = (char*)malloc(sizeof(char)+1);

    gets(nome);

    cout << nome << endl;

    return 0;
}

As far as I know, the memory allocation in C++ is done through the new, not using malloc().

Another thing that caught my attention was that this code would not be invading memory since it was only allocated a space of 2 bytes?

  • I didn’t understand your question about the allocation of memory in c/c++,the new operator is c++,it is more abstract than malloc since you don’t need to specify the size in bytes of your dynamic allocation

  • My question is whether this code is invading memory, since it has only been allocated 2 bytes for the variable "name" and whether this code is the correct way to allocate memory in C++, since it appears to be a C code, excluding "Cout".

  • Allocation in c++ is done with new, but no one prevents you from using malloc, although I shouldn’t. Since I speak in "shouldn’t", you shouldn’t use gets in c++ which has been marked as obsolete in c++11 and removed in c++14. But what is your specific question ?

  • Well, this code is theoretically made to use only the amount of memory needed, for example, the user typed a string with 15 characters, it will consume only the data needed to store 15 characters + the string finalizer. However, I believe that you are only allocating a space of 2 bytes, that is, 1 character + the string finalizer. Is this code actually invading memory? Or are you fulfilling your proposal without invading memory?

  • 1

    You’ve allocated 2 bytes, so if you save more than 2 bytes, you’re invading memory. Conclusion: anything that the user places with more than one letter (the other is for the terminator) represents invalid access in memory, in the buffer overflow case and consequent undefined behavior.

  • Thank you very much, so in fact it is what I thought, is invading memory. Apparently I will have to change my source of study, some suggestion?

  • 1

    To c tag wiki already has a suggestion of good books on the topic. Good studies.

  • Each program executed in RAM has its own space for its execution, and this space is divided by some areas, the main ones being the stack, this space of 2 bytes is being allocated in the space destined to it,what could happen is the use of the total heap of the program,to release this allocated memory

Show 3 more comments

1 answer

1


If you were learning to allocate memory like that on the Internet, just run. Almost all the content on the internet about programming is bad, something saved and for a layman it is very difficult to identify what is bad because he does not know it yet, so it is better to look only sources that are admittedly reliable.

In this case your concern should not be that, this code should be just like this:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string nome;
    cin >> nome;
    cout << nome << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In fact, if you were to make an allocation you should use new and not malloc(), and even then only in very specific code, probably a library, in normal code this is more rare, the most common type is to make your own memory management or use smart pointers.

If you were to use the malloc() should use logo 2, have no reason to use sizeof(char) because he’s always 1.

This code has too many errors and should be completely ignored, including it allocates space to only 1 character and will corrupt the memory.

Browser other questions tagged

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