Dynamic array help

This is a discussion on Dynamic array help within the C Programming forums, part of the General Programming Boards category; Hello all, I am creating a program that uses a dynamic array to store students. My code compiles but i ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Dynamic array help

    Hello all, I am creating a program that uses a dynamic array to store students. My code compiles but i keep getting a runtime error which prints out a bunch of stuff, but the main thing being:

    *** glibc detected *** ./a.out: realloc(): invalid pointer: 0xb7fc2e00 ***

    so apparently there is a problem with my realloc but I just can't seem to figure out what it is. As far as I can see it should work. Any help with this would be greatly appreciated.

    Here is my main which is pointless right now. Basically it's just set up to test whether the add function works.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "student.h"
    
    int main()
    {
    	
    	student *array;
    	student std1;
    	int number_of_elements = 0;
    	int array_size = 0;
    	
    	std1.lastname = "smith";
    	std1.firstname = "bob";
    	std1.score = 50;
    
    	add(array, std1, &number_of_elements, &array_size);
    	
    	free(array);
    	return 0;
    }
    And my header file

    Code:
    #include <stdlib.h>
    
    #ifndef STUDENT_C
    #define STUDENT_C
    typedef struct student
    {
    	char *lastname;
    	char *firstname;
    	int score;
    } student;
    
    void add(student*, student, int*, int*);
    
    #endif
    and finally the important one. The student file which contains the dynamic memory function. Again the error seems to be on the realloc line.

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

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Maybe the problem is the extra "student" in the first line of the definition:
    Code:
    typedef struct student
    {
    	char *lastname;
    	char *firstname;
    	int score;
    } student;

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    thanks for the reply. hmmm.. removed that and it doesn't seem to make a difference

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I just realized -- you're using realloc without ever having used malloc, so the pointer that you're giving to realloc doesn't point to any memory -- ergo invalid.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    try this , define student *array=NULL; array must be NULL to allocate memory with realloc
    Last edited by xmariux; 04-05-2009 at 09:07 PM.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Quote Originally Posted by xmariux View Post
    try this , define student *array=NULL; array must be NULL to allocate memory with realloc
    that worked! Thanks a million, I can't believe it was something so trivial.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    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.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    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??

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    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.

  10. #10
    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)++;
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's good practice to not assume a pointer you're getting actually points to something. Check to make sure your pointers aren't NULL. Also, 'item' isn't a pointer, so once your function ends, the copy of that structure that you've just passed to the function no longer exists.


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

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Can you post the updated code.

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by Perogy View Post
    .... but if I use it again it segfaults...
    It shouldn't -- unless you have an error in main. I suspect that in the second call to add() you forgot to make the first argument a pointer (&array, ...)



    Quote Originally Posted by quzah View Post
    Also, 'item' isn't a pointer, so once your function ends, the copy of that structure that you've just passed to the function no longer exists.
    Quzah.
    Not true. The array is an array of structures, not an array of pointers, so when the new 'item' is assigned to the array, its contents are copied into the memory allocated to the array. Even if 'item' itself is dynamically allocated, copied into the array and then 'item' is freed, the data in the array would not be lost.

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Quote Originally Posted by quzah View Post
    It's good practice to not assume a pointer you're getting actually points to something. Check to make sure your pointers aren't NULL. Also, 'item' isn't a pointer, so once your function ends, the copy of that structure that you've just passed to the function no longer exists.
    Quzah.
    I don't see why item would have to be a pointer though, it just gets stored in the array and that's that. Let me know if I'm wrong.

    I'll post the entire updated code shortly.

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by Perogy View Post
    I don't see why item would have to be a pointer though, it just gets stored in the array and that's that. Let me know if I'm wrong.
    It doesn't. Did you read post #13?

Page 1 of 2 12 LastLast
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-03-2005, 11:13 PM
  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, 09:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM

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