0

I am using Windows 7 Professional. I have been given a task where i need to generate some System Level issues, so that a event should generate which we can see in Event viewer.

I am testing a product (Company Product* Can't disclose) consist of one dart which can check or create alert for a memory leak.

I wrote a simple code but i can't see any such event log in event viewer. Please suggest me any tool or any procedure through which at least an event should generate.

My code:

#include<malloc.h>
#include<stdio.h>
int main() {
   for(int i = 0; i < 100; i++) {
      int * ptr = (int *) calloc (1000, sizeof(int));   // allocating 40 bytes 
                    // let sizeof int =  4 bytes)
      ptr = NULL;
   }
   return 0;
}

  • Use `` to get the declaration of `calloc()` et al. The 'allocating 40 bytes' comment doesn't match the code; at least one of them is wrong, therefore. I don't know Windows 7 Professional, but I expect that it won't report a memory leak as such; it will report problems from programs crashing out of memory, which will require far more than 400 KiB of memory allocated to cause trouble, especially on 64-bit Windows and a machine with, say, 8 GiB of main memory. – Jonathan Leffler Apr 07 '14 at 05:14

1 Answers1

0

I'm going to guess that the optimizer removed the call to calloc since the value returned from it is not being used at all. Add the following line after the call to calloc and before ptr = NULL;.

printf("%d\n", ptr[0]);

In other words, check the following program:

#include<malloc.h>
#include<stdio.h>
int main() {
   for(int i = 0; i < 100; i++) {
      int * ptr = (int *) calloc (10, sizeof(int));     // allocating 40 bytes 
                    // let sizeof int =  4 bytes)
      printf("%d\n", ptr[0]);
      ptr = NULL;
   }
   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Would be easier to just use `volatile`, something like `int*volatile ptr` – Jens Gustedt Apr 03 '14 at 06:05
  • @JensGustedt, it seems a compiler is allowed to optimize away `volatile` objects too as long as they are not global variables. See [unused volatile variable](http://stackoverflow.com/questions/4933314/unused-volatile-variable). Not sure how the compiler used by the OP deals with them. – R Sahu Apr 03 '14 at 06:11
  • If it is initialized dynamically, it is used. The compiler is not allowed to optimize the store to a `volatile`. If the compiler really would do that, it would be non conforming, but for a MS compiler you can expect anything. In that case I'd make it global in addition. – Jens Gustedt Apr 03 '14 at 06:15
  • @Jens: If an identifier has block scope and internal linkage, and does not point to memory-mapped I/O or similar, I think the compiler can prove that it **cannot** have any unknown side effect, and can thus disregard the `volatile` qualifier. – Nisse Engström Apr 07 '14 at 06:09