Trust issues.

Sep 27, 2019 at 9:45pm
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 Sep 27, 2019 at 9:46pm
Sep 27, 2019 at 9:46pm
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 Sep 27, 2019 at 9:49pm
Sep 28, 2019 at 12:04am
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.
Sep 28, 2019 at 2:42am
Thanks!

@mbozzi why would line 7 throw an exception?
Sep 28, 2019 at 4:18am
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 Sep 28, 2019 at 4:22am
Sep 28, 2019 at 4:21pm
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 
Sep 28, 2019 at 4:33pm
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.
Sep 29, 2019 at 6:09pm
@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 Sep 29, 2019 at 6:09pm
Sep 29, 2019 at 6:17pm
@zapshe, nvm I found it. I guess I just never looked hard enough.
Sep 29, 2019 at 6:18pm
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.
Sep 29, 2019 at 6:22pm
I did end up finding stuff on it, but thank you!
Sep 29, 2019 at 6:34pm
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.
Sep 29, 2019 at 9:07pm
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 Sep 29, 2019 at 9:08pm
Topic archived. No new replies allowed.