CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2008
    Posts
    2

    Question rand() & Microsoft ??!!!

    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 :
    Code:
    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 :


    and I got something strange :


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

    Thanks..

  2. #2
    Join Date
    Aug 2006
    Posts
    16

    Re: rand() & Microsoft ??!!!

    Your passing '&pass' (the address of the pointer) as opposed to 'pass' which is the pointer to the data.

    Code:
    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;
    }

  3. #3
    Join Date
    Jan 2008
    Posts
    2

    Talking Re: rand() & Microsoft ??!!!

    Thanks alot *io* ,

    it works fine now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured