malloc() and memory leaks

This is a discussion on malloc() and memory leaks within the C Programming forums, part of the General Programming Boards category; Good day, I've been tasked to actually write a sample of a memory leak in c (linux or windows), I've ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    malloc() and memory leaks

    Good day,

    I've been tasked to actually write a sample of a memory leak in c (linux or windows), I've been searching around the net, but I can't get an example to analyse on. can somebody direct me to one?

    cheers,

    PS:I couldn't understand wiki's example.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Step 1: Allocate some memory with malloc().
    Step 2: Overwrite the pointer to this memory before using free().

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    malloc() program

    if you want to make a array with malloc take a look here:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main(){
           int i,j;
           int **X;
               X = (int**)malloc(5*sizeof(int*));
               for(i=0; i<5; i++){
                        X[i]=(int*)malloc(3*sizeof(int));
               }
       for(i=0; i<5; i++){
                for(j=0; j<3; j++){
                         X[i][j]=1;
                }
       }
    printf("This is the array: \n \n");   
       for(i=0; i<5; i++){
                printf("\n");
                for(j=0; j<3; j++){
                         printf(" [%d][%d]: %d\t", i, j, X[i][j]);
                }
       }
    printf("\n \n");
    //system("pause");
    }
    i hape i help you...
    Last edited by Salem; 05-01-2010 at 08:57 AM. Reason: Added [code][/code] tags - learn to use them yourself

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    A simple example:

    Here we have a function that allocates memory to an integer pointer and assigns a value to it. However we aren't returning the pointer or doing anything else with it. A memory leak has occurred. This memory is still allocated and floating somewhere in the heap, without a way for us to reference it anymore.

    Most modern operating systems will free this when the program ends, but there are some out there that won't, in the later case, if we continuously run the program over and over again it will come to a point where you won't be able to allocate any new memory.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void doSomething();
    
    int main ()
    {
       doSomething();
       return 0;
    }
    
    void doSomething() 
    {
       int* i = malloc(sizeof *i);
       *i = 15;
    }
    Last edited by hawaiian robots; 05-01-2010 at 11:01 AM.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Stop giving code samples when no effort has been made by the OP to solve the problem.

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You can do it in one line of 'main()'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 10:33 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 03:15 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21