传递参数并给它们值会导致运行时错误 [英] Passing parameters and giving them values causes runtime error
问题描述
所以我有以下代码:
void emptyString(char* token, int size) {
for (int i=0; i < size; i++) token[i] = '\0';
}
void setDefaults(char uloginName[], char *home_directory, char *password, char *shell, char *gecos) {
emptyString(home_directory, sizeof(home_directory) - 1);
strcat(home_directory, "home/home/");
strcat(home_directory, uloginName);
}
int main(){
char buffer[256];
AccountInfo *tempAccount;
UserDB users;
char uloginName[9] = "Smith";
char homeDirectory[33];
int userID;
int groupID;
char password[17];
char shell[17];
char gecos[65];
int i, j, k;
char flag[3];
char IDString[6];
char command[11];
char blank = ' ';
setDefaults(uloginName, homeDirectory, password, shell, gecos);
return 0;
}
当我在Visual Studio中运行此代码时, / p>
When I run this code in Visual Studio I get a pop up window saying
运行时检查失败#2 - 堆叠变量'users'是
已损坏
Run-Time Check Failure #2 - Stack around the variable 'users' was corrupted
这是由于 setDefaults()
方法中的代码行引起的。我希望有人帮助我更正代码。我的目的是将home_directory的值设置为home / home / userloginname ,其中userloginname也是传递的变量之一。
This is caused due to the lines of codes in the setDefaults()
method. I was hoping someone helped me to correct the code. My intention is to set the value of home_directory to "home/home/userloginname" where userloginname would be one of the variables being passed as well.
EDIT:我刚刚发现我的代码中的prolem是 strcat()
方法。如果我使用 strcpy()
的第一个,它工作正常,但一旦我想添加更多,它显示一个弹出窗口说,变量用户
已损坏。下面是userDB类的代码:
I just found out the prolem in my code is the strcat()
method. If I use strcpy()
for the first one, it works fine, but once I want to add more, it shows me a pop up window saying that the variable users
got corrupted. Here is the code for the userDB class:
class UserDB
{
private:
AccountInfo* _accounts[200]; // store up to 200 accounts
unsigned int _size; // number of account stored
unsigned int _nextUid; // next user id to be assigned
unsigned int _defaultGid; // default group id
// other private methods necessary for this class
public:
// constructors(empty), destructor
void adduser( AccountInfo* newUser);
void showUsers();
void showPasswd();
void finger(char* userLoginName);class
int size(); // return the number of accounts stored (_size)
// and other public methods (mutators/setters, accessors/getters)
};
代码现在是,因为它在这里。也许我需要做构造函数或析构函数或者什么,但作为一个新手,因为没有办法知道是什么实际上导致错误,需要修复。
the code is, right now, as it is in here. Perhaps I need to make the constructor or destructor or whatnot, but as a newbie as have no way of knowing what is actually causing the error and needs to be fixed.
编辑:所以经过很长时间我发现strcat()是什么原因导致的错误,但是我需要做strcat声称做的事情。这是我的完整代码的副本 http://ideone.com/A5iWh ,以便有兴趣的人可以让我知道什么改变,因为我不知道还有什么要做。感谢。
So after a long while I found out strcat() is what is causing the error, however I need to do what strcat claims to do. Here is a copy of my complete code http://ideone.com/A5iWh so that whoever is interested can let me know what to change because I do not know what else to do. Thanks.
推荐答案
显着的问题:
// in getNextToken
while ((buffer[i] != delimeter) && (i < 256) && (j < tokenSize))
token[j++] = buffer[i++];
没有检查您是否已到达 []
,而不是长度检查。如果 cin.getline(buffer)
读取用户名p mypasswd?然后:
命令
填写命令
终止null。也许这不是一个问题... uloginName
也不插入终止空字符。 密码
读取16个字节。也许你会得到一个空终止符运气,如果密码足够短。
There's no check that you've reached the end of the contents of buffer[]
, other than a length check. What if cin.getline(buffer)
were to read "a username p mypasswd"? Then:
command
fills up command
but never puts a terminating null at the end. Maybe this isn't a problem...
uloginName
also doesn't insert a terminating null character.
password
reads 16 bytes. Maybe you'll get lucky with a null terminator, if the password was short enough.
这篇关于传递参数并给它们值会导致运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!