In my code I have an Array of class User as a private variable in class UserManager. When UserManager::Shutdown() is called it save the Array of Users to a file. Yet I get an access violation error. The Access violation error occurs in fwrite.c
I am compiling on Visual Studio 2010 running Windows 7.
User Class
class User {
public:
User() {
memset(username, 0, 255);
memset(password, 0, 255);
}
int userId;
char username[255];
char password[255];
};
MAX_USERS Definition
#define MAX_USERS 2048
User Manager Constructor
UserManager::UserManager() {
memset( users, 0, MAX_USERS);
}
Shutdown Function
void UserManager::Shutdown( void) {
FILE *userDB = fopen( "users.bin", "wb");
int k=1;
for(k=1;k<MAX_USERS-1;k++){
if (users[k]!=NULL) {
fwrite((const void*)&users[k]->userId, sizeof(int), 1, userDB);
fwrite((const void*)users[k]->username, 255, 1, userDB);
fwrite((const void*)users[k]->password, 255, 1, userDB);
} else {
fpos_t skip = 255 + 255 + sizeof(int);
fsetpos( userDB, &skip);
}
}
fclose( userDB);
}
The array 'users' is memset to zero at the constructor.
usersis declared, and how it is initialized. My guess: it is an array of uninitialized pointers toUser.