none
free(pointer) in ansi C.

    Question

  • Hello, I'm using Visual C++ 2003 Standard for C coding.
    I have a problem with a freeing the pointer. In one place in my program it works, in another not. On gcc all goes well. Here is the code:

    void basic_pointers(){
        int *pointer;
        int a=10;
        if (pointer == NULL)
        {

            printf("Out of memory, exiting\n");
            _getch();
            /* end application */
            return 0;
        }
        pointer=&a;
        printf("%d", *pointer);
        free(pointer);
       pointer = NULL;
    }

    When I comment last 2 lines it compiles well. Thank you in advance.
    Friday, July 08, 2005 10:16 AM

Answers

  • Ok, I have found the answer. I should use malloc/calloc for pointer like this:

    int basic_pointer(){
        int *pointer;
        int a=10;
        if (pointer == NULL)
        {

              printf("Out of memory, exiting\n");
              _getch();
          /* end application */
          return 0;
        }
        pointer=calloc(a, sizeof(int));
        printf("%d", *pointer);
        free(pointer);
        pointer = NULL;
        return 0;
    }

    Friday, July 08, 2005 9:31 PM
  • One minor nitpicking point:

    The calloc line should occur before the if(pointer==NULL) line. The reason is because until calloc is called, pointer is in an uninitialized state. This means that pointer has whatever value was present in memory before your program started (this could be anything. NULL, not NULL, completely non-existent memory).

        pointer=(int*)(calloc(a, sizeof(int)));
        /* The calloc line should occur first. */
        if (pointer == NULL)
        {

              printf("Out of memory, exiting\n");
              _getch();
          /* end application */
          return 0;
        }


    If you turned on all warnings, Visual C++ will complain about an uninitialized variable.

    Edit: Oh wait, a warning is issued even under default warning levels.
    Saturday, July 09, 2005 5:59 PM
    Moderator

All replies

  • You only free heap allocated memory. You should probably read a chapter on memory management in C.

    The code you wrote is bogus for any compiler, it is trying to free a pointer to a location on the execution stack. Our platform/compiler gives you a much more immediate diagnostic. That is goodness.

    Ronald Laereman's
    Visual C++ team
    Friday, July 08, 2005 10:54 AM
    Moderator
  • Ok, I know that diagnostic is very good and I'm happy about it. However this code is from C course and I still don't know what to do to solve it:

    I use "if (pointer == NULL)" to check if all is OK.
    Later I use "free(pointer);" and for security also "pointer = NULL;". What else I can do?
    Friday, July 08, 2005 11:00 AM
  • Ok, I have found the answer. I should use malloc/calloc for pointer like this:

    int basic_pointer(){
        int *pointer;
        int a=10;
        if (pointer == NULL)
        {

              printf("Out of memory, exiting\n");
              _getch();
          /* end application */
          return 0;
        }
        pointer=calloc(a, sizeof(int));
        printf("%d", *pointer);
        free(pointer);
        pointer = NULL;
        return 0;
    }

    Friday, July 08, 2005 9:31 PM
  • One minor nitpicking point:

    The calloc line should occur before the if(pointer==NULL) line. The reason is because until calloc is called, pointer is in an uninitialized state. This means that pointer has whatever value was present in memory before your program started (this could be anything. NULL, not NULL, completely non-existent memory).

        pointer=(int*)(calloc(a, sizeof(int)));
        /* The calloc line should occur first. */
        if (pointer == NULL)
        {

              printf("Out of memory, exiting\n");
              _getch();
          /* end application */
          return 0;
        }


    If you turned on all warnings, Visual C++ will complain about an uninitialized variable.

    Edit: Oh wait, a warning is issued even under default warning levels.
    Saturday, July 09, 2005 5:59 PM
    Moderator
  • Yes as above said your course did forget something :-)

    You only assign a reference to a stack pointer variable from a stack (value ) variable. Stack variables are not dynamically created at runtime. The compiler assigns an constant stack size to your exe when he compiles the code considering the number of variables etc.

    Only dynamic variables must be freed by the programmer via free in C or delete in C++ coz they are created on the heap.

    void foo()
    {
     int a; <- first variable on stack frame
     float b; <- second variable on stack frame
     double* c; <- third pointer variable on stack frame
    }

    Cya
    MartinBig Smile
    Sunday, July 10, 2005 8:02 PM
  • Mark

    Monday, July 11, 2005 3:46 AM