Search
 
SCRIPT & CODE EXAMPLE
 

CPP

dynamic memory c++

#include<iostream> //c++
#include<stdio.h> // c

C++:
	int* array = new int[n]; // одномерный массив

	free(array);      	 //можно также использовать delete(array); (освобождение памяти)




	int** array = new int*[n]; //двумерный массив (n строк)
	for (int i = 0; i < n; i++) {
		array[i] = new int[m];   	            //(m столбцов)
	}
	
	 for (int i = 0; i < n; i++)      //освобождение памяти
        delete[] array[i];
    delete [] array;

C:

    int**array = (int**)malloc(n * sizeof(int*));
    for (i = 0; i < n; i++) {
        array[i] = (int*)malloc(n * sizeof(int));
    }

     for (i = 0; i < n; i++)
        free(array[i]);               //освобождение памяти
     free(array);          



     int* array = (int*)malloc(n * sizeof(int));   //одномерный массив
     free(array);                                  //освобождение памяти

Comment

dynamic memory in c++

#include <iostream>

int main()
{
  int* ptr = new int(5);
  
  int* arr = new int[3];
  arr[0] = 96;
  arr[1] = 45;
  arr[2] = 72;
  
  std::cout << *ptr << "
";// output: 5
  
  for (int i = 0; i < 3; i++)
  {
    std::cout << arr[i] << " ";
  } // output: 96 45 72
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: double to float c++ 
Cpp :: matrix dynamic memory c++ 
Cpp :: quicksort geeksforgeeks 
Cpp :: c++ initialize static variable 
Cpp :: C++ continue with for loop 
Cpp :: inserting element in vector in C++ 
Cpp :: accumulate() in c++ 
Cpp :: c++ #define 
Cpp :: size() in c++ SET 
Cpp :: best time to buy and sell stock leetcode solution 
Cpp :: char to string c++ 
Cpp :: initialize vector 
Cpp :: how to convert string to int in c++ 
Cpp :: c++ initialise array 
Cpp :: what do you mean by smallest anagram of a string 
Cpp :: resize vector c++ 
Cpp :: substring in c++ 
Cpp :: c++ program to convert celsius to kelvin 
Cpp :: clear previous terminal output c++ 
Cpp :: resharper fold if statement c+ 
Cpp :: if argv == string 
Cpp :: balanced parentheses 
Cpp :: c++ random int troll 
Cpp :: c++ string find last number 
Cpp :: c++ include < vs "" 
Cpp :: c++ if else example 
Cpp :: Maximum sum of non consecutive elements 
Cpp :: C++ Vector Operation Delete Elements 
Cpp :: c++ threadpool 
Cpp :: calling by reference c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =