0

This is my code

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

int main() 
{
    int *m = malloc(3 * sizeof(int));

    m[0] = 1;
    m[1] = 2;
    m[5] = 3;

}

I want to be able to detect the memory leaks (as I am using malloc without using free. I was able to use clang test.c -o test -fsanitize=address -g but I am unable to use -fsanitize=memory or -fsanitize=leak. As I am getting the following error: clang: error: unsupported option '-fsanitize=memory' for target 'arm64-apple-darwin21.2.0' and clang: error: unsupported option '-fsanitize=leak' for target 'arm64-apple-darwin21.2.0'. I just wanted to know exactly what I have to do in order to use Memory Sanitizer and Leak Sanitizer on my macOS Monterey and be able to check that I just accessed memory that I should not have.

AMara
  • 23
  • 3
  • https://stackoverflow.com/questions/65112164/how-to-resolve-clang-error-unsupported-option-fsanitize-leak-on-apple-dev – n. m. could be an AI Jun 08 '22 at 19:05
  • `m[5] = 3;` is not a leak. A leak is when your program enters a state in which it will not free memory (prior to termination) that is no longer needed, as happens when you lose track of the address of allocated memory. Assigning `m = 0;` would be a leak since it would lose the value that was returned by `malloc`. – Eric Postpischil Jun 08 '22 at 19:09
  • Well what I meant in my question was that I would be accessing memory that I had not allocated. I think that would mean I was creating a segmentation fault. I was wondering how I could check that I made a segmentation fault with the Memory Sanitizer and Leak Sanitizer... – AMara Jun 08 '22 at 23:41
  • You use address sanitizer and UB sanitizer for that (-fsanitize=address,undefined). – n. m. could be an AI Jun 09 '22 at 20:09
  • Ok, I was able to do that, but now going back to what @EricPostpischil stated, if I set m = 0, than that would cause a leak. So I was wondering how I could use -fsanitizie=memory or -fsanitize=leak if the need arises to do so... I am beginner so I think I am not being clear enough in the question I want answered. I am able to make the address sanitizer work, but I also want to work with the memory sanitizer and leak sanitizer, what are the steps and processes I need to follow in order to accomplish that? – AMara Jun 09 '22 at 21:19
  • A *leak* is when you allocate something and then lose access to the last pointer pointing to it. Your program has a leak regardless of whether you set `m` to null or not, because you lose access to it when it goes out of scope. Have you tried `-fsanitize=address`? This should detect leaks as well. [demo](https://godbolt.org/z/WrnorPhKa) If this doesn't work for you, [ditch weird Apple's clang](https://stackoverflow.com/questions/53456304/mac-os-leaks-sanitizer). Normal clang supports -fsanitize=memory too. – n. m. could be an AI Jun 10 '22 at 08:35
  • Another way to detect such errors is [valgrind](https://valgrind.org/) The advantage is that you don't need any special compiler support. The disadvantage is that you need to install and use another piece of software. – n. m. could be an AI Jun 10 '22 at 08:44

0 Answers0