0

What is the difference between:

int messageLen = 25;
char* message = new char [messageLen + 1];
int messageLen = 25;
char* message = new char (messageLen + 1);

and between:

delete(message);
delete [] message;

I read some articles on this topic, but none of them give me the exact answer.

I am beginner in C++, I'd be very grateful for some example!

  • 1
    `char* message = new char (messageLen + 1);` allocates a single character. `delete(message);` frees that single character. – drescherjm Mar 23 '20 at 15:52
  • 3
    Don't search for articles online, get [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Mar 23 '20 at 15:53
  • 1
    `char* message = new char [messageLen + 1];` allocates an array of 26 characters. `delete [] message;` frees the array. – drescherjm Mar 23 '20 at 15:53
  • 4
    Modern `c++` teaches you to avoid / almost never use `new` – drescherjm Mar 23 '20 at 15:55
  • std::string would be more appropriate most of the time over std::vector – drescherjm Mar 23 '20 at 16:00
  • @drescherjm Why? When someone new would learn C++ a he doesn't know about `new` etc. I mean these hard stuff. why would anyone learn it then? – John Doe Mar 23 '20 at 16:50
  • 2
    `new` is not a beginner concept. It's more of an advanced concept that is not easy to get correct. And modern `c++` nearly completely removes the need to ever use `new`. Its better for students to learn how to write quality `c++` code first then perhaps in an advanced course learn the ugly details of how it all works internally. – drescherjm Mar 23 '20 at 16:53
  • @JohnDoe `new` and `delete` do exist, and can be used, but for more complex projets, it leads to bugs or memory leaks because of the difficulty to track the usage of the allocated memory. Raw arrays are still useful in some cases, eg., interop with c (write code in c), interop with other languages (c#). However today, we should use "initialization at declaration" and unique/managed pointers, that's the canonical modern c++. My opinion is that learning all the possibilities is the way to learn and understand. – Soleil Mar 23 '20 at 16:54

1 Answers1

3

What is the difference between:

int messageLen = 25; char* message = new char [messageLen + 1];

This allocates a char array of 26, normally for 25 characters and a trailing \0.

int messageLen = 25; char* message = new char (messageLen + 1);

Allocates a single char and gives it the value 26.

delete(message);

delete [] message;

delete[] frees memory allocated by new[], delete (w/o []) does the same for memory allocated by new; for efficiency, C++ doesn't keep track of this itself.

I read some articles on this topic, but none of them give me the exact answer.

It sounds like you may need a good C++ book.

I am beginner in C++, I'd be very grateful for some example!

All of the above is now considered fairly advanced C++, "beginners" shouldn't concern themselves with it at all. There are very few reasons to use new/delete and/or new[]/delete[]; avoid them unless you really know what you're doing.

The easiest way to deal with string data is to use std::string. If you really need an array, look to std::array or std::vector.

 constexpr size_t messageLen = 25;
 std::array<char, messageLen+1> message;

If you don't have std::string or std::vector, are you sure you're really writing C++ code? Probably not (C with classes?). Although std::array was added in C++11, so some really old environments may not have that.

But even then, reasonable facsimiles of these components can be written which still avoids the use of new, et. al. in normal client code.

Ðаn
  • 10,934
  • 11
  • 59
  • 95