Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
How to Allocate Dynamic Memory (C++)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
In C++, dynamic memory allocation allows you to allocate memory during runtime rather than at compile time. This is useful when you don'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's how to use each of them: 1. Using `new` keyword: The `new` keyword is used to allocate memory for a single object. Here's an example: ```c++ int* p = new int; // Allocates memory for a single integer *p = 10; // Sets the value of the allocated memory to 10 ``` 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. To deallocate the memory allocated using `new`, you can use the `delete` keyword as follows: ```c++ delete p; // Deallocates the memory allocated using new ``` 2. Using `malloc()` function: The `malloc()` function is used to allocate memory for multiple objects. Here's an example: ```c++ int n = 5; // Number of integers to allocate memory for int* p = (int*)malloc(n * sizeof(int)); // Allocates memory for n integers for (int i = 0; i < n; i++) { *(p + i) = i + 1; // Sets the value of each integer to its index + 1 } ``` 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. To deallocate the memory allocated using `malloc()`, you can use the `free()` function as follows: ```c++ free(p); // Deallocates the memory allocated using malloc ``` It'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.
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)