summaryrefslogtreecommitdiff
path: root/cpp_alloc_test/alloctest.cpp
blob: 065dcc1ed49dc60f6e4e9e6f49ad29d47bff393e (about) (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <iostream>

class AllocTest{
    private: std::string *in_class_str = NULL;
    private: std::string *local_str_ptr = NULL;
    private: unsigned char *local_char_array_ptr = NULL;

    public: AllocTest(){
        return;
    }

    public: void is_heap_or_stack( void *mem_addr ){
        if ( mem_addr == NULL ){
            std::cout << "Error: address is NULL\n";
            return;
        }
        if ( ((size_t)mem_addr << 8) >= 0x55000000000000 && ((size_t)mem_addr << 8) <= 0x5fffffffffffff ){
            std::cout << "Address is in heap memory area (address is " << mem_addr << ")\n";
            return;
        }
        else if ( ((size_t)mem_addr << 8) >= 0x7f000000000000 ){
            std::cout << "Address is in stack memory area (address is " << mem_addr << ")\n";
            return;
        }
        else
        {
            std::cout << "Unknown memory area (address is " << mem_addr << ")\n";
            return;
        }
    }

    public: void test_local_std_string(){
        std::string local_str( "test string" );
        this->local_str_ptr = &local_str;
        this->is_heap_or_stack( static_cast<void*>(&local_str) );
        return;
    }

    public: void local_std_string_exists(){
        if ( this->local_str_ptr == NULL ){
            std::cout << "Local std::string no longer exists in memory\n";
            return;
        }
        if ( strlen(this->local_str_ptr->c_str()) == 0 ){
            std::cout << "Local std::string is 0 chars\n";
            return;
        }
        std::cout << "Local std::string is still non-NULL value: " << this->local_str_ptr->c_str() << "\n";
        this->local_str_ptr->assign( "" );
        return;
    }

    public: void test_new_std_string(){
        std::string *teststr = new std::string( "string created with new keyword" );
        this->is_heap_or_stack( static_cast<void*>(teststr) );
        delete teststr;
        return;
    }

    public: void test_in_class_str(){
        this->in_class_str = new std::string( "string created into class" );
        this->is_heap_or_stack( static_cast<void*>(this->in_class_str) );
        delete this->in_class_str;
        return;
    }

    public: void test_local_char_array(){
        unsigned char testarray[1024] = {0};
        memset( static_cast<void*>(&testarray), 0, sizeof(testarray) );
        memcpy( static_cast<void*>(&testarray), static_cast<const void*>("test contents"), strlen("test content") );
        this->local_char_array_ptr = reinterpret_cast<unsigned char*>(&testarray);
        this->is_heap_or_stack( static_cast<void*>(&testarray) );
        return;
    }

    public: void local_char_array_exists(){
        if ( this->local_char_array_ptr == NULL ){
            std::cout << "Local char array pointer is NULL\n";
            return;
        }
        if ( strlen(reinterpret_cast<const char*>(this->local_char_array_ptr)) == 0 ){
            std::cout << "Local char array is 0 chars\n";
            return;
        }
        std::cout << "Local char array has value: " << this->local_char_array_ptr << "\n";
        memset( static_cast<void*>(this->local_char_array_ptr), 0, strlen((const char*)this->local_char_array_ptr) );
        return;
    }

    public: ~AllocTest(){
        if ( this->local_str_ptr != NULL )
            this->local_str_ptr = NULL;
        return;
    }
};

int main( int argc, char *args[] ){
    AllocTest *alloc_test = new AllocTest();
    std::cout << "Testing class memory address\n";
    alloc_test->is_heap_or_stack( alloc_test );
    std::cout << "Testing locally created std::string\n";
    alloc_test->test_local_std_string();
    std::cout << "Checking if local std::string still exists in memory\n";
    alloc_test->local_std_string_exists();
    std::cout << "Testing std::string created with new keyword\n";
    alloc_test->test_new_std_string();
    std::cout << "Testing std::string created into class\n";
    alloc_test->test_in_class_str();
    std::cout << "Testing local char array\n";
    alloc_test->test_local_char_array();
    std::cout << "Checking if local char array exists still\n";
    alloc_test->local_char_array_exists();
    delete alloc_test;
    exit( EXIT_SUCCESS );
}