Quantcast
Channel: Pointer or array? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Pointer or array?

$
0
0

I know that pointers are a lot like arrays (if not one and the same) in a way, but I'm having trouble deciphering when I should explicitly delete a pointer.

Take this code for example:

#include <vector>int main(int argc, char** argv){    std::vector<char*> strings;    strings.push_back(argv[0]);    return 0;}

Now obviously, no one in their right mind would make a program exactly like this, but for pure discussion, let's say this is my program. Should I have deleted strings.at(0) to avoid a memory leak there, or is the data considered a character array to be destroyed when the program terminates?

Similarly, suppose I have a vector of a class I've created, and I've declared them all as new instances of the class. Do I then need to delete, or is it fine?

I'm a self-taught programmer, so these complicated (and sometimes dangerous) concepts make me paranoid.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>