0

I am new in C++ and now I am learning how cin and cout works. The case is, as you can see in the code below I create a string, and a char * in order to compare the C way to read a string and the C++ way. In C++, it makes sense that if I create a string then I worry about to set this string to NULL?

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#define SIZE 10

using namespace std;
int main () {
  cout << "Write Something: \n";
  string f1;
  char *p;
  p = (char *) malloc(SIZE); // This is how I will use it in c
  getline(cin, f1);
  cout << "Write Something else: \n";
  scanf("%s", p);
  cout << f1 << "1\t";
  printf("%s2\n", p);
  free(p);
  p = NULL; // After the pointer has been freed, we set it to NULL
  cout << "p freed\t";
  f1.clear();
  cout << "f1 deleted\t";
  return 0;
}
7
  • A std::string can be empty (i.e., ""), or it can have one-or-more char. It cannot be NULL. Commented Jul 12, 2020 at 18:13
  • Ok, and can this memory allocated be freed without using free() Commented Jul 12, 2020 at 18:14
  • 1
    Most objects in C++ manage their own resources. I haven't needed to use malloc or new or new[] in about a decade. Instead I use std::vector, or std::unique_ptr, or if I really have to std::shared_ptr. Commented Jul 12, 2020 at 18:15
  • @Eljay so in c++ strings aren't like String in java, always will have a value different to NULL? Commented Jul 12, 2020 at 18:16
  • 2
    Java and C++ are very different languages, with very different primitives, and very different memory management. Even a rather different approach to object-oriented programming. So, yes, you are correct, C++ std::string is not like Java String. (Likewise with JavaScript, except the OO is even more different.) Commented Jul 12, 2020 at 18:17

2 Answers 2

2

You don't have to worry about setting a std::string object to NULL.

And you don't have to clear() it if it is just going to go out of scope right afterwards. Its destructor will handle freeing any memory used for the character data for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, so in C++ the risk there was in C when we use malloc() doesn't exists?
@Josep Whatever you malloc - you free. Whatever you new - you delete. Whatever you new[] - you delete[]. If you don't do those - you don't worry them.
1

No, you don't have to even clear f1, as the RAII will automatically cleanup, when the function returns.

2 Comments

C++ does not have garbage collection
RAII is not at all like a garbage collector. Objects in C++ don't hang around until the garbage collector gets around to releasing them. They get destroyed when they go out of scope. There are many advantages to that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.