Trust issues.

If you have a dynamic array of strings and you delete[] it, will the strings still delete their dynamic arrays?

1
2
3
4
5
6
7
8
9
10
11
  #include <string>

  int main(){
    std::string * da = new std::string [3]; 
    //da now holds an address to 3 string objects which
    //have dynamic char arrays
    da[0] = "hdbye"; // da[0] makes a dynamic array holding the chars "hdbye"
    delete[] da; 
    // I delete my string objects, but do the string objects 
    // delete their character arrays?
  }


just wondering.
Last edited on
Yes, the destructor for each string is called. No leaks.

You can try this with your own custom class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>
#include <string>

class Blooper {
  public:
  ~Blooper()
  {
    std::cout << "dtor\n";   
  }
};

int main()
{
    Blooper* bloops = new Blooper[3];  
    delete[] bloops;
}

dtor
dtor
dtor


That being said, just use a vector!
Last edited on
The problem with your code is that you are not guaranteed to make it to line 8. Your code immediately leaks all those strings if line 7 throws an exception.

The bottom line is that new and delete are for careful use by experts. Strongly prefer library components that eliminate these problems such as std::vector, std::string or std::unique_ptr.
Thanks!

@mbozzi why would line 7 throw an exception?
It's also a bit pointless using new on something that already stores it's data on the heap.

Don't use new or delete :+)

Exceptions:
https://en.cppreference.com/w/cpp/string/basic_string/basic_string
Last edited on
why would line 7 throw an exception?

It's highly unlikely to.

If you need an array made at run-time, you should use smart pointers instead -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int size = 0;
	std::cout << "Input Size: ";
	std::cin >> size;

	std::unique_ptr<int[]> pp = std::make_unique<int[]>(size);


	for (int i = 0; i < size; i++)
		pp[i] = i + 10;

	std::cout << "Input elements are :\n";
	for (int i = 0; i < size; i++)
		std::cout << pp[i] << std::endl;

} //unique_ptr will deallocate when out of scope 
highwayman, line 7 can throw an exception if there is no more memory to allocate an std::string (has to turn that literal into an std::string). However, if your system is in a state to allow that, it might be that you're so constrained that even so much as printing an error message fails as well. https://stackoverflow.com/questions/7749066/how-to-catch-out-of-memory-exception-in-c

Most programs in the wild probably are not tested sufficiently for out-of-memory failure conditions.
@Ganado I know about not having enough memory, but I’m not using my memory for anything else. The sole items in my memory are the three strings, a single pointer and a 5 byte string literal. I’m pretty sure that won’t cause a memory exception. Thanks for the clearing up, was a little confused on what he meant.

@zapshe I keep on hearing about smart pointers, but I’ve never really used them and I have no idea how they work and I have no idea where they are in the reference page in cplusplus.com, so I can’t really bring my self to trust them because I don’t know what they’ll do at any moment. If you could explain them all to me or maybe just show me where they are on the site though that would help.

@TheIdeasMan just look at the zapshe response, I’m to lazy to write it again.
Last edited on
@zapshe, nvm I found it. I guess I just never looked hard enough.
Smart pointers are like regular pointers but with a small wrapper on them to make them less dangerous. Instead of needing to use new or delete, you allocate memory as shown on line 7 - "size" being the variable taken in so we know how big to make the pointer/array.

It's useful in that when the smart pointer goes out of scope, it deallocates the memory rather than giving you a memory leak as described on line 17. So at the end of an if statement, loop, or function, you don't need to manually delete the pointer or anything. If you need the pointer memory deallocated earlier, you can use the .reset() function.

The make_unique one makes it so you don't accidentally make two pointers that are staring at the same memory space.

https://www.learncpp.com/cpp-tutorial/15-5-stdunique_ptr/

^Haven't read it yet myself, just found it. It makes me happy that this tutorial covers it.
I did end up finding stuff on it, but thank you!
Instead of having a pointer array of std::strings, create a std::vector of pointers to the strings.

Or simply get the address of the string if you need a pointer.

With modern C++ there is little need for using raw pointers.
Thanks for the clearing up, was a little confused on what he meant.

Really my point is that in any real program, the code between new xyz and delete xyz is typically prone to failure -- either by simply exiting early, or throwing any kind of exception.

Writing code that doesn't leak in realistic cases is typically very easy as long as you're not writing plain new and delete everywhere.
Last edited on
Topic archived. No new replies allowed.