Thread: Segmentation fault (core dumped)

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    14

    Segmentation fault (core dumped)

    Result is not as expected, line free(P); give output Segmentation fault (core dumped) what's the wrong with it
    Code:
    #include <stdio.h>#include <stdlib.h> 
    
    
     
    int main ()
    {
       int *P = NULL;
            P = malloc (1 * sizeof(int));
            
            if( P == NULL ){
              printf("Can't allocate memory!" );
            }
            else{
                  ( "Allocated memory!" );
                  P = 10;
                  printf(" P = %d", P);
            }
            
            printf(" P = %d", sizeof(P));
            free(P); // /*Release memory. */
            printf(" P = %d", sizeof(P));
            
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    Not compiling without a decent level of warnings enabled, to at least inform you when you're doing something dumb.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:13:15: warning: statement with no effect [-Wunused-value]
                   ( "Allocated memory!" );
                   ^
    foo.c:14:17: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
                   P = 10;
                     ^
    foo.c:15:22: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
                   printf(" P = %d", P);
                          ^
    foo.c:18:16: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
             printf(" P = %d", sizeof(P));
                    ^
    foo.c:20:16: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
             printf(" P = %d", sizeof(P));
                    ^
    Fix the errors, then try again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    14
    Quote Originally Posted by Salem View Post
    Not compiling without a decent level of warnings enabled, to at least inform you when you're doing something dumb.
    Fix the errors, then try again.
    Why the size of P is 4 after Release memory?

    P = 10
    P = 4
    P = 4

    Code:
    #include <stdio.h>#include <stdlib.h> 
     
    int main ()
    {
       int *P = NULL;
            P = malloc (1 * sizeof(int));
             
            if( P == NULL ){
              printf("Can't allocate memory!" );
            }
            else{
                  ( "Allocated memory!" );
                  *P = 10;
                  printf(" P = %d \n", *P);
            }
             
            printf(" P = %d \n", sizeof(*P));
            free(P); // /*Release memory. */
            printf(" P = %d", sizeof(*P));
             
        return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    > Why the size of P is 4 after Release memory?
    Because sizeof() tells you the size of objects.

    P = NULL;
    P = malloc (1 * sizeof(int));
    P = malloc (10000 * sizeof(int));

    Whether you do
    printf(" P = %d", sizeof(P));
    or
    printf(" P = %d", sizeof(*P));

    You'll get the same answer each time, no matter how (or even if) you initialise your pointer variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    14
    Quote Originally Posted by Salem View Post
    > Why the size of P is 4 after Release memory?
    Because sizeof() tells you the size of objects.
    What I think is

    P = NULL; memory is not allocated so size should be 0
    P = malloc (1 * sizeof(int)); memory allocated so size should be 4
    P = free(P); memory has been released so the size should be 0

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    Well if you want those answers, then sizeof() is the wrong thing to use.

    sizeof() happens at compile time, so it can't possibly give you a run-time answer.

    In fact, there is no portable way to get the answers you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    I suppose there is a portable way to get the answers you want. It's called "keep track of the number of bytes allocated"
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)
    By johnmerlino in forum C Programming
    Replies: 2
    Last Post: 01-01-2014, 06:08 PM
  2. Segmentation fault (core dumped)
    By catasturslykid in forum C Programming
    Replies: 4
    Last Post: 07-29-2013, 08:57 AM
  3. Segmentation Fault (core dumped)
    By Juan Cervantes in forum C Programming
    Replies: 9
    Last Post: 10-29-2012, 05:58 PM
  4. Segmentation fault (core dumped)
    By benjaminp in forum C Programming
    Replies: 8
    Last Post: 10-10-2012, 12:46 PM
  5. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM

Tags for this Thread