Pointer Use-Cases

ANSIF
2 min readJul 28, 2024

--

A pointer is a data type in C++ that holds the address of another variable of the same type, with the exception of void pointers, which I will explain later

//Pointer basics are added below
int num = 100;
int *num_ptr = #
cout << *num_ptr; //prints "100"
cout << num_ptr; //prints address of object a

char text[] = "hello";
char *text_ptr = text;
cout << text; //prints "hello"
cout << text_ptr; //prints "hello"

string word = "hello";
string *word_ptr = &word;
cout << *word_ptr; //print "hello"

//void pointer
void *vptr = &num or text or &word

Conversely, a void pointer must be typecast to another pointer type. For example, int *ptr = (int*) vptr;

Pointer Arithmetic's

char arr[] = {"A", "B", "C"}

int *ptr = &arr; //points to the first item = "A"

ptr++; //points to second item = "B"

ptr - ; //points back to first item = "A"

ptr = ptr + 2; //points to last item "C"

Real Use cases

  1. Managing memory for user input sizes
int* userInputArray = new int[100]; // Allocate memory for 100 integers
// Use the array
delete[] userInputArray; // Deallocate memory

2. Building dynamic data structures for social networks

struct User {
std::string name;
User* next;
};
User* firstUser = new User{"Alice", nullptr};
firstUser->next = new User{"Bob", nullptr};

3. Handling different user commands in a menu-driven program

void openFile() { std::cout << "File opened" << std::endl; }
void saveFile() { std::cout << "File saved" << std::endl; }
void (*commandFunc)() = openFile; // Assign function pointer
commandFunc(); // Execute function

4. Automatically manage database connections

#include <memory>
std::unique_ptr<DatabaseConnection> dbConn = std::make_unique<DatabaseConnection>("db_url");
// Connection is automatically closed when dbConn goes out of scope

5. Rendering different shapes in a graphics application.

class Shape { public: virtual void draw() = 0; };
class Circle : public Shape { public: void draw() override { std::cout << "Draw Circle" << std::endl; } };
Shape* shapePtr = new Circle();
shapePtr->draw(); // Output: Draw Circle
delete shapePtr;

6. Interact with hardware registers and memory-mapped devices.

volatile uint32_t* GPIO_PORT = reinterpret_cast<uint32_t*>(0x40021000); // Base address of GPIO port
*GPIO_PORT |= (1 << 5); // Set bit 5 of the port

7. Controlling GPIO pins in an embedded system

volatile uint32_t* GPIO_PORT = reinterpret_cast<uint32_t*>(0x40021000); // Base address of GPIO port
*GPIO_PORT |= (1 << 5); // Set bit 5 to turn on an LED

8. Custom allocator for game engine memory management

class GameAllocator {
public:
void* allocate(size_t size) { return malloc(size); }
void deallocate(void* ptr) { free(ptr); }
};
GameAllocator allocator;
void* spriteMemory = allocator.allocate(1024);
allocator.deallocate(spriteMemory);

9. Sharing sensor data between different components in a robotics system

void processSensorData(int* data, int size) {
for (int i = 0; i < size; ++i) data[i] += 1; // Process data
}
int sensorData[] = {1, 2, 3, 4, 5};
processSensorData(sensorData, 5);
for (int val : sensorData) std::cout << val << " "; // Output: 2 3 4 5 6

--

--

ANSIF
ANSIF

No responses yet