|
-
June 30th, 2010, 07:55 PM
#1
Dynamic memory allocation problem
I am trying to write a function to do dynamic memory allocation with a try catch block, so that I don't have to manually write a try catch block each time I want to allocate memory for an array. The code bellow is supposed to do that, but it outputs:
Allocated memory for small array
Segmentation fault
So apparently the function creates a copy of the array and allocates memory for the copy, but not for the original array. How do I fix this? Thanks in advance.
# include <iostream>
# include <fstream>
# include <string>
# include <cstring>
# include <sstream>
# include <time.h>
# include <iomanip>
# include <vector>
using namespace std;
template <class T>
bool SafeArrayAlloc(T *array, int Size)
{
try
{
array=new T[Size];
}
catch(bad_alloc&)
{
return false;
}
return true;
}
int main(int argc, char * argv[])
{
int big, small;
int *big_array, *small_array;
big=3000000000;
small=10;
if (!SafeArrayAlloc(&small_array, small))
{
cout <<"Unable to allocate memory for small array"<<endl;
}
else
{
cout <<"Allocated memory for small array"<<endl;
for (int i=0;i<small;i++) small_array[i]=0;
for (int i=0;i<small;i++) small_array[i]=i;
for (int i=0;i<small;i++) cout <<small_array[i]<<endl;
}
if (!SafeArrayAlloc(&big_array, big))
{
cout <<"Unable to allocate memory for big array"<<endl;
}
else
{
cout <<"Allocated memory for big array"<<endl;
for (int i=0;i<big;i++) big_array[i]=0;
for (int i=0;i<big;i++) big_array[i]=i;
for (int i=0;i<big;i++) cout <<big_array[i]<<endl;
}
return EXIT_SUCCESS;
}
-
June 30th, 2010, 08:08 PM
#2
Re: Dynamic memory allocation problem
bool SafeArrayAlloc(T*& array, int Size);
I am guessing that will fix your problem without looking that much. Also there is no need to create this function to implement a nothrow new.. just use
int* I = new (nothrow) int [array size] ();
http://www.cplusplus.com/reference/std/new/nothrow/
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
June 30th, 2010, 08:30 PM
#3
Re: Dynamic memory allocation problem
Thanks. It worked. I tried bool SafeArrayAlloc(T&* array, int Size) previously but it did not work. You are probably right that this function is not very useful, but I also wanted to write a function for multidimensional array allocation and I can't do that without getting this function to work. Thanks again.
-
July 1st, 2010, 04:14 AM
#4
Re: Dynamic memory allocation problem
 Originally Posted by BrainInVat
Thanks. It worked. I tried bool SafeArrayAlloc(T&* array, int Size) previously but it did not work. You are probably right that this function is not very useful,
The real question is why did you need to write this when std::vector is available?
You even included <vector> in your code, but for some unexplained reason, you need to write this instead. 
Regards,
Paul McKenzie
-
July 1st, 2010, 09:12 AM
#5
Re: Dynamic memory allocation problem
 Originally Posted by BrainInVat
I also wanted to write a function for multidimensional array allocation and I can't do that without getting this function to work. Thanks again.
You should consider whether boost::multi_array solves your problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|