Answer the question
In order to leave comments, you need to log in
C++ which type of memory is better to use and most importantly why?
1) What kind of memory is better to use?
- char area1[256];
- char * area2 = (char*)malloc( 251);
- MyClass m;
- MyClass * m2 = new MyClass();
- class MyClass() {
- private:
- char area1[256];
- char * area2;
- public:
- MyClass() {
- area2 = (char*)malloc( 251);
- }
- ~MyClass() {
- free(area2);
- }
- };
Answer the question
In order to leave comments, you need to log in
> What kind of memory is better to use?
Depending on the task.
1 If the size of the array is unknown at compile time, then heap allocation should be used. If it's C++, then why not do it via new / delete?
2 Otherwise, if the size of the array is known, then yes, you can declare a constant and use it to determine the dimension.
Option 1 is more common in real life.
If you write in C++ then forget about arrays. Always use pointers where you don't need arrays and std::vector\std::array where you need them. Arrays are a C holdover and not a C++ way
Using malloc\free in C++ code is also not a good choice.
Applicability difference. Statically, you predetermine the number of elements. That is, in C you cannot write char string[n]. If the amount of allocated memory is not known in advance, then it has to be allocated dynamically. The same is true for class instances.
The difference when used in a class - well, you wrote it yourself - in this case, it must be allocated in the constructor and released in the destructor. Pitfall - then you need to write your own copy constructor. For the default copies everything in the forehead, as a result you will get 2 objects in which your field refers to the same memory. When destroying these objects, you will get an error (you cannot free the same memory area twice).
Regarding malloc / realloc - if the old data is no longer needed, then it is better to use realloc, rather than free + malloc, since the memory is quickly fragmented and soon the application will not be able to allocate the required amount (malloc allocates a continuous block of memory)
1) The difference is that in the first case (with an array) - memory is allocated on the stack at the start of the function and, accordingly, is automatically released when the function ends.
And if you use malloc, then this memory is taken from the heap. And, accordingly, this memory can be transferred without problems to the calling function when exiting our function. But the calling function must remember to make free.
2) With classes, too, the same as 1). The lifetime of a class instance in the second case (new) may be longer, but on the other hand, we are responsible - we must not forget to call delete (this is an analogue of free, only with a call to the class destructor).
3) Well, here, by and large, it doesn’t matter how to act, this is our class (all variables are private) what we want, then we turn it back inside, no one will know anyway.
But there is such a subtlety, if we create a multithreaded application (multithread), then we need to use the multithreaded malloc function (link with the multithreaded LIBCMT.LIB instead of the obsolete LIBC.LIB, for VisualStudio this is the /MT flag) - otherwise there will be problems.
the most important difference is that when you use malloc, you have to do a type cast, at this moment the programmer takes full responsibility for the correctness of the cast, which means that there is a potential place for errors (moreover, C is scolded for this, since errors are the most terrible), and if you completely get rid of pointers (you can leave the reference types MyClass & a), then the compiler will monitor the integrity of the system and you will not be able to write 'in the wrong place'.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question