You cannot return raw arrays from a function. You can, however return a pointer to the first element of an array - that array cannot be a local one, as it would be destroyed when the function exits.
How does that even compile?
It's not safe to return a static array from a function, as the array is destructed as soon as the function returns.
Instead, use dynamically allocated array, or std::string.
I just re-compiled it... It's working without any changes... I am not sure what is it doing, nor am I sure why the array is not being destructed... Here is the actual full code... It is returning something, but it is not what I desire... I want to generate a random hash of 15 char, it is returning 7 char... Also, I want the hash to be all alphabets, but it is returning some non-alpha chars anyway. Please take the time to have a thorough look and let me know what would be the "proper" and "professional" way to do it... Thank you...
Your array is deallocated at the end of the function. You can't return auto arrays. You'll need to allocate them dynamically or use some sort of container
eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
char* generateHash(int numBytes)
{
//...
char *buffer = newchar [numBytes]; // allocate the array
//...
return buffer;
}
int main(int *argc, char *argv[])
{
char *p = generateHash(15);
cout << p <<endl; // remember that cout << p ends when it finds a zero character
delete[] p; // free the array
return 0;
}
1- I am not sure I have the concept of allocation and de-allocation of pointers... See, this is the "Beginners" section... :)
2- I edited the code and re-executed it... It shows the same O/P, this time a little bit more gibberish.
Ed^êJ|ÄÆ]uztvngmíl┌Ö ÿ
3- I re-edited the code and turned all the chars to ints (except the one in arguments of main)... This showed me (I think) what was running behind the char mask... And the output looks to me like an address in memory (thanks to Embedded-Systems classed :P)... So, I think it is printing the memory address only. Or maybe it is printing the memory address only when it has been explicitly told to deal with it as an int... I dunno. I can't think as deep as you. I aspire to. So enlighten me, por favor.
3. When trying to print a char pointer, it is interpreted as a null-terminated char array (i.e. a C string).
For an int pointer, the address is printed. So you'll have to print each element manually.
Few things to note: for (int i=0; i<=numBytes; i++) if the size of the array is numBytes this will overflow ( Use < instead of <= ) buffer[i] = rand() % 90 + 65; generates numbers from 65 to 155 ( 65 + 90 )
You never give the terminator to your string ( the zero char ).
So kind of you to point out... Corrected the errors, but it is still printing way lot more than 15 chars and some of them gibberish.
From my classes about 8086-debug I remember that some memory locations already have values. I think since the cout statement in main() keeps printing until it encounters a NULL character, it is going on and printing the non-NULL values it encounters even after the desired segment of memory addresses has ended. Is that so?
That would require everything that used a C-style string to know the size somehow. If you want a string that knows it's size, just use the C++ std::string.