CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2007
    Posts
    9

    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;
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    592

    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

  3. #3
    Join Date
    Jan 2007
    Posts
    9

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Dynamic memory allocation problem

    Quote Originally Posted by BrainInVat View Post
    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

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Dynamic memory allocation problem

    Quote Originally Posted by BrainInVat View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured