Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

is it safe to memset a vector with zero?

3,305 views
Skip to first unread message

pango

unread,
Apr 19, 2008, 5:34:00 AM4/19/08
to
like below code:
std::vector<int> v;
memset(&v,0,sizeof(v));

is it safe?Can I write this kind of code?

Bo Persson

unread,
Apr 19, 2008, 5:53:47 AM4/19/08
to

No, definitely not!

And you don't have to, the vector will initialize itself properly.


Bo Persson


pango

unread,
Apr 19, 2008, 9:40:00 AM4/19/08
to

"Bo Persson" wrote:

> I want to know is the code will hurt program's stability?Will it cause program crash ?

Bo Persson

unread,
Apr 19, 2008, 11:10:04 AM4/19/08
to

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


Alex Blekhman

unread,
Apr 19, 2008, 11:12:59 AM4/19/08
to
"pango" wrote:
> I want to know is the code will hurt program's stability?Will it
> cause program crash ?

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


Igor Tandetnik

unread,
Apr 19, 2008, 11:16:37 AM4/19/08
to
"pango" <pa...@discussions.microsoft.com> wrote in message
news:D757E98F-A682-40F2...@microsoft.com

> "Bo Persson" wrote:
>
>> pango wrote:
>>> like below code:
>>> std::vector<int> v;
>>> memset(&v,0,sizeof(v));
>>>
>>> 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.
>
> I want to know is the code will hurt program's stability?Will it
> cause program crash ?

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.


David Wilkinson

unread,
Apr 20, 2008, 8:02:16 AM4/20/08
to

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

Stephen Howe

unread,
Apr 21, 2008, 6:38:03 AM4/21/08
to
>> I want to know is the code will hurt program's stability?Will it cause
>> program crash ?

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


Joe Greer

unread,
Apr 21, 2008, 12:45:06 PM4/21/08
to
=?Utf-8?B?cGFuZ28=?= <pa...@discussions.microsoft.com> wrote in
news:D33CAA85-FFD7-412E...@microsoft.com:

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


0 new messages