Answers for "dynamic memory basic examples in c++"

C
1

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);                                  //освобождение памяти
Posted by: Guest on February-04-2022
1

dynamic memory allocation c++

string* str_arr = nullptr;
str_arr = new string[10];

//initialize
str_arr[0] = "Hello";
str_arr[1] = " World!";
Posted by: Guest on January-30-2022

Code answers related to "C"

Browse Popular Code Answers by Language