Thread: Simple Question memset(vector<int>, 0, sizeof(vector<int>))

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Post Simple Question memset(vector<int>, 0, sizeof(vector<int>))

    For some reason I'm having a brain cramp. I'm guessing this is really bad due to the functions within the vector class, but figured I might as well query "the mob".

    Code:
    #include <vector>
    
    struct foo
    {
      std::vector<int> fooVec;
    };
    
    foo var1;
    
    memset(&var1, 0, sizeof(foo));
    Bad, okay, or just safer to swap out the STL vector with an int*?

    (Yes in my example zeroing the memory is pointless, but imagine it having like 15+ variables in addition to the vector that I wish to quickly initialize to Zero)

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by HyperShadow View Post
    For some reason I'm having a brain cramp. I'm guessing this is really bad due to the functions within the vector class, but figured I might as well query "the mob".

    Code:
    #include <vector>
    
    struct foo
    {
      std::vector<int> fooVec;
    };
    
    foo var1;
    
    memset(&var1, 0, sizeof(foo));
    Bad, okay, or just safer to swap out the STL vector with an int*?

    (Yes in my example zeroing the memory is pointless, but imagine it having like 15+ variables in addition to the vector that I wish to quickly initialize to Zero)
    I'm not sure why you're doing that? If you didn't add anything to var1.fooVec, the size of the vector will be 0.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by HyperShadow View Post
    Code:
    #include <vector>
    
    struct foo
    {
      std::vector<int> fooVec;
    };
    
    foo var1;
    
    memset(&var1, 0, sizeof(foo));
    BAD! - Do NOT do that!
    If the vector is not already empty and you want to empty it, use either
    foo.fooVec.clear();
    or to release any reserved memory as well:
    foo.fooVec.swap(std::vector<int>());
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's usually a very bad idea to zero out classes. If you multiple variables in a struct, then you can bunch them together and take their address to start from there. Just be sure to match the size now since you're excluding variables.
    Or just make it a class/struct which has a constructor which inits them to 0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    I'm not sure why you're doing that? If you didn't add anything to var1.fooVec, the size of the vector will be 0.
    Yep, isn't my intention to set all vector elements to Zero, but rather initialize an entire struct that contains an STL dynamic array without adding a function to my struct.

    Ex:
    Code:
    struct foo{
      int a;
      float b;
      double c;
      vector<int> d;
    };
    
    foo var1;
    
    ZeroMemory(&var1, sizeof(foo));
    So think the following would be safe?

    Code:
    ZeroMemory(&var1, sizeof(foo) - sizeof(vector<int>));  //Assuming of course my vector is at the end of the struct.
    
    I'm assuming the sizeof() a class never changes... and yep I should probably stop being lazy and just write a constructor... but I'm afraid I will want to down the road do a binary dump of my struct to a file and I'm looking for more "data storage" struct rather than a full class with Read() & Write() members, etc.

    Code:
    file.Write(&var1, sizeof(foo) - sizeof(vector<int>));
    file.Write(&var1.d[0], var1.d.size() * sizeof(int));
    Thanks for any input.

    edit: Ah I remember why I was posting this question... quickly initializing embedded structs.

    Example:
    Code:
    struct one{
      int a;
      int b;
      float c;
      double d;
    
      BYTE *data;
    
      two innerStruct;
    };
    
    struct two{
      int a;
      int b;
      int c;
      int d;
    
      vector<int> e;  <--- using STL vector to not have to worry about mem. management.
    };
    
    one outerStruct;
    ZeroMemory(&one, sizeof(outerStruct));
    Guess there is no easy way to do this without either.

    a) Writing constructor for both structs.
    b) Initializing of all veriables at each declaration.
    c) Replacing all STL classes with simple pointers.
    d) Possibly if careful enough ZeroMemory(&one, sizeof(outerStruct) - sizeof(vector<int>), which would really hinder growth.
    Last edited by HyperShadow; 12-10-2007 at 01:41 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Classes are complex objects and generally it's a bad idea to serialize them directly. Instead consider making a Write member function that serializes the object.
    If you use MFC, you can also use CArchive.

    Ah, but if you are using C++, then you can actually use a trick.
    Consider this:

    Code:
    class b
    {
    	int n1, n2, n3, n4, 5;
    	// Some other variables (NOT CLASSES OR STRUCTS)
    };
    
    class a: public b
    {
    	std::vector<int> myvec;
    	// Put other classes or structs here. Member that you should not initialize to 0.
    };
    
    a mya;
    ZeroMemory(&mya, sizeof(b));
    However, remember that this kind of a hack. And if you use virtual functions, it gets trickier because the vtable will use 4 bytes at the beginning of the class.
    I wouldn't rely on this too much. It's better to make constructors to initialize data to 0. It's the best, safest and most portable way.
    Last edited by Elysia; 12-10-2007 at 02:08 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The moment you do anything that isn't possible in C, memset and friends are off-limits.

    but I'm afraid I will want to down the road do a binary dump of my struct to a file
    That's another thing that's not possible the moment you have anything but primitives in the struct.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM