Rozdíly

Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.

Odkaz na výstup diff

public:pb071:leaks_2011 [2018/02/24 19:10] (aktuální)
Řádek 1: Řádek 1:
 +<code c leaks.c>
 +#include <​stdlib.h>​
 +#include <​stdio.h>​
 +int main(int argc, char* argv[]) {
 +    int*  pArray = 0;
 +    const int arrayLen = 25;
  
 +    pArray = malloc(arrayLen * sizeof(int));​
 +    //pArray = malloc(arrayLen * sizeof(int));​ // this line will always cause memory leak
 +    printf("​Before argument: pArray = %p\n", pArray);
 +
 +    // we can make memory leak to happen only occasionally
 +    // e.g., when program is executed with cmd line argument '​1'​
 +    if ((argc == 2) && ​ atoi(argv[1]) == 1) {
 +      //​free(pArray);​
 +      pArray = malloc(arrayLen * sizeof(int));​
 +      printf("​Argument 1 given: pArray = %p\n", pArray);
 +    }
 +    if (pArray) free(pArray);​
 +
 +    int*  pArray2 = 0;
 +    pArray2 = pArray = malloc(arrayLen * sizeof(int));​ // we can store addresses returned by malloc into multiple variables
 +    printf("​pArray = %p, pArray2 = %p\n", pArray, pArray2);
 +    //enable this to see invalid free
 +    pArray2 += 20;
 +    printf("​pArray2 after +20 = %p\n", (void*) pArray2);
 +
 +    if (pArray2) free(pArray2);​
 +
 +    return 0;
 +}
 +             
 +</​code>​
QR Code
QR Code public:pb071:leaks_2011 (generated for current page)