------- DMA_.CPP
#include <iostream>
using namespace std;
int main()
{
//int *arr = (int*)malloc(5 * sizeof(int));
// to allocate and initialize a block of memory to Garbage Value.
int* value1 = new int;
// int *arr = (int*)calloc(5, sizeof(int));
//to allocate and initialize a block of memory to zero.
int* value2 = new int();
// Assiging the value including DMA
int* value3 = new int(5);
// Allocate memory for an array of 5 ints and initialize them to zero
int* arr = new int[10]{}; // {} initializes to zero, similar to = {0}
int* arr1 = new int[10]; // {} initializes to zero, similar to = {0}
cout << *value1 << endl;
cout << *value2 << endl;
cout << *value3 << endl;
cout << "\n\n\n" << endl;
cout << "Values of the array:" << endl;
for (int i = 0; i < 10; ++i)
{
cout << "arr[" << i << "] = " << arr[i] << endl;
}
// Deallocate the memory
delete[] arr;
delete value1;
delete value2;
delete value3;
/*
In C, realloc is used to resize a previously allocated block of memory.
In C++, you can use new[] to allocate an array of elements and delete[]
to deallocate the entire array. Resizing is not directly supported by new and delete,
so you may need to manually allocate a new array, copy elements,
and then deallocate the old array.
*/
// int *resized_arr = (int*)realloc(arr, 10 * sizeof(int));
int *resized_arr = new int[10];
delete resized_arr;
return 0;
}
---------DMA_2
#include <iostream>
using namespace std;
void swap_by_value(int x, int y)
{
int temp = x;
x = y;
y = temp;
cout << x << endl;
cout << y << endl;
}
int main()
{
int a = 5, b = 10;
swap_by_value(a, b);
// Copies of a and b are passed
// After the function //call, a remains 5 and b remains 10.
cout << a << endl;
cout << b << endl;
return 0;
}
No comments:
Post a Comment
Fell free to write your query in comment. Your Comments will be fully encouraged.