0

I am working on a project for school below illustrates a simpler general idea of what I'm trying to achieve.

Basically What i would like to do is the following: -Ask user for a number(check!) -Create a dynamic array that stores these numbers (check!) -It will keep storing numbers until terminating value -999 is entered(check!)

My problem is this: -The professor says I may not assume the array is a certain size; -However, he does give a starting point for the size of the array which in my case is 5, which will then expand the array(double the size) as more numbers are input.

-Looking at the code below at when the user enters 6 numbers for input, the dynamic array will allocate more memory to compensate space for more integers. This part works fine.

-However, when the user enters 10 number. I would like the for the dynamic array to expand and double in size once again. However using the same method as above, this time however it fails to double in size, and gives me an error in linux while compling that says" realloc(): invalid next size: 0x000000000221f0"

Basically What i am trying to implement is a dynamic array that has a starting size of 5, and keeps allocating more size for more ints as needed. for example: dyanimic array has space for 5 ints, the user enters 5 ints. array expands space for 10 ints, the user enters 10 ints, and the array doubles again in size for 20 ints and so on.....

If someone can recommend a method for doing this, or point out my mistake I would be very appreciative.

I have spent the last 2 hours searching on here and google with possible fixes, and understanding how malloc and realloc function,but have failed thus far with a resolution.

int count=0;
int size=5;
int *to=(int *)malloc(size*sizeof(int));



printf("Enter\n");

scanf("%d",&to[count]);

while(to[count]!=(-999))
{
    count++;
    scanf("%d",&to[count]);

if(count==5)
    {
    printf("Expanding size . . . . . .\n");
    to=(int*)realloc(to,size*sizeof(int));
    }

if(count==10) 
    {
    printf("Expanding size to 10. . . . .\n");
    to=(int*)realloc(to,(2*size)*sizeof(int));
    }

}
3
  • You need not cast the return value of malloc or (for that matter) realloc. Commented Jan 25, 2015 at 22:24
  • scanf("%d",&to[count]); if(count==5) : 0) It has already been used beyond the area. 1) size has not been updated. also size of to are not increase. Commented Jan 25, 2015 at 22:27
  • I have taken into consideration what you said and updated my code. It makes sense what you are saying. I am no longer receiving an error. !! :) Commented Jan 25, 2015 at 22:35

1 Answer 1

0

Why not just

if ((count%5) == 0)
{
    printf("Expanding size . . . . . .\n");
    to=realloc(to,count*2*sizeof(int));
}

Checking for memory allocation errors in realloc is left as an exercise for the reader.

Sign up to request clarification or add additional context in comments.

3 Comments

expand 5 -> 10 -> 20 (not 15)
Thank you very much for this recommendation to help clean up my code. May i ask why I dont have to use a type cast for malloc or recalloc in this case? and when is it a good idea to type cast. thank you
I have found the answer to the question here. stackoverflow.com/questions/605845/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.