From ab607fc39b6dfc766f7481c33e5f1cf35a2f55d9 Mon Sep 17 00:00:00 2001 From: stderr64 Date: Thu, 24 Oct 2024 23:04:43 +0300 Subject: Recreated repository --- cpp_alloc_test/Makefile | 7 ++++ cpp_alloc_test/alloctest.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 cpp_alloc_test/Makefile create mode 100644 cpp_alloc_test/alloctest.cpp (limited to 'cpp_alloc_test') diff --git a/cpp_alloc_test/Makefile b/cpp_alloc_test/Makefile new file mode 100644 index 0000000..cc90ca6 --- /dev/null +++ b/cpp_alloc_test/Makefile @@ -0,0 +1,7 @@ +CC=g++ + +rel: + $(CC) -pedantic -Werror -Wall -O2 alloctest.cpp -o alloctest + +debug: + $(CC) -pedantic -Werror -Wall -O2 -ggdb alloctest.cpp -o alloctest diff --git a/cpp_alloc_test/alloctest.cpp b/cpp_alloc_test/alloctest.cpp new file mode 100644 index 0000000..0671901 --- /dev/null +++ b/cpp_alloc_test/alloctest.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include + +class AllocTest{ + private: std::string *in_class_str = NULL; + private: std::string *local_str_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(&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; + } + std::cout << "Local std::string is still non-NULL value: " << this->local_str_ptr->c_str() << "\n"; + 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(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(this->in_class_str) ); + delete this->in_class_str; + 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(); + delete alloc_test; + exit( EXIT_SUCCESS ); +} -- cgit v1.2.3-86-g962b