Click to See Complete Forum and Search --> : rand() & Microsoft ??!!!


AraK
February 10th, 2008, 09:47 PM
Hi,

this is my first post in this great forum.

I needed to produce an array of random numbers. so I decided to dumb the array to a file for testing purpose only. I used this function :

void GeneratePass( const char* passPath, size_t units ){

std::ofstream passFile;
size_t *pass = new size_t[units];

srand( __time32_t(NULL) );

for( size_t C = 0; C < units; C++ )
pass[C] = rand();

passFile.open( passPath, std::ios::binary );
passFile.write( reinterpret_cast<char*>(&pass), units * sizeof(size_t) );
passFile.close();

delete[] pass;

return;
}

I opened the file using Notepad++ to see the file in hex mode, I got repeated values :
http://up3.m5zn.com/uploads/611b12dde8.gif

and I got something strange :
http://up3.m5zn.com/uploads/bfe96b9f2f.gif

could someone please explain what's happening ??!!!
I am using VC++ 2008 Express, WinXP SP2.

Thanks..

*io*
February 11th, 2008, 02:19 AM
Your passing '&pass' (the address of the pointer) as opposed to 'pass' which is the pointer to the data.

void GeneratePass( const char* passPath, size_t units ){

std::ofstream passFile;
size_t *pass = new size_t[units];

srand( __time32_t(NULL) );

for( size_t C = 0; C < units; C++ )
pass[C] = rand();

passFile.open( passPath, std::ios::binary );
passFile.write( reinterpret_cast<char*>(pass), units * sizeof(size_t) );
passFile.close();

delete[] pass;

return;
}

AraK
February 11th, 2008, 04:37 AM
Thanks alot *io* ,

it works fine now.