Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added day 08 content #522

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions docs/day-08/String-Memory-Usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
---
sidebar_position: 2
title: "String Memory Usage in C++"
description: "In this tutorial, we will learn about memory usage of strings in C++ programming with the help of examples. Understanding memory usage is important for efficient string manipulation."
sidebar_label: "String Memory Usage in C++"
slug: String-Memory-Usage
---

# String Memory Usage in C++

This repository provides an explanation of memory usage for strings in C++, along with examples and techniques to understand and manage it effectively.

## Table of Contents

- [Introduction](#introduction)
- [Memory Allocation](#memory-allocation)
- [Capacity and Size](#capacity-and-size)
- [Shrink to Fit](#shrink-to-fit)
- [SBO (Small Buffer Optimization)](#sbo-small-buffer-optimization)
- [Memory Management Techniques](#memory-management-techniques)

## Introduction

Memory management is a crucial aspect when working with strings in C++. Efficient use of memory can improve the performance of your applications and prevent memory-related issues.

## Memory Allocation

In C++, `std::string` manages its memory dynamically. When you create or modify a string, memory is allocated on the heap to store its contents. The string class handles the allocation and deallocation of this memory.

### Example

```cpp
#include <iostream>
#include <string>

int main() {
std::string str = "Hello, World!";
std::cout << "String: " << str << std::endl;

return 0;
}
```

## Capacity and Size

#### Size

The `size()` or `length()` method returns the number of characters in the string:

```cpp

#include <iostream>
#include <string>

int main() {
std::string str = "Hello, World!";
std::cout << "Size: " << str.size() << std::endl;
std::cout << "Length: " << str.length() << std::endl;

return 0;
}
```

#### Capacity

The `capacity()` method returns the size of the storage space currently allocated for the string, expressed in terms of bytes. This capacity is always greater than or equal to the string size.

cpp
Copy code
#include <iostream>
#include <string>

int main() {
std::string str = "Hello, World!";
std::cout << "Capacity: " << str.capacity() << std::endl;

return 0;
}
Reserve
The reserve() method requests that the string capacity be at least enough to contain n characters. If n is greater than the current string capacity, the function increases the capacity.

```cpp

#include <iostream>
#include <string>

int main() {
std::string str;
str.reserve(50); // Reserves capacity for 50 characters
std::cout << "Reserved Capacity: " << str.capacity() << std::endl;

return 0;
}
```

## Shrink to Fit

The `shrink_to_fit()` method reduces the capacity of the string to fit its size. This can be useful for reducing memory usage after a large string has been shrunk.

```cpp

#include <iostream>
#include <string>

int main() {
std::string str = "Hello, World!";
str.reserve(100); // Reserves capacity for 100 characters
std::cout << "Capacity before shrink: " << str.capacity() << std::endl;

str.shrink_to_fit();
std::cout << "Capacity after shrink: " << str.capacity() << std::endl;

return 0;
}
```

## SBO (Small Buffer Optimization)

Small Buffer Optimization (SBO) is a technique used by many implementations of std::string to store small strings directly within the object, avoiding dynamic memory allocation. This optimization can improve performance for short strings by reducing the overhead of heap allocation.

### Example

```cpp

#include <iostream>
#include <string>

int main() {
std::string shortStr = "Hi"; // Likely to use SBO
std::string longStr = "This is a longer string that may not use SBO"; // Likely to use dynamic allocation

std::cout << "Short String: " << shortStr << std::endl;
std::cout << "Long String: " << longStr << std::endl;

return 0;
}
```

## Memory Management Techniques

#### Avoiding Excessive Copying

Using references or pointers can avoid excessive copying of strings:

```cpp

#include <iostream>
#include <string>

void printString(const std::string& str) { // Pass by reference
std::cout << "String: " << str << std::endl;
}

int main() {
std::string str = "Hello, World!";
printString(str);

return 0;
}
```

#### Using std::move

std::move can be used to transfer ownership of a string's data, avoiding costly deep copies:

```cpp

#include <iostream>
#include <string>

int main() {
std::string str1 = "Hello, World!";
std::string str2 = std::move(str1); // Moves str1 to str2

std::cout << "str2: " << str2 << std::endl;

return 0;
}
```

#### Avoiding Memory Fragmentation

Be mindful of operations that repeatedly change string size, as they can cause memory fragmentation. Using `reserve()` can help mitigate this by pre-allocating sufficient memory.

```cpp

#include <iostream>
#include <string>

int main() {
std::string str;
str.reserve(100); // Pre-allocates memory

for (int i = 0; i < 100; ++i) {
str += 'a'; // Avoids reallocations
}

std::cout << "Final string: " << str << std::endl;

return 0;
}
```
Loading