Please explain why the program seg fault?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sathishc58
    New Member
    • Sep 2007
    • 34

    #1

    Please explain why the program seg fault?

    Hi All,

    I tried to understand the reason for core dump but I could not get the reason. Could anyone help me to figure out the issue.

    Code:
         1  #include <stdio.h> 
         2  #include <string.h> 
         3  #include <malloc.h> 
         4 
         5  int main() 
         6  { 
         7      int   i; 
         8      char *ptr[];   --> If dimension is given then the program 
    is working fine. 
         9 
        10      for ( i = 0; i < 2; i++ ) 
        11      { 
        12          ptr[i] = malloc(6); 
        13 
        14          memcpy(ptr[i], "Hello", 6); 
        15      } 
        16 
        17      for ( i = 0; i < 2; i++ ) 
        18      { 
        19          printf("The value of i is %d\n", i); 
        20          printf("The string is %s\n", ptr[i]); 
        21          free(ptr[i]); 
        22      } 
        23 
        24      printf("out %d\n", i); 
        25 
        26      return 0; 
        27  }
    [08:49:22]/home/e025400/sathish <-> cc -o arrofptr arrofptr.c
    "arrofptr.c ", line 8: warning: null dimension: ptr


    [08:49:28]/home/e025400/sathish <-> arrofptr
    The value of i is 0
    The string is Hello
    The value of i is 1
    The string is Hello
    out 2
    Segmentation Fault(coredump)


    If I give dimension at "line no 8" then the program works fine else it dumps core after printing out all the values correctly. Please let me know the reason.

    And I have a basic doubt, who argv[] in main() works.


    Thanks & Regards
    Sathish Kumar
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by sathishc58
    [08:49:22]/home/e025400/sathish <-> cc -o arrofptr arrofptr.c
    "arrofptr.c ", line 8: warning: null dimension: ptr
    This says it all really, you have created an array with a 0 dimension, then later in the program you use the array resulting in an out of bounds memory access.

    If you don't want to give a dimension at compilation time declare it as char ** and allocate data for the array of pointers at run time.

    Comment

    • sathishc58
      New Member
      • Sep 2007
      • 34

      #3
      Dear Banfa,

      Thanks for your reply. Could you please let me know the suggestion/answer as how argv[] in main() behaves.

      Because argv[] would be declared with NULL dimension always if I am not wrong. I wrote the program by keeping argv[] in mind.


      Thanks & Regards
      Sathish Kumar

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Dimension of argv is not NULL. Please read this.

        Regards
        Dheeraj Joshi

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          In any function prototype, definition or declaration using the syntax int parameter[] is equivalent to int* parameter, that is it equates to the declaration of a pointer because you can not pass arrays as parameters or return them from functions.

          In a block of code using [] declares an array. Using [] can be useful because then the compiler declares the array with the correct size to hold its initialisation data, for example

          char text[] = "Hello World";

          the compiler can tell that "Hello World" requires 12 char to contain it so automatically makes the array that size. If there is no initialiser list

          char text[];

          then there is nothing to hold and this becomes the equivalent of

          char text[0];

          An array with no or null size, which is not at all useful which is why your compiler produced a warning.

          Comment

          Working...