Passing parameters and giving them values causes runtime error?

Up vote 0 down vote favorite share g+ share fb share tw.

So I have the following code: void emptyString(char* token, int size) { for (int i=0; I EDIT: 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* _accounts200; // 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. EDIT: 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. C++ segmentation-fault runtime-error link|improve this question edited Feb 1 at 7:23Yokhen1487 asked Feb 1 at 6:14user1182024.

1 This code, as written, doesn't compile. There is no definition for AccountInfo or UserDB. If I add dummy definitions (i.e.

Struct AccountInfo; and struct UserDB {};, the code compiles and runs without error for me (running VC++10). The error message indicates a stack overflow, but there's no stack overflow in the currently listed code. – Managu Feb 1 at 6:38 Yes sorry man, I should have know it was better to just post the complete code.

Here it is. Thanks for all the help. – user1182024 Feb 1 at 6:45 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 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 and sorry for all the trouble. – Yokhen Feb 1 at 7:04 You can fix the compiler errors by adding #include and #include to the beginning of your source file.

– Blastfurnace Feb 1 at 7:08 Oh my, is this homework? – Managu Feb 1 at 7:18.

EmptyString(home_directory, sizeof(home_directory) - 1); You have n problem here: sizeof(home_directory) This statement gives you size of the pointer home_directory, not the array. If you want the size of array inside the function then you will have to pass it explicitly as an function parameter. Also, further, Your variables are not initialized causing Undefined Behavior.

Specifically, in the code snippet shown uloginName. You will need to initialize all of your variables lest they will cause problems all over your project.

1 I just added the emptyString() method. I personally do not see a problem with it, but I would appreciate it if you let me know if there is something wrong with it. It has worked with other methods, though not with the setDefaults() one.

– user1182024 Feb 1 at 6:21 Your first reasoning of "sizeof() giving wrong result" is correct, but I think in the OP's context, it would not give a runtime error. – iammilind Feb 1 at 6:24 @iammilind: I or anyone wouldn't know that unless the OP posted code for emptyString() which was posted After I posted the answer. – Als Feb 1 at 6:25 Yes, sorry about that.

– user1182024 Feb 1 at 6:27.

Notable problems: // in getNextToken while ((bufferi! = delimeter) && (i Getline(buffer) were to read "a username p mypasswd"? Then: at line 150, the copy into command fills up command but never puts a terminating null at the end.

Maybe this isn't a problem... at line 153 the read into uloginName also doesn't insert a terminating null character. At line 170 the read into password reads 16 bytes. Maybe you'll get lucky with a null terminator, if the password was short enough.

Strcat(home_directory, uloginName); ^^^^^^^^^^ uninitialized The reason is char uloginName9; is not initialized inside main() which is being passed to setDefaults(...) and thus it's not guaranteed to be a nul terminated string. The buffer could be overrunning out of bounds and the code goes in undefined behavior. Update: From the code you posted in the link, it seems to be in a mess.

You haven't initialized the character arrays, as I stated earlier. Moreover, I don't see any range checks performed, so the buffer may overrun easily. For example, uloginName is just of size 9 and it's highly likely that variable value of k can be more than that.

Which will cause a stack overflow and it will corrupt the values of the adjacent variables declared near to it. Try to use std::string instead of naked arrays if you are not comfortable with bound checking yet.

Oh dang, I forgot to add that too! I'm sorry! But that one was actually initialized!

I will change the code right now so that it is initialized to something. – user1182024 Feb 1 at 6:22 @RenatoAlegreMarchand, I don't see any error otherwise. Are you sure, you have posted the exact code?

E.g. Char uloginName9 = "smith"; – iammilind Feb 1 at 6:31 The code is way more complex than that, but the point is that uloginName is initialized way before setDefaults() is executed. I can put the whole thing in though if you wish.

– user1182024 Feb 1 at 6:38 @YokhenDq, may be you can put your minimal needed code in ideone.com and post a link to that as a comment. – iammilind Feb 1 at 6:40 Here you go sir. – user1182024 Feb 1 at 6:44.

Your error message indicates a "stack overflow. " You've got a fixed size buffer of some sort on your program's stack, and you're writing into it more than the buffer can hold. Whenever you declare a local variable in C or C++, some storage is allocated for it on the running function's stack.

Let me give an example. #include int main() { short above=0; char buffer1; short below=0; strcpy(buffer, ""); strcat(buffer, " world! "); printf("%d %d %s", above, below, buffer); } So I've declared buffer to have enough space to store 1 byte.

I try to put into it the 14 bytes of " world! \0". What do I get when I run this program?

27762 0 world! Now, I set 'above' to 0, explicitly. How did it become 27762?

Well, here's an illustration of what's actually happening: Stack: | .... | | ... | | misc platform stuff | | d! \0...| |---------------------| |--------| | storage for 'above' | 2 bytes = sizeof(short) | rl | |---------------------| |--------| | padding/cookie | system dependent |ello, wo| |---------------------| |--------| | storage for 'buffer'| 1 byte = sizeof(buffer) -> | H | |---------------------| |--------| | padding/cookie | system dependent |? | |---------------------| |--------| | storage for 'below' | 2 bytes = sizeof(short) | 0 0 | | ... | When I write into buffer, the first byte lands where it's supposed to be.

Then the next few bytes start writing over the 'padding/cookie' area. Eventually that area is overflowed, and we go on overwriting the next higher thing, 'above'. A quick check shows us that 27762 is how my machine interprets the characters rl in the middle of "world!

" as a short. Now, errors like this are stack buffer overflows, or "stack overflows". A malicious user can make use of some instances of stack overflows to take control of your program and make it do entirely different things from what it was designed to do.

Exploiting a stack overflow is a tried and true hacking technique. So much so that compiler writers and operating system designers have taken to a strategy of "defense in depth" to prevent stack overflows from being exploitable. Ideally, if some user manages to overwrite a bad portion of the stack, the toolchain tries to do the only reasonable thing: crash immediately.

In this particular case, what the compiler does is to insert a special hard-to-guess value right after the storage for 'buffer' called a 'stack cookie. ' When it leaves main, it checks to see that the 'stack cookie' hasn't changed. If it has, something naughty (probably a stack overflow) has happened, and so the program crashes immediately with the dialog you see.

How do we fix stack overflows? Code defensively. Never call strcpy, strcat, sprintf or their various friends.

Instead of using fixed size buffers (e.g. Char homeDirectory33;), use dynamic facilities such as std::string homeDirectory; to manage strings. Used properly, it's impossible to overflow a std::string in such a way that unintended memory is overwritten.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions