SOURCE CODE C++ Memory Manipulation Basics (Pointers, memcpy, etc.)
by Amnesia - 15-07-25, 06:01 PM
#1
Hello bitches

Heres a quick rundown of how u can manipulate memory directly in C++ great for low-level control or learning how things work under the hood

1. Using Pointers
Code:
int value = 42;
int* ptr = &value;
*ptr = 100;
std::cout << "Value: " << value << std::endl;

2. Dynamic Memory Allocation
Code:
int* ptr = new int(42);
*ptr = 55;
delete ptr;

For arrays:
Code:
int* arr = new int[5];
arr[0] = 1;
delete[] arr;

3. memcpy / memset
Code:
#include <cstring>
int arr[5] = {1, 2, 3, 4, 5};
std::memset(arr, 0, sizeof(arr));
std::memcpy(arr + 2, arr, 2 * sizeof(int));

4. Reinterpreting Memory
Code:
float f = 3.14f;
int* i = reinterpret_cast<int*>(&f);
std::cout << *i << std::endl;
5. Pointer Arithmetic
Code:
int arr[3] = {10, 20, 30};
int* p = arr;
std::cout << *(p + 1) << std::endl;

Note: be careful when working w raw memory — it's powerful but easy to mess up always manage ur memory properly

Hope this helps someone learning the internals of C++
[Image: Untitled-removebg-preview-2.png][Image: Cool-Text-Cyber-Rapists-485454522038581.png]
@Amnesia - @deabec - @dvx - @SH8O
Reply
#2
cool tysm
Reply


Forum Jump:


 Users browsing this thread: 1 Guest(s)