0

I'm trying to write a dynamic array in C and I almost did it.Now I' stuck at a point where it gives error while inserting in that error using realloc().Here is my code:

   #include<stdio.h>
   #include<stdlib.h>

   typedef struct {

     int *array;//pointer that points to the start of the contiguous  allocated blocks(an array of int)

     size_t used;//array used

     size_t size;//array total size
   }D_Array;


   void alloc_array(D_Array *a, int initial_size)
  {
    //allocate contiguous memory blocks and point a pointer to its beginning address...
    a->array = (int*)malloc(initial_size*sizeof(int));
    a->used = 0;
    a->size = initial_size;
  }


  void insert_array(D_Array *a, int element)
  {
   if(a->used == a->size)
   {
    //allocate one more space and then insert in array
    a->array = (int*)relloc(a->array,(a->size)*sizeof(int)); 
   }
    a->array[a->used++] = element;
  }


  void free_array(D_Array *a)
  {
   free(a->array);
   a->array = NULL;
   a->used = a->size = 0;
  }

  int main()
  {

   D_Array a; 
   int i=0, initial_size=0, insert_element=0;

   printf("Enter the initial size of array :");
   scanf("%d",&initial_size);
   alloc_array(&a, initial_size);

   printf("\nEnter the elements to be inserted initially");
   for(i=0 ; i<initial_size ; ++i)
   {
    scanf("%d",&insert_element);
    insert_array(&a, insert_element);
   }
    a.array[0] = 3;
   for(i=0 ; i<initial_size ; ++i)
   {
    printf("%d",*((a.array)+i));
   }
  }

Problem is in method "insert_array" but I don't know why.Everything seems fine to me.

[update from comment:]

The compiler gives:

quicksort.c:32:14: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] a->array = (int*)relloc(a->array,(a->size)*sizeof(int)); ^ /tmp/ccXTwlHh.o: In function insert_array': quicksort.c:(.text+0x8d): undefined reference to relloc' collect2: error: ld returned 1 exit status
4
  • quicksort.c:32:14: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] a->array = (int*)relloc(a->array,(a->size)*sizeof(int)); ^ /tmp/ccXTwlHh.o: In function insert_array': quicksort.c:(.text+0x8d): undefined reference to relloc' collect2: error: ld returned 1 exit status Commented May 22, 2016 at 6:29
  • doesn't a->array[a->used++] = element; cause out of bound access since a->used == a->size ? Commented May 22, 2016 at 6:29
  • @bkVnet No it shouldn't....suppose he size is 3 and "used" is 0.Now it will insert at '0' index and increment 'used' then it will insert at '1' and increment 'used'...again it will insert at '2' and increment 'used'....Now 'used is '3' and 'size' is 3.So it will enter the 'if' and reallocate one more block and then insert again. Commented May 22, 2016 at 6:49
  • if size is 3 then used is also 3 because a->used == a->size according to your code and in the if block it allocate a memory block of 3 since size is 3 and now so is used. Next you are accessing a->array[3] on an array which has 3 elements which is out of bound. Commented May 22, 2016 at 6:55

2 Answers 2

4

There are two problems with your realloc call: The first is what will happen if realloc fails and returns NULL. That's the reason you should never reassign to the variable you pass to the realloc call.

The second problem, and what's probably causing your problem, is that that you don't actually resize the array, instead you reallocate it using the exact same size as before, meaning that when you next do

a->array[a->used++] = element;

you are indexing out of bounds and will have undefined behavior.

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

12 Comments

So how should I use realloc() in this case? Suppose I have 5 blocks of memory that I have created using malloc().The "array" pointer points to the address of first block.Now when I use realloc() like I have used here,isn't it supposed to create a new contagious block of memory?
@Reckoner it does but you are not increasing the size of the memory block but just reallocating with the same size. You need to increase the value of a->size before calling it. That is of course once you correct the relloc typo :)
@Reckoner You have already allocated a->size elements using malloc, then the realloc call reallocates a->size number of elements. You must increase a->size before reallocating.
Sorry if I'm wrong but let's go back to the basics......here is a sample code that I've written to check what you are saying: { int ptr; ptr = (int)malloc(1*sizeof(int)); ptr = 10; ptr = (int)realloc(ptr,1*sizeof(int)); ++ptr; printf("%p",ptr); --ptr; printf("%p",ptr); return 0; } Now tell me the difference between the 2 codes here: In this sample code,I am expecting to add a contiguous memory block using realloc() and it does what I expect....any problems in this code?
@Reckoner The code in the comment is not a problem because you don't dereference the pointer ptr out of bounds. The problem with the code in the question is that you don't do e.g. a->size += 10 before the call to realloc. In your question you allocate X bytes. Then you call reallocae to allocate X bytes again. Both the malloc and realloc call will allocate a chunk of contiguous memory, equally large.
|
2

Adding to what's been said, you've written relloc instead of realloc. So there is perhaps a typo which is hindering the compilation.

1 Comment

Such a silly mistake from my side.....its done..Thanks a lot...deserve some down votes for sure......

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.