Why "Segmentation fault " when two arrays are interspersed?

Asked

Viewed 61 times

1

I’m building a simple c/c++ program that intersperses two arrays but I am encountering an inconvenient error. I believe when calling the function IntercalarArranjos, the program shows an error of type "Segmentation fault".

I do not know if I was wrong in the logic of this function and I know that this problem is related to improper access to memory or simply overflow of the same. Forgive me, but I am still beginner in this subject.

Here is the code:

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

 /****
 *
 *   Título do módulo :
 *
 *   Descrição geral  :
 *
 *   Entrada :
 *
 *   Saída   :
 *
 ****/
 void IntercalarArranjos ( float * primeiroArranjo , float *  segundoArranjo , float * arranjoIntercalado ,int quantPrimeiroArranjo ,  int quantSegundoArranjo , int quantArranjoIntercalado ){

     int i , j , k ;

     k = 0;

     j = 0;

     i = 0;

     while ( i < quantArranjoIntercalado ){

         if( j > quantPrimeiroArranjo ){

             *( arranjoIntercalado + i ) = *( segundoArranjo + j );

             k = k + 1;


         }

         if( k > quantSegundoArranjo ){

             *( arranjoIntercalado + i ) = *( primeiroArranjo + k );

             j = j + 1 ;

         }

         if(  j <=  quantPrimeiroArranjo && k <= quantSegundoArranjo ){

             if( i % 2 == 0 ){

                 *( arranjoIntercalado + i ) = *( primeiroArranjo + j );

                 j = j + 1 ;

             }
             else {

                 *( arranjoIntercalado + i ) = *( segundoArranjo + k );

                 k = k + 1 ;

             }
         }

     }
 }

 int cont = 0 ; /** variável global **/

 /****
 *
 *   Título do módulo : ImprimaVetor
 *
 *   Descrição geral  : Imprime o arranjo desejado
 *
 *   Entrada : (int) quantidade de elementos , (float) vetor dos elementos
 *
 *   Saída   : nenhuma
 *
 ****/

 void ImprimaVetor( int quantVetor , float * realVector ){

     int i ;

     cont += 1 ;

     if( cont == 1 ){

          printf("\nPrimeiro arranjo : ");

     }

     else{

         if( cont == 2 ){

             printf("Segundo arranjo : ");

         }

         else printf("Arranjos intercalados : ");

     }


     for( i = 0 ; i < quantVetor ; i ++ ){
         printf(" %f ", *( realVector + i ) );

     }

     printf("\n\n");

 }


 /****
 *
 *   Título do módulo : LeiaVetor
 *
 *   Descrição geral  : Lê o vetor de elementos
 *
 *   Entrada : (int) quntidade de elementos do vetor , (float) vetor dos    elementos
 *
 *   Saída   : nenhuma
 *
 ****/

 void LeiaVetor( int quantVetor , float * realVetor ){

     int i ;

     for( i = 0 ; i < quantVetor ; i ++ ){

         printf("\n\nDigite o %d elemento do arranjo :", i + 1 );
         scanf("%f", realVetor + i );

     }

 }

 int main()
 {

     float * primeiroArranjo , * segundoArranjo , * arranjoIntercalado ;
     int quantPrimeiroArranjo , quantSegundoArranjo , quantArranjoIntercalado ;

     printf( "---------------------------------------------------------");
     printf( "\nInsira a quntidade de elementos do primeiro arranjo :" );
     scanf("%d", &quantPrimeiroArranjo );

     primeiroArranjo = (float*) malloc( quantPrimeiroArranjo*sizeof(float) );

     LeiaVetor( quantPrimeiroArranjo , primeiroArranjo );

     printf("\n\n");

     printf( "---------------------------------------------------------");
     printf( "\nInsira a quntidade de elementos do segundo arranjo :" );
     scanf("%d", &quantSegundoArranjo );

     segundoArranjo = (float*) malloc( quantSegundoArranjo*sizeof(float) );

     LeiaVetor( quantSegundoArranjo , segundoArranjo );

     printf("\n\n");


     printf( "---------------------------------------------------------");

     quantArranjoIntercalado = quantSegundoArranjo + quantPrimeiroArranjo ;

     arranjoIntercalado = (float*) malloc(  quantArranjoIntercalado*sizeof(float) );

     IntercalarArranjos ( primeiroArranjo ,segundoArranjo ,arranjoIntercalado ,quantPrimeiroArranjo , quantSegundoArranjo ,quantArranjoIntercalado );

     ImprimaVetor( quantPrimeiroArranjo , primeiroArranjo  );

     ImprimaVetor( quantSegundoArranjo , segundoArranjo  );

     ImprimaVetor( quantArranjoIntercalado , arranjoIntercalado );

     printf("\n\n");

     return 0 ;

 }
  • 1

    C/C++ does not exist! If you want to use C++, use C++ (new, delete, etc ... do not use malloc(), free(), etc.)

  • Sorry. I didn’t find your comment constructive. Yes, I can use malloc() and free() in a c++ program. What’s stopping me? But that’s not the problem.

  • Hint: a more usual way of writing *(apontador + deslocamento) is apontador[deslocamento].

2 answers

3

The instructions

     k += k + 1;
     j += j + 1;

should be

     k = k + 1;
     j = j + 1;

or

     k += 1;
     j += 1;

or

     k++;
     j++;

2

Dear colleague, looking at your code I realized that by changing the relational operator of < as stated in its code, to >, within the function Intercalararranjos() he does not display Segmentation fault, more precisely in that part quoted below:

while ( i > quantArranjoIntercalado )

what I could realize is that you "instructed" that while i am less than quantArranjoIntercalado do this, but there was no verification, since the only instruction I changed was that besides declaring the functions before defining them.

Note:Another thing I noticed in your code is you are allocating, but not worried about releasing what was allocated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.