fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. int main()
  6. {
  7. const int N = 4; // consider = sizeof(char*) instead
  8.  
  9. // your memory allocation
  10. char* buffer = static_cast<char*>(malloc(N));
  11.  
  12. // the pointer b will accesses the value of buffer as if it was a char array
  13. char* b = reinterpret_cast<char*>(&buffer);
  14.  
  15. // copy the char array from b to where buffer is pointing
  16. std::copy(b, b + N, buffer);
  17.  
  18. // output
  19. printf("The pointer returned by malloc was %p\n", buffer);
  20. printf("The contents of buffer[0]-buffer[%d] are: ", N-1);
  21. for(int i=0; i<N; ++i)
  22. printf("0x%02x ", (unsigned char)buffer[i]);
  23. printf("\n");
  24.  
  25. free(buffer);
  26. }
Success #stdin #stdout 0s 2856KB
stdin
Standard input is empty
stdout
The pointer returned by malloc was 0x8490008
The contents of buffer[0]-buffer[3] are: 0x08 0x00 0x49 0x08