This is how the entire task looks like completely - the main thread creates two other threads suspended and after creating them starts the first one The first of them allocates a memory block of 200Kb in size, fills it with random numbers and starts the second one, which writes the generated random numbers to a file, frees the allocated memory and ends

like this I have at the moment

#include "pch.h" #include <iostream> #include <windows.h> #include "conio.h" #include <vector> //#include "stdafx.h" using namespace std; const int Length =200 * 1024; unsigned char *buffer = new unsigned char[Length]; int num; DWORD WINAPI thread2(LPVOID); DWORD WINAPI thread3(LPVOID); int main() { // create threats HANDLE thread = CreateThread(NULL, 0, thread2, NULL, 0, NULL); HANDLE thread1 = CreateThread(NULL, 0, thread3, NULL, 0, NULL); // stop threads SuspendThread(thread); SuspendThread(thread1); ResumeThread(thread); _getch(); return 0; } DWORD WINAPI thread2(LPVOID t) { for (int i = 0; i< Length; i++) { buffer[i] = (int)rand() % Length + 1; } for (int i = 0; i < Length; i++) { cout « (int)buffer[i] « " "; cout « endl; } delete[]buffer; //cout « "wwwwwwwwwwwwwwwwwwww"; return 0; } DWORD WINAPI thread3(LPVOID n ) { return 0; } 

Closed due to the fact that the essence of the question is not clear by the participants nick_n_a , HolyBlackCat , αλεχολυτ , 0xdb , aleksandr barakin 27 Nov '18 at 5:54 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What does "filled with random numbers" mean? Numbers of what size? One byte? More? From where and why did you suddenly get some int *Arr and why exactly int ? - AnT
  • If I understood the task correctly, then it’s necessary that these 200kbytes are filled with random integers. In any case, the task says “The first one (stream) allocates a 200Kb memory block, fills it with random numbers and starts the second stream” - Anton
  • Here you have allocated a memory block in the first line. - nick_n_a
  • Instead of * buffer, you need 200 * 1024 all the same. Arr needs buffer instead. The second line is not needed. After edits, the extra piece of code will go away. Everything. - nick_n_a
  • @ Anton If you need a block int s, then why did you select a block of bytes? - AnT

1 answer 1

It is good to open a book first and become a little stronger.

At first I looked at it.

 byte *buffer = new byte [200*1024]; 

normally allocated memory, 200 kilobytes (although disk manufacturers will say that there is 204.8 kilobytes). True std :: byte type is available only with 17 pluses, which means that not every school compiler will take it. But maybe it's just an ordinary #define byte char

But then I saw it

 int *Arr = new int[*buffer]; 

buffer is a pointer to an array, and *buffer is *(buffer+0) , that is, buffer[0] . That is, we select another array of int whose size is recorded in the first element of the array. Very interesting. The fact is that buffer [0] itself is not yet initialized, therefore, the code can allocate any number of bytes (in a certain range, of course).

Look further

 for(int i=-; i< *buffer; i++) 

clearly corresponded with a copier. The expression int i=-; not valid. And most likely it meant int i=0; .

But this expression did not end there. We look into the second half of i< *buffer - while i is less than the value of the zero element (uninitialized array). Oh. This is a complete oh. But looking down at the next cycle, it seems to me that by *buffer author meant the size of the array. Then everything falls into place. Even this line num = rand()% *buffer +1; makes sense. But it also suggests that byte is not std :: byte. And rand doesn’t seem like that

let's go down. There awaits us delet[]Arr; . obviously unfinished letter e. OK. True, it is in a loop, but what a difference now :) now you can write as it should.

Just fixed, but still terrible version.

 using namespace std; const int LEN = 200*1024; unsigned char *buffer = new unsigned char [LEN]; int num; for(int i=0; i< LEN; i++) { buffer[i] = (int)rand()% LEN +1; } for(int i = 0; i< LEN; i++) { cout << buffer[i] << " "; cout << endl; } delete[]buffer; 

Conclusion: Open a book or outline. Read.

And streams .. And there are no streams in the program But judging by the question, looking for something like Consumer-producer. But this is a bit more complicated code. For example, somewhere like that .

  • With this listing, my characters are constantly printed, shouldn't there be a bit of them? and is filled not with numbers but with letters - Anton
  • one
    correct the output like this cout << (int)buffer[i] << " "; - KoVadim
  • added in principle how the full task looks like - Anton