Thread: using new inside functions

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    8

    using new inside functions

    Hi,

    I am calling a function that dynamically allocates memory for some arrays using new, and then fills the arrays with values. It is my understanding that once the function exits, the memory and values should persist and that I should be able to access the values in other parts of my program. However, after the function exits, when I try to access the arrays, I get the message 'Access violation at address...'. Can anyone give me some advice on why this is happening and how I can fix it? Thanks a lot.


    I am using Borland C++ Builder 5.0 on Windows XP pro.

    Here's some sample code:

    #include "header.h"

    {
    ifstream infile("filename");
    float *data, buff;
    int size=0;

    size = get_data_size(&infile);
    fill_data_array(&infile, data, size);
    buff = data[0]; //this is where access violation occurs
    }

    //function is defined in header.h

    void fill_data_array(ifstream *infile, float *data, int size)
    {
    data = new float[size];
    for(int i=0; i<size; i++)
    {
    ... //do stuff with infile to fill data
    }
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Everything you said is correct. Remember, though, that the pointer is a data type like any other. If you have a pointer to something, and then set the value of the thing it points to within the function, then you can access that value outside through the same pointer. If you set the pointer itself, however, the data will be lost. Setting the value of the parameter does not set the value of the object passed as the parameter.

    Think of this example:
    Code:
    void f(int x) {
       x = 5;
    }
    So, you've set the value inside the function, but you didn't set the value of whatever variable was passed to the function. Instead, of course, you'd use this:
    Code:
    void f(int& x) {
       x = 5;
    }
    By analogy, to set your pointer, what you need to do is:
    Code:
    void f(int*& x) {
       x = new int;
    }

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    Your advice was very helpful. Thanks!

    I just wasn't thinking about the fact that I was assigning a new pointer value to data inside the function that would be lost when it exited.

    Here's an example of the working code for anyone interested:

    #include "header.h"

    {
    ifstream infile("filename");
    float *data, buff;
    int size=0;

    size = get_data_size(&infile);
    fill_data_array(&infile, &data, size);
    buff = data[0];
    }

    //function is defined in header.h

    void fill_data_array(ifstream *infile, float *data, int size)
    {
    *data = new float[size];
    for(int i=0; i<size; i++)
    {
    *infile >> (*data)[i];
    }
    }

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    8
    Oops, sorry.

    The example code should have been:

    #include "header.h"

    {
    ifstream infile("filename");
    float *data, buff;
    int size=0;

    size = get_data_size(&infile);
    fill_data_array(&infile, data, size);
    buff = data[0]; //this is where access violation occurs
    }

    //function is defined in header.h

    void fill_data_array(ifstream *infile, float **data, int size)
    {
    data = new float[size];
    for(int i=0; i<size; i++)
    {
    ... //do stuff with infile to fill data
    }
    }


    There is an extra '*' in front of data in the function definition.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suppose if I don't tell you this, no one will...

    Use [code] tags on your posts! If you had actually read the Announcements, you'd be doing it by now. So, while we're on the subject, go read them.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 02:42 PM
  2. global functions and OO
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 11:00 AM
  3. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  4. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  5. Variables do not equal functions!!!
    By me@burk. in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 06:24 AM