I practice my c skills any try to to do task. I stack with that exercise: I have 2 order arrays in one array the first element in the first array is bigger than the last element of the first array. I need to order the array only by using realloc() and memcpy, (no use malloc/ new arrays ). and return the size of the first array - K. (0 <= k < n ). the function get address of pointer to dynamic array with n numbers.
for exe: { 32,64,66,69,72,78,81,87,94,95,1,2,4,8,17 }
I have a little problem with the realloc() with the pointer. I try to enlarge the array in the size of the first array, copy the first part to the end of the array and than copy the second part to the start, and than copy the last part to the next part , and than realloc() again to much size.
my code until now:
int arrangeArray(int** arr, int n)
{
int i = 0;
int first_size;
int second_size;
int new_size;
while (*(*arr i) < *(*arr i 1)) { // find the size of the first array
num = *(*arr i 2);
i ;
}
first_size = i 1;;
second_size = n - i;
new_size = n first_size;
*arr = (int*)realloc(*arr, new_size * (sizeof(int))); // enlarge the size of the array;
memcpy((*arr) n, (*arr), sizeof(int) * first_size); // copy the first part to the end of the array;
memcpy((*arr), (*arr) first_size, sizeof(int) * second_size); // copy the second array to the first of the new;
memcpy((*arr) second_size - 1, *arr n, sizeof(int) * first_size); // copy the last array to the last;
*arr = (int*)realloc(*arr, (n * (sizeof(int)))); // resize to the orginal size;
return first_size;
}
CodePudding user response:
You use malloc, free, realloc etc. with a "pointer to T", where T is any type. In your case, T = int*, which can be confusing, but there is nothing special about it.
For an array with n elements, you'd write
T* array = (T*) malloc(n * sizeof (T))
or
T* array = (T*) calloc (n, sizeof (T)).
To reallocate an array, you have two choices: You either pray that realloc never fails, or you write code to handle failure. One way to handle it:
T* array = ...; // This is where you created and filled the array
T* oldArray = array;
array = (T*) realloc (array, n * sizeof (T));
if (array == NULL) {
// realloc failed, oldArray contains the original data.
// Do what you can to handle the failure
array = oldArray;
return;
}
// Now you are NOT allowed to use oldArray anymore. It's only valid if
// realloc failed.
Two things to keep in mind: If you are actually shrinking the array, you must rearrange elements before realloc with the ones that you want to throw away at the end. HOWEVER realloc can fail if you make the array smaller! In that case you keep using the original array. And it can (and will quite often) return a different pointer than the original.
If you make the array larger, and realloc doesn't fail, then the new array will start with the original data, followed by garbage.
And note that you have to keep track of the array size yourself at all times. And as mentioned in a comment, sizeof returns the size of a pointer, so most likely 4 or 8, not the size of the allocated memory.
And please don't use addition dereferencing for pointer arithmetic. It is much more readable to write a[i]
instead of *(a i)
.