Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, December 14, 2015

Significance of double pointer and triple pointer in this code snippet

Significance of double pointer and triple pointer in this code snippet


#include  #include  void add(char **p);  void print(char **p);  int cnt=0;  main()  {    int option;    char **p=NULL;  while(1)  {    printf("------MENU-----\n");    printf("1>input\n 2>print\n3>exit\n");    printf("enter ur choice\n");    scanf("%d",&option);getchar();   switch(option)   {      case 1: add(p);              break;      case 2: print(p);              break;     case 3: return;     default: printf("Invalid option\n");     }  }  }  void add(char **p)  {    int i;    p=(char**)realloc(p,(cnt+1)*sizeof(char*));    if(p==NULL)    {          printf("Error: memory not available\n");          return;      }    p[cnt]=NULL;    p[cnt]=(char*)realloc(p[cnt],20*sizeof(char));      puts("enter a name");    gets(p[cnt]);    cnt++;    printf("cnt=%d\n",cnt);  }  void print(char **p)  {    int i;    for(i=0;i

In the above code, I am making a database of names. For this I am using dynamic memory allocation. I am allocation memory for 2D-array using array of pointers method. When I am executing this program on gcc compiler, I am getting segmentation fault. I am not understanding why is it happening?Could you please tell me where the bug is?

Answer by Gopi for Significance of double pointer and triple pointer in this code snippet


Pass by reference. You need to pass the address of your pointer to make sure the changes in function add() is reflected in main(). Avoid using gets()

In this case

add(&p);  

Accordingly your add() function definition should change to handle this. Or the other way is for the add function to make the required allocations and return that address to your pointer

char **add();  

Check the code below:

char  **add(char **p)  {    int i;    p=(char**)realloc(p,(cnt+1)*sizeof(char*));    if(p==NULL)    {          printf("Error: memory not available\n");          return;      }    p[cnt]=NULL;    p[cnt]=(char*)realloc(p[cnt],20*sizeof(char));      scanf("%s",p[cnt]);    cnt++;    printf("cnt=%d\n",cnt);    return p;  }  

So your call should be:

p = add(p);  

Answer by undur_gongor for Significance of double pointer and triple pointer in this code snippet


p in main is handed over to add by value. add modifies a local copy then, but not the original p.

Besides the terrible formatting and everything you need to hand a pointer to your main's p to add:

...      case 1: add(&p);  ...    void add(char ***p)  {    int i;    *p = realloc(*p,(cnt+1)*sizeof(char*));    if(*p==NULL)    {          printf("Error: memory not available\n");          return;      }    (*p)[cnt]=NULL;    (*p)[cnt]=realloc((*p)[cnt],20*sizeof(char));      puts("enter a name");    gets((*p)[cnt]);    cnt++;    printf("cnt=%d\n",cnt);  }  

Answer by Jens for Significance of double pointer and triple pointer in this code snippet


In main, all you do is to assign p = NULL and then use this NULL pointer in print, which causes the segfault. Note that add(p) is equivalent to add(NULL) and does not change p. Did you mean to pass an address with add(&p)? If so, you need to fiddle a bit with the number of *.

Answer by Lavish Kothari for Significance of double pointer and triple pointer in this code snippet


A copy of 2D pointer p is created in the add function, and updating this pointer will not be reflected in the main function. A quick solution to this problem is to return the 2D pointer 'p' from the add function.

char** print(char **p)  {      ....      ....      return p;  }  

Answer by AAT for Significance of double pointer and triple pointer in this code snippet


Apart from the other things, I don't think anyone's mentioned the realloc trap of reallocation failure. Suppose this call

p=(char**)realloc(p,(cnt+1)*sizeof(char*));  

fails to allocate the new memory: what happens then? Yes, you will get NULL returned: but the memory which was allocated, and which pis pointing to, does not get free'd. Instant memory leak.

You must call realloc like this:

char* pTemp = realloc(p, cnt + 1);  if(pTemp != NULL)  {      p = pTemp;             // Now safe to update p  }  else  {      // Error handling as required, possibly including freeing p  }  

Yet another reason not to use realloc. It doesn't really by you very much over doing the buffer copies yourself IMHO.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.