language: C++ 4.7.2 (gcc-4.7.2)
date: 588 days 2 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
 
using namespace std;
 
// macros are bad...
#define PR(EX) cout << (#EX) << ": " << (EX) << endl
 
void f1( void* v, size_t num_bytes, int val){
 
        unsigned char* p_addr = static_cast<unsigned char*>(v);
        unsigned char byte = static_cast<unsigned char>(val);
 
        for( size_t i = 0; i < num_bytes; i++){
                *p_addr = byte++;
                p_addr++;
        }
        PR(num_bytes);
}
 
int main(){
 
        int a[5] = { 0, 0, 0, 0, 0 };
        int value = 67;
        void* vp = static_cast<void*>(&a);
        unsigned char* byte = static_cast<unsigned char*>(vp);
 
        for( size_t i = 0; i < (sizeof(a) / sizeof(a[0])); i++){
                cout << "a[" << i << "] = ";
                for( size_t j = 0; j < sizeof(int); j++){
                        cout << static_cast<int>(*byte) << " "; // print int value
                        byte++;
                }
                cout << endl;
        }
//      PR(*byte);
 
        f1(vp, sizeof(a), value);    
 
        byte = static_cast<unsigned char*>(vp); // reset pointer
 
        for( size_t i = 0; i < (sizeof(a) / sizeof(a[0])); i++){
                cout << "a[" << i << "] = ";
                for( size_t j = 0; j < sizeof(int); j++){
                        cout << *byte << " "; // print ASCII character
                        byte++;
                }
                cout << endl;
        }
}