<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://freemwiki.com/index.php?action=history&amp;feed=atom&amp;title=How_to_Allocate_Dynamic_Memory_%28C%2B%2B%29</id>
	<title>How to Allocate Dynamic Memory (C++) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://freemwiki.com/index.php?action=history&amp;feed=atom&amp;title=How_to_Allocate_Dynamic_Memory_%28C%2B%2B%29"/>
	<link rel="alternate" type="text/html" href="https://freemwiki.com/index.php?title=How_to_Allocate_Dynamic_Memory_(C%2B%2B)&amp;action=history"/>
	<updated>2026-05-13T00:38:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>https://freemwiki.com/index.php?title=How_to_Allocate_Dynamic_Memory_(C%2B%2B)&amp;diff=5716&amp;oldid=prev</id>
		<title>Lukegao1: 创建页面，内容为“In C++, dynamic memory allocation allows you to allocate memory during runtime rather than at compile time. This is useful when you don&#039;t know the size of the memory required until runtime or when the size of the required memory changes during runtime.  To allocate dynamic memory in C++, you can use the `new` keyword or the `malloc()` function. Here&#039;s how to use each of them:  1. Using `new` keyword:  The `new` keyword is used to allocate memory for a single ob…”</title>
		<link rel="alternate" type="text/html" href="https://freemwiki.com/index.php?title=How_to_Allocate_Dynamic_Memory_(C%2B%2B)&amp;diff=5716&amp;oldid=prev"/>
		<updated>2023-03-21T08:36:46Z</updated>

		<summary type="html">&lt;p&gt;创建页面，内容为“In C++, dynamic memory allocation allows you to allocate memory during runtime rather than at compile time. This is useful when you don&amp;#039;t know the size of the memory required until runtime or when the size of the required memory changes during runtime.  To allocate dynamic memory in C++, you can use the `new` keyword or the `malloc()` function. Here&amp;#039;s how to use each of them:  1. Using `new` keyword:  The `new` keyword is used to allocate memory for a single ob…”&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In C++, dynamic memory allocation allows you to allocate memory during runtime rather than at compile time. This is useful when you don&amp;#039;t know the size of the memory required until runtime or when the size of the required memory changes during runtime.&lt;br /&gt;
&lt;br /&gt;
To allocate dynamic memory in C++, you can use the `new` keyword or the `malloc()` function. Here&amp;#039;s how to use each of them:&lt;br /&gt;
&lt;br /&gt;
1. Using `new` keyword:&lt;br /&gt;
&lt;br /&gt;
The `new` keyword is used to allocate memory for a single object. Here&amp;#039;s an example:&lt;br /&gt;
&lt;br /&gt;
```c++&lt;br /&gt;
int* p = new int; // Allocates memory for a single integer&lt;br /&gt;
*p = 10; // Sets the value of the allocated memory to 10&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
In the above example, we are allocating memory for a single integer and assigning the value 10 to it. Note that we can use the dereference operator (`*`) to access the value stored in the allocated memory.&lt;br /&gt;
&lt;br /&gt;
To deallocate the memory allocated using `new`, you can use the `delete` keyword as follows:&lt;br /&gt;
&lt;br /&gt;
```c++&lt;br /&gt;
delete p; // Deallocates the memory allocated using new&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
2. Using `malloc()` function:&lt;br /&gt;
&lt;br /&gt;
The `malloc()` function is used to allocate memory for multiple objects. Here&amp;#039;s an example:&lt;br /&gt;
&lt;br /&gt;
```c++&lt;br /&gt;
int n = 5; // Number of integers to allocate memory for&lt;br /&gt;
int* p = (int*)malloc(n * sizeof(int)); // Allocates memory for n integers&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
    *(p + i) = i + 1; // Sets the value of each integer to its index + 1&lt;br /&gt;
}&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
In the above example, we are allocating memory for n integers and assigning the value of each integer to its index + 1. Note that we can use pointer arithmetic to access the value stored in the allocated memory.&lt;br /&gt;
&lt;br /&gt;
To deallocate the memory allocated using `malloc()`, you can use the `free()` function as follows:&lt;br /&gt;
&lt;br /&gt;
```c++&lt;br /&gt;
free(p); // Deallocates the memory allocated using malloc&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
It&amp;#039;s important to note that when using `malloc()`, you must cast the result to the appropriate type of pointer before assigning it to a pointer variable. Additionally, `malloc()` returns a `void*` pointer, which cannot be dereferenced. Therefore, you must use pointer arithmetic to access the value stored in the allocated memory.&lt;/div&gt;</summary>
		<author><name>Lukegao1</name></author>
	</entry>
</feed>