Thread: Dynamic array help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try calling the add() function more than once in your main() function. You have a problem in that the array parameter is just a pointer, so this merely changes the local copy of the pointer:
    Code:
    array = (student*)realloc(array, ((*array_size) * sizeof(student)));
    You should declare array to be a pointer to a student*. (Maybe you should also rename it to students.)
    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

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Try calling the add() function more than once in your main() function. You have a problem in that the array parameter is just a pointer, so this merely changes the local copy of the pointer:
    Code:
    array = (student*)realloc(array, ((*array_size) * sizeof(student)));
    You should declare array to be a pointer to a student*. (Maybe you should also rename it to students.)
    sorry but I don't think I'm quite following, you mean that array should be a double pointer??

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by Perogy View Post
    sorry but I don't think I'm quite following, you mean that array should be a double pointer??
    Yes, a student**. But not in main(), only in the add() function, so when you call the function from main the first argument will be &array.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    ahhh ok I see what you mean, just so we can change the original it needs to be passed in as an address. I did that and now it seems to work perfectly for the first time I use add but if I use it again it segfaults. This is my new Student.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include "student.h"
    
    void add(student **array, student item, int *number_of_elements, int *array_size)
    {
    	if (*number_of_elements == *array_size) /*resize array*/
    	{
    		if (*array_size == 0)
    		{
    			*array_size = 1;
    		}
    		else
    		{
    			*array_size = *array_size * 2;
    		}
    		*array = (student*)realloc(*array, ((*array_size) * sizeof(student)));
    		
    		if (*array == 0)
    		{
    			printf("no space for memory");
    			exit(1);
    		}
    	}
    	*array[*number_of_elements] = item;
    	(*number_of_elements)++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM