C ++ malloc segmentation fault

I have a problem with malloc (). This is strange. My code is as follows. I am using a random generator to generate items for an array. The array is opened by malloc (). If the array size is less than 8192, everything is fine. If the size is greater than 8192, it shows a segment error.

void random_generator(int num, int * array) {

  srand((unsigned)time(0)); 
  int random_integer; 
  for(int index=0; index< num; index++){ 
    random_integer = (rand()%10000)+1; 
    *(array+index) = random_integer; 
    cout << index << endl;
  } 
}

int main() {
  int array_size = 10000;
  int *input_array;
  input_array = (int*) malloc((array_size));
  random_generator(8192, input_array);    // if the number is larger than 8192, segment fault
  free(input_array);
}

      

+2


a source to share


7 replies


malloc()

takes size in bytes, not cardinality. The size is int

usually 4 bytes, so you actually allocate enough memory for 2500 integers. You are allocating array_size

bytes, whereas you should be allocating array_size * sizeof(int)

bytes.

Thus, the error will be fixed on

input_array = (int*) malloc(array_size * sizeof(int));



PS Never assume you know the size int

or any other datatype as it is platform dependent. Always use sizeof()

.

PPS This is really a C question, not a C ++ question. If you are really using C ++ you should use new

and delete []

instead of malloc()

and free()

or better use std::vector

instead of an array as Neil pointed out.

+15


a source


Do you want to:

input_array = (int*) malloc( array_size * sizeof(int) );

      

you can consider in a much simpler way:

input_array = new int[ array_size ];
// stuff
delete [] input_array;

      



or even:

std::vector <int> input_array( array_size );

      

and no need to worry about free call or delete or exceptions.

+11


a source


Seeing it as C ++, you would be much better off doing the following:

int main() 
{
  int array_size = 8192;
  int *input_array = new int[array_size];
  random_generator(array_size, input_array);
  delete[] input_array;
}

      

Edit: or better yet:

#include <vector>

int main() 
{
  int array_size = 8192;
  std::vector< int > array;
  array.resize( array_size );
  random_generator(array_size, &array.front());
}

      

And don't even worry about being released: D

+5


a source


input_array = (int*) malloc(sizeof(int) * (array_size));

This is because the malloc parameter is a byte and int is usually 4 bytes long.

+4


a source


Call the malloc call you are using. You must use:

malloc(sizeof(int) * array_size)

      

I think.

+3


a source


You are allocating array_size bytes, but you need array_size integers. Try to allocate array_size * sizeof (int) bytes in malloc.

+1


a source


All others have closed the dynamic allocation bug.
But do you really need dynamic allocation?

int main() {
  int const array_size = 10000;
  int input_array[array_size];
  random_generator(8192, input_array); 
}

      

+1


a source







All Articles