hello I have a doubt how does the code below works??
#include <iostream>
using namespace std;
int main()
{
int* arr = new int;
arr[0] = 94;
arr[1] = 4;
cout << arr[0] << endl;
}
and why does this shows me a error what should I do
#include <iostream>
using namespace std;
struct test
{
int data;
};
int main()
{
test* arr = new test;
arr[0] -> data= 4;
arr[1] -> data= 42;
cout << arr[0]->data << endl;
}
In your code:
#include <iostream>
using namespace std;
int main()
{
int* arr = new int;
arr[0] = 94; // This will work
arr[1] = 4; // This will cause undefined behaviour
cout << arr[0] << endl;
}
Int the above code, arr is a pointer to a single int, so you can access that one int using either:
arr[0]
*arr
..but arr1 won't work as there is not enough memory allocated for the array.
To fix this, you must allocate more memory for arr:
int* arr = new int[2];
..and to change the size of arr:
int arr_size = 2;
int* arr = new int[arr_size]; // size of arr = 2
arr[0] = 12;
arr[1] = 13;
int* new_arr = new int[arr_size + 1];
for (int i = 0; i < arr_size; i++)
{
new_arr[i] = arr[i];
}
delete[] arr;
arr = new_arr;
// size of arr = 3
But all of this gets computationally expensive and time-consuming when arr has a huge number of elements. So I recommend using C++'s std::vector:
Your 2'nd program using std::vector:
#include <iostream>
#include <vector>
struct test
{
int data;
};
int main()
{
std::vector<test> vec{ test(4), test(42) };
std::cout << vec[0].data << std::endl;
}
For more info on std::vector, click here.
Also, consider not using the following line in your code:
using namespace std;
..as it's considered as bad practice. For more info on this, look up to why is "using namespace std" considered as bad practice.
Related
I have a smart pointer defined like this
auto ptr = make_shared<int[]>(5);
and another array
int arr[] = {1,2,3,4,5};
I want to copy data in arr to ptr, I tried using this
memcpy(ptr.get(), arr, 5 * sizeof(int))
But when I try printing like this
for (int i = 0; i < 5; i++) {
std::cout<<ptr[i]<<std::endl;
}
I get
malloc(): corrupted top size
Process finished with exit code 134
(interrupted by signal 6: SIGABRT)
Is there something wrong with the ptr initialization ?
Here is an example of what you need (hope so):
#include <iostream>
#include <memory>
#include <cstring>
int main() {
// Create a unique_ptr to an int array
std::unique_ptr<int[]> arr1(new int[5]{ 1, 2, 3, 4, 5 });
// Create a unique_ptr to an int array
std::unique_ptr<int[]> arr2(new int[5]);
// Use memcpy to copy the data from arr1 to arr2
std::memcpy(arr2.get(), arr1.get(), 5 * sizeof(int));
// Print the contents of arr2
for (int i = 0; i < 5; i++) {
std::cout << arr2[i] << " ";
}
std::cout << std::endl;
return 0;
}
I am newly to programming with c++ and I am trying to allocate memory for array of struct. The problem is I don't know the size of elements inside it at compile time. I want the "empty" part to be allocated with null pointers. I run this code below, but there is no the output I expected to be. Could somebody help me?
#include <iostream>
using namespace std;
typedef struct ones{
int a;
int b;
}ones;
ones** twoes = nullptr;
int main()
{
cout<<"Hello World";
for(int i = 0; i < 5; i++)
{
twoes[i]= nullptr;
}
cout<< "i dont get HERE" << endl;
for(int i = 0; i < 5; i++)
{
twoes[i]= new ones;
twoes[i]->a = 2;
cout<< twoes[i]->a <<endl;
}
return 0;
}
I wrote a function that the doubles the size of the array
int *expand(int ar[], int curr_size) {
int *new_array = new int[curr_size * 2];
for (int i = 0; i < curr_size; i++)
new_array[i] = ar[i];
return new_array;
}
When I try to use it
int main(){
int ar[] = {1,2,3,4};
ar = expand(ar, 4);
}
It throws following error:
Array type 'int [4]' is not assignable
What is the problem?
In main(), ar is a fixed sized array. You can't resize it, or assign a pointer to it. All you can do with it is read values from it, and write values into it.
For what you are attempting, you need to allocate arwith new[], and then delete[] it before assigning a new pointer to it
int main(){
int *ar = new[4]{1,2,3,4};
int *new_ar = expand(ar, 4);
delete [] ar;
ar = new_ar;
delete [] ar;
}
This would be better handled using a std::vector instead:
#include <vector>
int main(){
std::vector<int> ar{1,2,3,4};
ar.resize(ar.size()*2);
}
Arrays in c++ do not grow. Their size is set at compile time. Use std::vector instead.
Remy Lebeau absolutely right.
Your code can be changed like this:
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
int *expand(int *ar,int curr_size){
int *new_array = new int[curr_size*2];
for (int i = 0; i < curr_size; i++){
new_array[i] = ar[i];
}
delete [] ar;
return new_array;
}
int main()
{
int *ar = new int[4]{1,2,3,4};
for (auto i=0;i<4;i++) {
cout<<ar[i];
}
cout<<endl;
ar = expand(ar,4);
for (auto i=0;i<8;i++) {
cout<<ar[i];
}
return 0;
}
I am studying for an exam I have next week and am having troubles understanding Dynamic memory allocation. I have a question given which I dont know how to answer;
line 4: int *arr = new int[3];
Write a function that includes the line 4 above and returns the size of the memory location occupied by variable arr. Use the signature:
int size_of_variable_star_arr() ;
I assume I should be using both a main.cpp and a function.cpp - the main file should contain the array variables, while the function file should contain the array that returns it.
Not entire sure what to do here though in order to return the size of the memory location.
//Main.cpp
#include <iostream>
using namespace std;
int main(){
//int *arr = new int[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
cout << "Array: ";
}
//function.cpp
#include <iostream>
using namespace std;
int size_of_variable_star_arr(){
int *arr = new int[3];
for(int i = 0; i < 4; i++){
cout << arr[i] << " ";
}
return 0;
}
This is a really strange question, but here's the answer anyway:
int size_of_variable_star_arr() {
int *arr = new int[3];
delete [] arr;
return sizeof(arr);
}
Notes:
I added delete [] to undo the (unnecessary) new
It returns the size of variable arr, as stated in the question. Not the size of the memory block which arr points to.
so this is what i'm suppose to do but its got me kinda confused, this is what i got so far any help would be appreciated:)
Write a function that dynamically allocates an array of integers. The function should accept an integer argument indicating the number of elements to allocate and should return a pointer to the array. Then write a driver in the main function that generates a random number (something not too large), calls the function, and verifies access by saving a value to the first element and displaying the contents of that element.
edited code it runs but i feel like im not using my function at all.
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int *MyArray(int);
int main()
{
srand(time(0));
int random = rand() % 5 + 1;
const int size = 5;
int array[size];
MyArray(size);
array[0] = random;
cout << array[0] << endl;
}
int *MyArray(int numOfElements)
{
int *array;
array = new int[numOfElements];
return array;
}
edited code
int main()
{
srand(time(0));
int random = rand() % 5 + 1;
const int size = 5;
int* array = MyArray(size);
array[0] = random;
cout << array[0] << endl;
delete [] array;
}
I believe you try to do something like this:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int *MyArray(int);
int main()
{
srand(time(0));
int random = rand() % 5 + 1;
int *array = MyArray(random); //! store the pointer of dynamically allocated memory and use it.
array[0] = random;
cout << array[0] << endl;
delete [] array; //! To avoid memory leak
}
int *MyArray(int numOfElements)
{
int *array = new int[numOfElements];
return array;
}
Note: I'm just guessing this is what you probably looking for.