-1

my function get two exist array(+sizes) and pointer that in function build new array and realloc the size at the end. finally the function return the new size ,but the printf of new array is crash. the algorithm working casue the new size really true.

int UniteArray(int *a,int sizeA,int *b,int sizeB,int *tempArray)
{
    int i=0,j=0,counter=0;
    tempArray=(int*)malloc(sizeA*sizeof(int));
    while (i<sizeA)
        {
            if (a[i]==b[j])
                {
                    tempArray[counter++]=a[i];
                    i++;j++;
                }
            else if (a[i]<b[j])
                i++;
            else if (b[j]<a[i])
                j++;

        }
    tempArray=(int*)realloc(tempArray,counter);
    return counter;
}

here the function call;

void Ex3()
{
    int *a,*b,*unite;
    int sizeA,sizeB,newSize,counter,i;

    a=BuildArray(&sizeA);//Build the A array.
    b=BuildArray(&sizeB);           //Build the B array.                                       
    merge_sort(a,0,sizeA-1);//merge the A array.
    merge_sort(b,0,sizeB-1);            //merge the B array.

    counter=UniteArray(a,sizeA,b,sizeB,&unite);  //<<<<<<< HERE
    printf ("The new Length: %d.\n",counter);
    for (i=0;i<counter;i++)
        printf ("%d ",unite[i]);
Naor Malca
  • 143
  • 1
  • 10
  • 1
    So you ignore compiler warnings (or use an ancient compiler), but wounder why your code does not work. Next time enable all recommended warnings, make the code compile without any warning message and use the debugger. And learn what local variables are. That should have been the very first in your C book or lesson. If the latter: tell your teacher to clarify about local variables and pass-by-value. – too honest for this site Jan 20 '17 at 15:09
  • *Ahem* To put that a little more tactfullly...OP, you need to include the full text of the error in your question. We really can't help you with "it crashed" or "there was an error." If it's a "Segmentation fault", then you may need to use another tool to find reason. See [this question and answer](http://stackoverflow.com/q/33047452/472647) for more info about segfaults. – CodeMouse92 Feb 18 '17 at 17:43

1 Answers1

3

You pass a int ** for the fifth parameter of UniteArray, but the function is expecting an int *. While they are both pointers, they are not the same. Your compiler should have warned you about this.

You need to change your function to accept a int ** and change the references to tempArray accordingly. Also, you're not reallocating enough memory. You need to multiply the counter by sizeof(int).

int UniteArray(int *a,int sizeA,int *b,int sizeB,int **tempArray)
{
    int i=0,j=0,counter=0;
    *tempArray=malloc(sizeA*sizeof(int));
    while (i<sizeA)
        {
            if (a[i]==b[j])
                {
                    (*tempArray)[counter++]=a[i];
                    i++;j++;
                }
            else if (a[i]<b[j])
                i++;
            else if (b[j]<a[i])
                j++;

        }
    *tempArray=realloc(*tempArray,counter*sizeof(int));
    return counter;
}
dbush
  • 205,898
  • 23
  • 218
  • 273