Thread: realloc and pointer conversion from sub to main function

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    Question realloc and pointer conversion from sub to main function

    Hello.
    I'm having a difficult time figuring out how to set the plines[n] pointer. It looks like the realloc is working in the sub program, but I'm not having luck with printing it out in the main program.

    On compile, I get, "Suspicious pointer conversion in function readfile". Basically, this means the pointer is not "set" on strdup(). Can somebody please take a look at this and help me with this pointer problem?
    Thanks!

    Code:

    /* dynline2.c */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int readfile(char ***plines);
    int main(void){
    char **plines; /* ptr to ptr array */
    int n, k; /* counters */

    n = readfile(&plines);

    /* test prints */
    printf("\nfmain:\nplines-*>%p\nplines-s>%s\n n->%d\n", plines, plines[n], n);

    for(k=0; k<n; k++){
    puts(plines[k]); /* print line */
    free(plines[k]); /* free storage */
    }

    free(plines); /* free storage */

    return 0;
    }

    int readfile(char ***plines){
    const int hunk=10; /* increment of linesize */
    char *line; /* input buffer */
    char *tmp1, ***tmp2; /* temporaries */
    int linesize; /* current size of line input area */
    int n; /* counters */

    line=(char *)malloc(hunk); /* allocate space for input line */
    if (line==NULL) { perror("dynline2"); exit(1); }
    linesize = hunk;
    plines=NULL;

    for(n=0;;n++){
    if(feof(stdin)) break; /* if EOF then no more lines */
    line[0]='\0';
    for(;;){
    tmp1 = fgets(line+strlen(line), linesize-strlen(line), stdin);
    if (tmp1==NULL) break;
    if ( line[-1+strlen(line)] == '\n'){/*if \n then end of line */
    strtok(line, "\n"); /* discard \n */
    break;
    }
    linesize = linesize + hunk;
    tmp1=(char *)realloc(line, linesize); /* storage for ptr */
    if ( tmp1==NULL ) { perror("dynline2"); exit(1); }
    else line=tmp1;
    }

    tmp2=(char ***)realloc( plines, (n+1)*(sizeof(char ***)) );
    if (tmp2==NULL) { perror("dynline2"); exit(1); }
    else plines=tmp2;
    plines[n]=strdup(line); /* allocate space for ptr */
    puts("test print plines[n]:");
    puts(plines[n]);

    /* test prints */
    puts("freadfile:");
    printf("plines-*>%p \n tmp2-*>%p \nplines-s>%s \n tmp2-s>%s\n", plines, tmp2, plines, tmp2);
    printf(" line-*>%p \n tmp1-*>%p \n line-s>%s \n tmp1-s>%s\n", line, tmp1, line, tmp1);

    if (plines[n]==NULL) { perror("dynline2"); exit(1); }
    }
    free(line);

    return n;
    }

    compile:
    C:\BORLAND\BCC55\BIN>bcc32 -odynline2 dynline2.c
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    dynline2.c:
    Warning W8075 dynline2.c 57: Suspicious pointer conversion in function readfile
    Warning W8075 dynline2.c 59: Suspicious pointer conversion in function readfile
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

    output:
    C:\BORLAND\BCC55\BIN>dynline2
    hello world how are you?
    test print plines[n]:
    hello world how are you?
    freadfile:
    plines-*>007C2750
    tmp2-*>007C2750
    plines-s>`'|
    tmp2-s>`'|
    line-*>007C272C
    tmp1-*>007C273F
    line-s>hello world how are you?
    tmp1-s> you?
    I'm having difficulty figuring this sucker out?
    test print plines[n]:
    I'm having difficulty figuring this sucker out?
    freadfile:
    plines-*>007C2750
    tmp2-*>007C2750
    plines-s>`'|
    tmp2-s>`'|
    line-*>007C2780
    tmp1-*>007C27A7
    line-s>I'm having difficulty figuring this sucker out?
    tmp1-s>ker out?
    What is the problem?
    test print plines[n]:
    What is the problem?
    freadfile:
    plines-*>007C2750
    tmp2-*>007C2750
    plines-s>`'|
    tmp2-s>`'|
    line-*>007C2780
    tmp1-*>007C2780
    line-s>What is the problem?
    tmp1-s>What is the problem?
    ^Z
    test print plines[n]:

    freadfile:
    plines-*>007C280C
    tmp2-*>007C280C
    plines-s>`'|
    tmp2-s>`'|
    line-*>007C2780
    tmp1-*>00000000
    line-s>
    tmp1-s>(null)

    fmain:
    plines-*>007C21CC
    plines-s>(null)
    n->4
    Last edited by cjtotheg; 02-04-2002 at 11:14 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    On the face of it, it looks OK

    But I would do this, to trim the number of *** you have to deal with at any one time

    This is how I would structure your readfile function
    Code:
    int readfile ( char ***plines ){
      int num_lines = 0;
      char **read_lines = NULL;
    
      // here, you just deal with read_lines, which is just a char**
      // and you don't have to bother with an extra level of indirection
      // at the end, just copy the whole shooting match back to the
      // outside world with a simple assignment, like so
      *plines = readlines;
      return num_lines;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    Thanks Salem

    I'm still trying to get this to work, but I'm much closer due to your response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Replies: 3
    Last Post: 08-22-2008, 11:12 AM
  3. problem with pointer passing to function
    By umeshjaviya in forum C Programming
    Replies: 3
    Last Post: 05-02-2008, 05:44 AM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM