Thread: When do we get Null Pointer Assignment

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    7

    Angry When do we get Null Pointer Assignment

    #include<alloc.h>

    int *ptr[2];
    int loc[2];

    call()
    {
    * ptr[0] = 20;
    *ptr[1] = 1;
    }

    assign()
    {
    int i;

    for( i = 0 ; i < 2; i++)
    if((ptr[i] = (int *) malloc(sizeof(int )))== NULL)
    {
    printf("Meemory not allocated");
    exit(0);
    }
    ptr[0] = &loc[0];
    ptr[1] = &loc[1];
    }

    freea()
    {
    int i;
    for(i = 0; i< 2; i++)
    {
    free(ptr[i]);
    }
    }


    main()
    {
    int i;
    for (i = 0 ; i < 2; i++)
    {
    assign();
    call();
    }
    freea();
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    In your freea() procedure you attempt to free a memory which is not allocated with malloc()

    see your assign() procedure:
    > ptr[0] = &loc[0];
    > ptr[1] = &loc[1];

    the pointers in the P array no longer can be managed by the heap manager (malloc, free ... etc.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. Null pointer assignment
    By Raghavan in forum C Programming
    Replies: 1
    Last Post: 01-19-2008, 10:23 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM