2

noob to C. I'm not sure why this code is causing the following error:

munmap_chunk(): invalid pointer

This is based on a Kata at https://www.codewars.com/kata/55466989aeecab5aac00003e/train/c. The Kata also throws the following error:

Test Crashed Caught unexpected signal: 6 Completed in 0.0000ms

// C returns a structure named Data.
// array is the returned array.
// sz is array size. 
// If from the beginning square length == square width
// return a Data with sz = 0.

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

typedef struct Data Data;
struct Data {
     int *array;
     int sz;
};

Data* sqInRect(int lng, int wdth) {
  // your code
}
David
  • 33
  • 1
  • 7
  • 2
    `int l = (int) malloc(sizeof(int));` is just one of the reasons one [should not cast the return of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jul 07 '21 at 22:17
  • 1
    `data->array = arr;` you're returning a pointer to a local variable. The pointer becomes invalid when the function returns. – Barmar Jul 07 '21 at 22:18
  • 3
    The error you get is likely because of e.g. `free(&l); free(&w);`. You make beginners mistakes. Whatever book, tutorial or other resource you're using to learn C, throw it away. Do some searching for good books, and take classes. And never use so-called "competition" or "online judge" sites to learn anything, that's not really what they're for. – Some programmer dude Jul 07 '21 at 22:19
  • 1
    `l = lng;` You're overwriting the variable that was assigned from `malloc()`, so you have a memory leak. – Barmar Jul 07 '21 at 22:20
  • I don't see `munmap*` anywhere in your code, so why is it in the title? I agree with S.P.D. about the "good book" vs "online judge". But, here's a hint: Change: `int l = (int) malloc(sizeof(int));` to `int *l = malloc(sizeof(int));` and `free(&l);` to `free(l);` I believe it's UB ("undefined behavior") to compare pointers that come from two separate `malloc` calls [per ISO/C]. Thus, `if (l > w)` is UB [or minimally nonsensical]. You _can_ compare pointers that are within the _same_ array/area, but you're _not_ doing that. Again, get a good book. Consider Harvard's `cs50` online _course_ – Craig Estey Jul 08 '21 at 00:53
  • Thank you for the suggestions. If you know of a good book to learn C please let me know. I have worked on learning some C a using https://cs50.harvard.edu/x/2020/ – David Jul 08 '21 at 00:56
  • 1
    You could start with the bible: https://en.wikipedia.org/wiki/The_C_Programming_Language (2nd edition) Here is a list from SO (the first entry is K&R's book): https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Craig Estey Jul 08 '21 at 01:02
  • 1
    Thanks for all the comments. I was able to fix the error and solve part of the Kata by removing the variables l, w, and arr and using the parameters passed with data->array[index]. Now I am getting the following error "free(): double free detected in tcache 2" I'll keep on working on it. Thanks again. – David Jul 08 '21 at 01:24

0 Answers0