I am learning pointers in C++ and am working on the new and delete functionality .
I have a local function which allocates memory on the heap but because i am returning the 2d array that i have created , I dont understand how to plug this memory leak, any help would be appreciated
main.cpp
#include<iostream>
#include "integers.h"
using namespace std;
int main()
{
int i[]={1,2,3,4};
int n=sizeof(i)/sizeof(int);
cout<<n<<endl;
printint(genarr(i,n),n);
}
integers.cpp
#include<iostream>
using namespace std;
int** genarr(int* val,int n)
{
int i,j;
int **a=new int*[n];
for(i=0;i<n;i++)
a[i]=new int[n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i==j)
a[i][j]=val[i];
return a; // The variable that will leak but because i am returning it , how do stop it
}
void printint(int** a,int n){
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return ;
}
integers.h
int** genarr(int*val, int n);
void printint(int **a,int n);
compiled by
g++ main.cpp integers.cpp -o integers
I have heard about smart pointers and am planning to learn about them after this , but for now i want to know if there is way to fix this or should i just go for smart pointers ?
To fix the problem, you need to delete what you new'd.
Change the code in main to:
int **arr = genarr(i,n);
printint(arr,n);
// we're done using arr; now we need to free it
for(int j=0;j<n;j++)
delete[] arr[j];
delete[] arr;
You can also extend integers.cpp and add a delarr function that complements genarr:
void delarr(int **a, int n) {
for (int i = 0; i < n; i++) {
delete[] a[i];
}
delete[] a;
}
Then main becomes simply:
int **arr = genarr(i,n);
printint(arr,n);
delarr(arr,n);
The easiest way to avoid memory leaks in C++ is to avoid explicitly calling delete anywhere. Smart pointers can solve this for you.
In your specific case, you could try something like this (untested):
using Vector = unique_ptr<int[]>;
using Matrix = unique_ptr<Vector[]>;
Matrix genarr(const int* val, int n)
{
Matrix a(new Vector[n]);
for(int i=0;i<n;i++)
a[i].reset(new int[n]);
// ...
Related
I'm trying to create a function printarr that prints a dynamically sized array. However, whenever I try to run the code I get the error above. I tried copying code I found online as well, but get the same error, so I'm wondering if there's something wrong with my installation, or another part of the code is somehow affecting it. The entire thing seems super simple, which makes me even more stumped. Here's the code:
#include <iostream>
using namespace std;
//Generates an array with k inversions of size n
int* generate_k_inversion(int k, int n){
int *arr=new int(n);
//Check if number of inversions is possible
if(k>(n*(n-1)/2)){
cout<<"Impossible number of inversions!";
throw(-1);
}
//Loop to generate points
for(int i=0;i < n;i++){
arr[i]=i;
}
//Loop to invert
return arr;
}
//Copies dynamic arrays of size n
int* copy(int* arr1,int n){
int* arr2=new int(n);
for(int i=0;i<n;i++){
arr2[i]=arr1[i];
}
return(arr2);
}
//Generates output of best and worst cases of bubble and insertion sort in integers of size n
void test1(int n){
//generate best case
int *arrb1=generate_k_inversion(0,n);
int *arrb2=copy(arrb2,n);
delete [] arrb1;
delete [] arrb2;
//generate worst case
int *arrw1=generate_k_inversion((n*(n-1)/2),n);
int *arrw2=copy(arrw2,n);
delete [] arrw1;
delete [] arrw2;
}
//Prints a dynamic array arr of size n
void printarr(int* arr, int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
}
//Just initialize both tests
int main(){
int size=10;
int* arr1=generate_k_inversion(0,size);
printarr(arr1,size);
delete [] arr1;
}
Thanks for the help
The line
int *arr=new int(n);
will allocate memory for a single int and initialize that int to n.
Therefore, the loop
for(int i=0;i < n;i++){
arr[i]=i;
}
will access arr out of bounds, causing undefined behavior.
What you probably want is to write
int *arr=new int[n];
instead, which will allocate memory for n values of type int.
I am trying to pass a dynamic memory allocated array and its size to a function 'sum' but it is giving error of permissive what should I do?
#include<conio.h>
#include<iostream>
using namespace std;
int sum(int n[], int *m)
{
for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}
}
int main()
{
int *n,*m,a; //declaration is done here**strong text**
cout<<"enter the size of array = ";
m=new int;
cin>>*m;
n=new int[*m];
for(int i=0;i<*m;i++)
{
cout<<"\n enter the "<<i+1<<" array = ";
cin>>n[i];
cout<<"\n";
}
/* for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}*/
int sum(n,&m);//here "m" is an pointer and I am trying to pass int in a function with an array
return 0;
}
Your code should, probably, look like the following (Linux Ubuntu + gcc):
#include <iostream>
using namespace std;
int sum(int n[], int m)
{
int s=0;
for(int z=0; z<m; z++)
{
cout<<"\n array["<<z<<"]= "<<n[z]<<"\n";
s+=n[z];
}
return s;
}
int main()
{
int *n,m;
cout<<"enter the size of array = ";
cin>>m;
n=new int[m];
for(int i=0; i<m; i++)
{
cout<<"\n enter array["<<i+1<<"] value = ";
cin>>n[i];
cout<<"\n";
}
int s = sum(n, m);
cout<<"s="<<s<<endl;
return 0;
}
There is no use allocating the size of the array m dynamically. It is an ordinary int variable and can be initialized as
cin>>m;
You may also write the sum prototype in the form
int sum(int * n, int m)
It is another way of passing a 1-dimensional array as a function parameter.
Speaking frankly, these questions are the very basics of the language.
You should, probably, read something like
Dynamic memory allocation/dynamic arrays
about dynamic memory allocation and dynamic arrays and
Simple cases of std::cin usage
about the simplest cases of std::cin usage in C++.
I have written a simple code where i want to allocate a 2d array in C++.The rows(m) and columns(n) would be provided by user at run time.
So the function func should return a pointer to the allocated array.Below is the code however i am getting compilation error .
int** func(int **arr,int m,int n){
arr=new int *[m];
for(int i=0;i<m;i++){
arr[i]= new int arr[n];
}
return arr;
}
int main()
{
int **arr;
arr=func(arr,4,10)
}
Use this:
arr[i] = new int[n];
instead of
arr[i]= new int arr[n];
and also, put semicolon after the function call,
arr=func(arr,4,10);
Full code:
#include <iostream>
using namespace std;
int** func(int **arr,int m,int n)
{
arr = new int*[m];
for(int i=0;i<m;i++)
{
arr[i] = new int[n];
}
return arr;
}
int main()
{
int **arr;
arr=func(arr,4,10);
}
I have created the following program which is supposed to return an int array to the main function, which will then display it on the screen.
#include <iostream.h>
int* returnArray(){
int* arr;
arr[0]=1;
arr[1]=2;
arr[2]=3;
return arr;
}
int main(){
int* res = returnArray();
for(int i=0; i<3; i++){
cout<<res[i]<<" ";
}
return 0;
}
And i was expecting it to print
1 2 3
but instead, it prints 3 someNumberWhichLooksLikeAPointer 0
Why is that? what can i do to return an int array from my function? Thank you very much!
You forgot to allocate your array:
int* arr = new int[3];
You also need to return it, and free the memory inside main after you finish with the loop in order to avoid a memory leak:
delete[] res;
Although this approach works, it is not ideal. If you have an option of returning a container, say, std::vector<int> it would be a much better choice.
If you must stay with plain arrays, another solution for filling an array inside an API is to pass it in, along with its size:
void fillArray(int *arr, size_t s){
if (s > 0) arr[0]=1;
if (s > 1) arr[1]=2;
if (s > 2) arr[2]=3;
}
int main(){
int res[3];
fillArray(res, 3);
for(int i=0; i<3; i++){
cout<<res[i]<<" ";
}
return 0;
}
You have tagged the question with C++. You Yous should consider to use the C++ solution: use a vector of int
#include <iostream>
#include <vector>
std::vector<int> returnArray(){
std::vector<int> arr(3);
arr[0]=1;
arr[1]=2;
arr[2]=3;
return arr;
}
int main(){
std::vector<int> res = returnArray();
for(int i=0; i<3; i++){
std::cout<<res[i]<<" ";
}
return 0;
}
I have to declare an array of pointers to objects (of classes) in C++. I thought this was the only way, but apparently I was wrong, as it throws a syntax error when I try to compile it. Specifically, among the 7 errors I received, 2 of these errors are in the lines: where I create the array using "new", and in the line where I call the "setData()" function. Can you tell me where I went wrong? Thanks.
#include <iostream>
class Test
{
public:
int x;
Test() { x=0; }
void setData(int n) { x=n; }
};
void main()
{
int n;
Test **a;
cin >> n;
a=new *Test[n];
for(int i=0; i<n; i++)
{
*(a+i)=new Test();
*(a+i)->setData(i*3);
}
}
Use a=new Test*[n];
Other than that, you have no deleteĀ“s in your program, trivial getter/setters
for public variables are strange, and *(a+i) could be a[i]
Your syntax is close but slightly off. Use this instead:
Test **a;
...
a=new Test*[n];
for(int i=0; i<n; i++)
{
a[i]=new Test();
a[i]->setData(i*3);
}
...
// don't forget to free the memory when finished...
for(int i=0; i<n; i++)
{
delete a[i];
}
delete[] a;
Since you are using C++, you should use std::vector instead. I would also suggest passing the desired value to the class constructor:
#include <iostream>
#include <vector>
class Test
{
public:
int x;
Test(int n = 0) : x(n) { }
Test(const Test &t) : x(t.x) { }
void setData(int n) { x=n; }
};
int main()
{
int n;
std::vector<Test> a;
cin >> n;
a.reserve(n);
for(int i=0; i<n; i++)
{
a.push_back(Test(i*3));
}
...
// memory is freed automatically when finished...
return 0;
}