When do we get Null Pointer Assignment

This is a discussion on When do we get Null Pointer Assignment within the C Programming forums, part of the General Programming Boards category; #include<alloc.h> int *ptr[2]; int loc[2]; call() { * ptr[0] = 20; *ptr[1] = 1; } assign() { int i; for( ...

  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, 09:23 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05: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

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