is it safe?Can I write this kind of code?
No, definitely not!
And you don't have to, the vector will initialize itself properly.
Bo Persson
"Bo Persson" wrote:
> I want to know is the code will hurt program's stability?Will it cause program crash ?
You can only use memset on objects that would be valid in C code, like
an array of int or a simple struct. You cannot use it on C++ objects.
In this case it would be like randomly overwriting memory.
Fortunately, in C++ a class can have a constructor that properly
initializes each object of that class type. That's what happen in
std::vector.
Bo Persson
Of course it will. What would you expect? The `std::vector' is a
class. You cannot treat an instance of it as a pointer. Read in
your favorite C++ textbook about classes.
Alex
Your code doesn't do what you apparently think it does. It just stomps
all over vector's internal data structures - it does _not_ zero out the
block of memory managed by vector.
What were you trying to achieve?
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925.
Pango:
There is no way this can work as written. But
memset(&v[0], 0, v.size()*sizeof(int));
should work.
But, as others have said, this is not the C++ way.
--
David Wilkinson
Visual C++ MVP
Certainly.
You are wiping out vectors internal data members used to keep track of what
memory is owned.
If the vector is not empty and it stores a POD (plain old data) you can do
memset(&v[0], 0, sizeof(v[0]) * v.size());
which overwrites just the data.
This is not safe to do if it is a non-POD.
Stephen Howe
It depends a lot on what you are trying to achieve. If you are trying to
corrupt your vector so that it doesn't work, this is a great way to do it.
If, on the other hand, you are trying to initialize your vector, you have a
problem and a misconception. The problem is that the vector as declared
doesn't have any elements allocated. Therefore, there is nothing to
initialize. The misconception comes into play because when you allocate
space for a vector (via resize() or constructor argument) the elements
allocated will be initialized with whatever value you wish. By default it
will use the default constructor to create a value to initialize it with,
but you can provide your own. This means that generally you don't need to
initialize your vector with something like memset. In the case above, if I
assume you wanted a vector of 100 ints, you could write:
v.resize(100);
and you would now have 100 ints allocated each initialized to 0 (which is
the value int's default constructor uses). If I wanted it initialzed to 1,
I could write:
v.resize(100,1);
That covers one case of memset usage. The others are spelled std::fill()
or std::fill_n() in C++. I could say:
std::fill(v.begin(), v.end(), 0);
and set/reset the vector to all 0s. This works for all the containers in
the std library where assignment like this makes sense (deque, vector,
list.) The nice thing about std::fill is that is works for types other
than numerics.
std::vector<std::string> v1(100);
std::fill(v1.begin(), v1.end(), std::string("Hello World"));
Now each elements contains the string "Hello World". In this case this
could have been written:
std::vector<std::string> v1(100, "Hello World");
as well, but we were talking about std::fill() :)
Hope that helps some,
joe