dynamic memory allocation

This is a discussion on dynamic memory allocation within the C++ Programming forums, part of the General Programming Boards category; i have a problem with allocating memory to an array of class cage, and i need to do this cos ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    dynamic memory allocation

    i have a problem with allocating memory to an array of class cage, and i need to do this cos i dont know the contents of the file beforehand then reading it into an array of class cage. i don't think i can write cage[i].min_weight or cage[i].max_weight, but what should i do??
    Code:
    	ifstream inClientFile("filename", ios::in);
                    cage *p;
    	p=new cage[j];
        
    	if(a==0){
        for(i=0; i<j; i++)
    	inClientFile>>cage[i].min_weight>>cage[i].max_weight;
    	}
    }//class cage contains 2 private members min_weight and max_weight. J is the size of the class.

    can some kind soul help me with this?? Thanks a million!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    You need to write some public interface function in your class which accesses min_weight for you.

    Which you would call with
    inClientFile>>temp;
    cage[i].set_min(temp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    i think u misunderstood me..i could read data from the file into the class..
    for eg. the file would look like this
    20 60
    10 50
    0 80
    so 20 n 60 will go into the first member of the array of class
    10 n 50 into the second and so on..
    but remember that the data inside the file could change so i need to dynamic allocate memory after counting the data which i already did..so the problem arose when i tried to allocate memory to the array..i wolud like to know if anybody could tell me how to do it..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why use an array? Why not a linked list or something more ... useful? Anyway, what is it exactly you're trying to do? Create a dynamic array? Meaning, the array may have 10 members once, 3 another, or what not?

    Why not just use a vector that?

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

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In your example j is the number of elements of in the array called p which holds elements of type cage, not the size of type cage. In your example once j is set the capacity of p cannot change without first deallocating the existing memory and reallocating additional memory to p.

    If you want more details read further.

    To use an array to hold an unknown number of elements you can either declare the array with capacity bigger than you think you'll need or dynamically reallocate memory whenever the number of elements you want to allocate exceeds the capacity of the array. To do it yourself you keep track of the number of elements actually entered vs the capacity. If you reach capacity then you declare a temporary array of the same type and size as the current array, copy all elements of the current array into the temporary array, deallocate memory for the current array, reallocate memory for the current array such that the capacity is increased, copy all elements of the temporary array back into the current array, and deallocate memory for the temporary array. Needless to say, that's quite a few steps and the need to do this sort of thing comes up often. To avoid this hassle a list (homegrown or an instance of the STL list class) or an instance of the STL vector class (which you can use like an array in terms of the [] operator, etc.) is commonly used. You may be able to shortcut these steps using a command like memcpy or something, but the gist remains the same.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    hmmm..

    thanks everyone! think i understand now
    Last edited by inquisitive; 03-13-2004 at 01:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21