Skip to main content
gamedev.net
🔒 Locked

memset: compile error

Started by Alessandro 6 replies 4k views
Original Post
Alessandro
Alessandro
Hi, I'm using VC++ 2003 and I'm getting a compile error with the following code: #include MEMORYSTATUS memory; memset(&memory,sizeof(MEMORYSTATUS),0); cube.cpp(151) : error C2501: 'memset' : missing storage-class or type specifiers cube.cpp(151) : error C2365: 'memset' : redefinition; previous definition was a 'function' C:\Programmi\Microsoft Visual Studio .NET 2003\Vc7\include\string.h(102) : see declaration of 'memset' cube.cpp(151) : error C2078: too many initializers Any idea ? Thanks
SiCrane
SiCrane
memset() can't be called outside of a function.
Drew_Benton
Drew_Benton
Just as SiCrane says, however, you can achieve that specific behavior by using: MEMORYSTATUS memory = {0};

However, never, ever use that construct on a non-C user data type that contains non-C types as you will really mess up the memory. If you have C-structures or Win32 API structures, it is safe. I.e, if you have a struct with a STD anything in it, you should be using a constructor and not a memset or = {0};

Usually, it's better to write the code to clear global variables in the main entry point function, but depending on what you are doing, that might not always be possible at which case you might need to rethink your design. I usually have a global setup/cleanup function I call to handle those things when I have to use global variables in the program (in which some cases it's unavoidable).
SiCrane
SiCrane
Quote:
Original post by Drew_Benton
Just as SiCrane says, however, you can achieve that specific behavior by using: MEMORYSTATUS memory = {0};

However, never, ever use that construct on a non-C user data type that contains non-C types as you will really mess up the memory. If you have C-structures or Win32 API structures, it is safe. I.e, if you have a struct with a STD anything in it, you should be using a constructor and not a memset or = {0};

Actually, anything you think you would screw up with an aggregate initializer list just won't compile. Aggregate initializers can be used safely with classes that contain C++ class members. For example:
struct MyAggregate {  int i;  std::string s;};MyAggregate a = { 0 };

a.i will contain 0 and s will be a default constructed std::string.
Red Ant
Red Ant
Quote:
Original post by Drew_Benton
Just as SiCrane says, however, you can achieve that specific behavior by using: MEMORYSTATUS memory = {0};

However, never, ever use that construct on a non-C user data type that contains non-C types as you will really mess up the memory. If you have C-structures or Win32 API structures, it is safe. I.e, if you have a struct with a STD anything in it, you should be using a constructor and not a memset or = {0};


I think MEMORYSTATUS memory = {0} may not do what you think it does. It simply initializes memory's first member to 0 and initializes all remaining members to their default values. You are right about memset() being dangerous as it literally just overwrites the specified range of memory with zeroes, but = {} will not have that effect.
I agree that it's usually better to provide a proper constructor, though.

This is just IMHO, so feel free to correct me if I'm wrong.


P.S. Meh, beaten to it.

2nd P.S. Ooops, I now see Sicrane's last post was already there more than an hour before I posted this. Guess I should have scrolled down all the way.
Antheus
Antheus
This is WinAPI. The canonical way of handling such structures is still:
MEMORYSTATUS memory;ZeroMemory(&memory, sizeof(MEMORYSTATUS));


It's not general, but as far as WinAPI goes, it's perfectly valid way, since it was designed with this in mind. C++ arguments also don't apply since it's just C code.

In general case all of the above applies, but in this particular example I don't believe there's much need to overcomplicate the subject.
SiCrane
SiCrane
Quote:
Original post by Antheus
This is WinAPI. The canonical way of handling such structures is still:
MEMORYSTATUS memory;ZeroMemory(&memory, sizeof(MEMORYSTATUS));


Of course, ZeroMemory() can't be called outside of a function either.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.