Thread: Segmentation fault; Pointer to Pointer;Parsing;Input Redirection

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    14

    Segmentation fault; Pointer to Pointer;Parsing;Input Redirection

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    // John Raphael B. Pascua 2015-13687
    int main(void){
        char *POI = (char*)calloc(1 , sizeof(char));
        char **POI2POI = (char**)calloc(1, sizeof(char));
        char test='a';
        int CNT=0,CNT2=1,CNT3=0;
        
        do{
            test = getchar();
            if(test != EOF){
                CNT2++;
                POI=realloc(POI,sizeof(char)*CNT2);
                *(POI+CNT) = test;
                CNT++;
            }
        }while(test != EOF);
        
        for(CNT=1,CNT2=0,CNT3=0;POI[CNT] != '"';CNT++){
            if(POI[CNT] != '"' && POI[CNT] != 10 && POI[CNT] != ' '){
                POI2POI[CNT2] = calloc(1,sizeof(char));
                POI2POI[CNT2][CNT3] = POI[CNT];
                CNT++;
                CNT3++;
                while(POI[CNT] != ' ' || POI[CNT] != 10 || POI[CNT] != ','){
                    POI2POI[CNT2] = realloc(POI2POI[CNT2],sizeof(char)*CNT3+1);
                    POI2POI[CNT2][CNT3]=POI[CNT];
                    CNT++;
                    CNT3++;
                }
            }if(POI[CNT] != ','){
                POI2POI[CNT][CNT3]=0;
                CNT3=0;
                CNT2++;
                POI2POI = realloc(POI2POI, sizeof(char*)*(CNT2+1));
            }
        }
       for(CNT3=0;CNT3<CNT2;CNT3++){
           free(POI2POI[CNT3]);
       }
       free(POI2POI);
       free(POI);
        return 0;
    }
    Why does my code put out a segfault when I try to print POI2POI[CNT2][CNT3]? Is there anything wrong with my allocation?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is clearly wrong:
    Code:
    char **POI2POI = (char**)calloc(1, sizeof(char));
    Actually, it would not be so clear if it were an assignment later in the code, and if the cast was not there, but the cast is unnecessary. You should have written:
    Code:
    char **POI2POI = calloc(1, sizeof(POI2POI[0]));
    This way, you reduce the risk that you might get the type wrong, i.e., you get sizeof(char*) instead of the incorrect sizeof(char).

    That said, it is rather inefficient to allocate and re-allocate one by one. A typically better approach is to either allocate and re-allocate in fixed size blocks, or re-allocate by a factor (e.g., doubling the size of the allocated block), depending on how large is the data, then keep track of the number of elements in use along with the number of elements 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

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    14
    POI2POI[CNT2] = calloc(1,sizeof(char));
    POI2POI[CNT2][CNT3] = POI[CNT];

    Is there something wrong here? It posts a segmentation fault when I try to print it. Thank you for the reply

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Quote Originally Posted by JohnRaphael View Post
    POI2POI[CNT2] = calloc(1,sizeof(char));
    POI2POI[CNT2][CNT3] = POI[CNT];

    Is there something wrong here? It posts a segmentation fault when I try to print it. Thank you for the reply
    Your naming convention using all caps like that makes the code hard to read. Have you run it through a debugger such as GDB? One thing that stands out is that you do no bounds checking when indexing POI and POI2POI.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your code is overly-complex.
    If you describe what you're trying to do I'm sure there's a better way.
    It looks like you're trying to extract fields from an input string, assigning the fields to an array of strings.
    Sounds like it might be a job for fgets and strtok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault, pointer on pointer
    By 73c in forum C Programming
    Replies: 1
    Last Post: 05-10-2014, 04:55 AM
  2. Replies: 11
    Last Post: 06-27-2012, 07:05 AM
  3. Segmentation Fault With pointer to pointer strcpy
    By touchy in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2011, 12:35 AM
  4. Pointer Segmentation Fault
    By Ggregagnew in forum C Programming
    Replies: 3
    Last Post: 11-28-2010, 02:37 AM
  5. Segmentation Fault With Pointer
    By CYBERRRR in forum C Programming
    Replies: 4
    Last Post: 11-10-2010, 05:19 PM