开发者

Random String Generation

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

char *charStr;
int stringLength;

void genRandom() {
    static const char alphanum[] =
        "0123456789"
        "!@#$%^&*"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < stringLength; ++i) {
        charStr[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    charStr[stringLength] = 0;
}

int main()
{
    while(true)
    {
        genRandom();
        cout < charStr;
    }
    return 0;

}

The issue comes in wh开发者_如何学编程en you compile it. It will compile just fine but nothing displays and then the program will cease to run. So my question is, what is wrong with this code?


Several issues with your code:

cout < charStr;

should be:

cout << charStr;

If you compile with the g++ -Wall argument (warning all), that error becomes easily apparent.

Also, you never set the value of stringLength! This is one example of why you generally should not use global variables--it can be hard to keep track of them. The unset value of stringLength could do weird things depending on your compiler--many compilers will simply initialize the value to 0, but some will set it to a random value. This undefined behavior can cause major headaches, so be very careful about that, and try to always initialize your variables when appropriate (this is generally a bigger issue with pointers, but the problem can still remain for other variables).

A fixed program is below:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = sizeof(alphanum) - 1;

char genRandom()
{
    return alphanum[rand() % stringLength];
}

int main()
{
    while(true)
    {
        cout << genRandom();
    }
    return 0;

}

Still using global variables, but I think this is a bit more of an appropriate use of them. I'm not sure what you were trying to accomplish by having the global char* string, just a headache waiting to happen and not really giving any advantages in your code. Generally in C++ it's better to use the C++ standard library string when you can--though in this case, your code really didn't need strings.


stringLength is 0 so no random characters will be generated. Also you didn't allocate any memory for charStr but you're writing a 0 to NULL (a bad thing). Also I think that you mean cout << charStr and not just < (the less than comparison operator).

Actually you're lucky that running the program nothing happened... the manuals say that with this kind of code a monster could come out of one of your nosrils.


DashRantic said "The unset value of stringLength could do weird things depending on your compiler--many compilers will simply initialize the value to 0, but some will set it to a random value."

The C/C++ spec says that uninitialized GLOBAL variables are guaranteed to be initialized to 0 (see Uninitialized Structures in C). This is not true of automatics (local / stack variables inside a function) but it is of globals. So, if you are using a standards compliant compiler, stringLength is guaranteed to be 0.

Therefore, your code should produce no output because charStr[0] is set to null at the end of the for loop (which, again, according to the standard, is guaranteed not to be executed because the condition is tested before the first loop iteration).

Unfortunately, because of those same rules, charStr is guaranteed to be initialized to 0 as well so your null character is being written to address 0 (where charStr points). Depending on what your execution environment is, that may or may not cause a problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜