简体   繁体   中英

I can't solve this error : heap-buffer-overflow on Leetcode

My code is below:

int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
    int count;
    int* out = malloc(500);
    int i, j;

    for(i = 0; i < numsSize; ++i){
        count = 0;
        for(j = 0; j < numsSize; ++j){
            if(nums[i] > nums[j]){
                count++;
            }
        }
        *(out + i) = count;
    } 

    return out;
}

and the error messages are below

在此处输入图片说明

Actually, this heap-buffer-overflow kept coming up when I've solved the problems on Leetcode. even if it works well in Terminal on Mac.

will appreciate any comments or helps on it!

Since I think this is homework I only describe what comes directly into my mind by looking at the code:

At first you allocate a 500 byte buffer on the heap ( malloc(500) ), at a point where you don't know the exact size yet. Also you don't check if malloc returned a NULL pointer. I would change this line to a calloc(numsSize, sizeof(*out)) , since the result array is at worst the same size as the input array. Also add a check for the return value.

The second thing is that you pass in a variable to hold the result count, but assign it nowhere. Without this information the caller can't know how many elements in the resulting array are valid.

At first, you don't check malloc function.

Secondly, the argument returnSize is not used in this function.

Your function will be failed if numsSize > (500/ sizeof(int))

May be the code below can help you

int* smallerNumbersThanCurrent(int* nums, int numsSize){
   int count;
   int* out = malloc(numsSize * sizeof(int));
   if (out) {
       fprintf(stderr, "%s: malloc failed\n", __func__);
       exit(EXIT_FAILURE);
   }

   for(int i = 0; i < numsSize; ++i){
       count = 0;
       for(int j = 0; j < numsSize; ++j){
            if(nums[i] > nums[j]){
               count++;
            }
       }
      *(out + i) = count;
  } 

   return out;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM