0

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

enter image description here

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!

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

thanks a lot! it helped me through! btw this isn't homework. I just started solving the problems there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.