Segmentation Fault (core dumped ) Array reading

Asked

Viewed 344 times

3

I have a problem related to reading a array that produces the error

Segmentation Fault (core dumped )

This is only the beginning of code development, but already presents an error after reading the first element of array.

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



void LerArray( int numElementos , float * elemArray ){

  int i;

  float num ;

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

      printf("Digite um numero para seu array:  ");

      scanf("%f", num );

      *( elemArray + i ) = num ;

      printf("/n/n");


  }


}


int main (){

  int numElementos;

  printf("Oi !!!  Quantos elementos vc quer armazenar :  ");

  scanf("%d", &numElementos );

  system("clear");

  float * vetorNum ;

  vetorNum = ( float * ) malloc( numElementos );

  LerArray( numElementos , vetorNum );

  return 0 ;

}
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

2

You have to multiply the number of elements by the size of each one

vetorNum = malloc(numElementos * sizeof *vetorNum);

and check for memory allocation error

if (vetorNum == NULL) /* erro de alocação */;

and when you no longer need the memory, release the resources

free(vetorNum);

2

The main thing causing this problem is that you are not allocating the amount of bytes you need for your vector.

The function malloc() stores an amount of bytes and not a number of elements, so you would need to multiply by the size of the data type of each element.

I also found a problem in scanf() that was not passing the argument by reference as it should be. And the character to jump line was also wrong.

Other than that, you’re not releasing memory with free(). No problem in this specific case because the program is very small, but get used to doing this always to not create addictions.

There is no guarantee that the allocation worked, it would be interesting to check this.

So I made these modifications and the code went like this:

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

void LerArray(int numElementos , float * elemArray) {
    for (int i = 0; i < numElementos; i++) {
        float num;
        printf("Digite um numero para seu array:  ");
        scanf("%f", &num);
        *(elemArray + i) = num;
        printf("\n\n");
    }
}

int main() {
  int numElementos;
  printf("Oi !!!  Quantos elementos vc quer armazenar: ");
  scanf("%d", &numElementos);
  system("clear");
  float * vetorNum;
  vetorNum = malloc(numElementos * sizeof(float));
  if (vetorNum == NULL) {
    printf("Erro na alocação, talvez funcione um um número de elementos menor ");
    return 1;
  }
  LerArray(numElementos, vetorNum);
  free(vetorNum);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thanks for the mustache. Your code above worked and in my case did not need to check allocation error ( if(vectorNum == NULL ) nor use the free() function as commented pmg. Thank you !!

  • @Alexandresantiagodasilva See the [tour]

  • @Alexandresantiagodasilva: The fact that you were lucky and no allocation error occurred does not mean that you should not test whether the allocation was successful. Releasing memory is good practice. In a simple exercise like this it may not make a difference but you better get used to good practices because when you have to work with more complex programs it can make a good difference in debugging.

Browser other questions tagged

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