Thread: Reallocate memory for array

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    3

    Reallocate memory for array

    I have a struct called Student and one of its elements is an array that will have a max of 40000 elements, so i built it like this:

    Code:
    typedef struct{
          int number;
          int grades [40000];
    }Student;
    I have a method that adds a new student but the number of elements in the number array can vary between 0 and 40000 so i never know the size of the array.
    What should i do? Should i change the element grades to a pointer? I know once i declare the array to have 40000 elements i can't change its size.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The real answer depends on how much memory you are using.

    I think you can get away with leaving it the way it is, but if there are 40000 Student instances too, not so much.

    The grades alone require like 160KB per student so you can use that to figure out roughly how much memory you are going to use on the stack. If it ends up being a lot you will need to make the change.

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    3
    I'm sorry I think I explained it wrong... Basically I'm working with hashtables and I have a function that adds a new Student to the hashtable,something like this:

    Code:
    void addsStudent(int number, int grades [ ]){
    
              Student st;
              st.number = number;
    
              /*Adds grades array to st*/
    
              insert(st);  /*Adds student to hashtable*/   
    }
    So I never know the size of the grades array, only that the max will be 40000, so, when I declared the struct I made the grades array have 40000 elements.
    My issue is how do I add the grades array not knowing its size, do I just add them to the 40000 array? Do I use pointers?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    The cheapest version of what you want is something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
        int id;
        int size;  // size of grades array
        int *grades;
    } Student;
    
    int main() {
        Student s;
        s.size = 0;
        s.grades = NULL;
    
        int n;
        while (scanf("%d", &n) == 1) { // ctrl-d (ctrl-z on windows) to quit
            s.grades = realloc(s.grades, ++s.size * sizeof(*s.grades));
            s.grades[s.size-1] = n;
        }
        
        for (int i = 0; i < s.size; i++)
            printf("%d\n", s.grades[i]);
    
        return 0;
    }
    Problems:
    * no error checking: in particular if realloc fails you lose the pointer to previously allocated memory.
    * inefficient: it's more efficient to increase the array size by doubling (or increasing by at least 50%) when you are out of space. But then you need to keep track of the "capacity" (total number of elements allocated) as well as the "size" (number of elements in use).
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    May 2018
    Posts
    3
    And will that work in a function? Because in the signature i have
    Code:
    void addsStudent(int id, int array) [])
    and i never know how many elements that array has, only that it has no more than 9000. Should i change the struct element grades and the addsStudent signature to a pointer?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MMG
    and i never know how many elements that array has, only that it has no more than 9000
    Same idea: instead of reading from standard input, you're processing the elements of an array. However, you do need to "know" how many elements the array has in use, typically by having another parameter for the number of elements in use. Otherwise, knowing that the array has a maximum size of 9000 is still no good because if it only has 100 elements in use, you should only loop over those 100 elements and leave the remaining 8900 untouched. (I note you mentioned a maximum of 40000 elements earlier, so it is actually a maximum of 9000?)

    With a parameter for the number of elements, you can then malloc exactly that number at one go instead of incrementally doing malloc calls to expand the dynamic array.

    Quote Originally Posted by MMG
    Should i change the struct element grades and the addsStudent signature to a pointer?
    Your "array parameter" for grades is already a pointer, just with array rather than pointer syntax, but it remains a pointer parameter either way. I would expect the function to look like this:
    Code:
    void addsStudent(HASHTABLE, int id, int *grades, int num_grades)
    where HASHTABLE is replaced by the declaration of a pointer parameter designating the hash table of students so that you can add the new student record to it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Dynamic Memory Array
    By darren78 in forum C++ Programming
    Replies: 8
    Last Post: 07-28-2010, 07:37 AM
  3. memory allocation for array
    By lehe in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2009, 09:05 PM
  4. Allocate memory for an Array
    By Coding in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2008, 06:18 PM
  5. Array memory map
    By _Elixia_ in forum C Programming
    Replies: 2
    Last Post: 08-17-2003, 04:39 PM

Tags for this Thread