tags:

views:

80

answers:

2

I have the following program:

int insert(int *array, int arraySize, int newElement)
{
   array[arraySize + 1] = newElement;
   return (arraySize+1);     // Return new Array size......
}
int main()
{
   int array[] = {1,2,3,4,5};
   int arraySize = sizeof(array) / sizeof(int);

   insertInArray(array, arraySize,6);
   print(array);
}

I am trying to work out this program in C, but when I print the array after insertion, it doesn't print the desired output.

Please correct me if I am doing something wrong.

Updated Code:

int insert(int **array, int arraySize, int newElement)
{
   int i;    
   *array = realloc(*array,++arraySize * sizeof(int));    
   (*array)[arraySize] = newElement;    
   return (arraySize);
}

int main()
{
   int i;
   int arraySize = 5;
   int *array = (int *)malloc(arraySize * sizeof(int));
   for(i=1; i<=arraySize; i++)
      array[i] = i;

   printArray(array, arraySize);
   arraySize = insert(&array, arraySize,6);
   printArray(array, arraySize);
}
+2  A: 

First of all, this will cause a buffer overflow (by two):

You've allocated room for 5 integers (last index 4). Then:

array[arraySize + 1] = newElement;

writes to index 6, which is two past the end of the array. If you want to change the size of arrays, you need to use realloc., and your function needs a different signature so it can modify the pointer. Something like (omitting error-checking):

int insert(int **array, int arraySize, int newElement)
{
  *array = realloc(*array, ++arraySize * sizeof(int));
  (*array)[arraySize - 1] = newElement;
  return arraySize;     // Return new Array size......
}

int arraySize = 5;
int *array = malloc(arraySize * sizeof(int));
for(int i = 0; i < arraySize; i++)
{
  array[i] = i + 1;
}

arraySize = insert(&array, arraySize, 6);

Second, it's not clear how you expect print to work in general, without knowing the size. When you pass an array to a function, you have to either pass the length or terminate the array in an agreed way (e.g. with NULL or 0).

Matthew Flaschen
ohh.... this ws just to indicate that logic for array print works here......... How can i add a new element at last position of the array,, through a function..........
AGeek
how can i insert elements in array..... after this line.... int *array = malloc(arraySize * sizeof(int));
AGeek
array[] = {1,2,3,4}; doesn't works..........
AGeek
I think it is better to use reference to a pointer instead of using pointer to a pointer.... especially with newbies...
Betamoo
@AGeek, I added the initialization. @Beta, read the question. C does not have references.
Matthew Flaschen
Still not working....... run-time error.........*** glibc detected *** ./a.out: realloc(): invalid next size: 0x09c44008 ***I have no idea about this error.........
AGeek
@AGeek, there's no memory corruption in my code. The error is somewhere else, perhaps your `print` function. Update your question with the code you have now.
Matthew Flaschen
Or maybe you are passing a stack-allocated array (created without `malloc`) to `insert`?
Matthew Flaschen
hi Matthew... the code is updated now..... Plz check the errors that m committing,, and correct me...........
AGeek
@AGeek, the error is your loop. You're writing to `array[arraySize]`. The last valid element is `arraySize - 1`. Use a < loop like my code.
Matthew Flaschen
+3  A: 

You can't just extend the size of a C array like that; it's not dynamic in that respect; not at all. You have to explicitly reallocate memory. The situation is further complicated by the fact that memory can be allocated in two ways in C: from the stack (local variables in functions) and from the heap (calls to malloc). (Actually I guess it's three ways, if you include globals/statics.)

Thus there are two things that need to change:

  1. If you want to work only with simple arrays, and not structures that contain the array and some size indication, your "insert" function will have to allocate new memory and copy the old array values to it
  2. Arrays to be manipulated by that function should be allocated from the heap.
Pointy