Thread: another question about strcpy

  1. #1
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    another question about strcpy

    Hello everyone.
    I found on a book this function(I suppose it works)
    which reads some lines and returns the number of lines readed.
    Code:
    int readlines(char *lineptr[],int maxlines)
    {int len,nlines;
    char *p,line[MAXLEN];
    nlines=0;
    while((len=getline(line,MAXLEN))>0)
      if(nlines>=maxlines || p=alloc(len)==NULL)
        return -1;
    else{line[line-1]='\0';
    strcpy(p,line);
    lineptr(nlines++)=p;
    }
    
    return nlines;
    }
    The problem is why using this
    Code:
    strcpy(p,line);
    lineptr(nlines++)=p;
    instead of this?
    Code:
    lineptr(nlines++)=line;
    What's the difference?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What's the difference?
    p refers to dynamic memory that isn't tied to the function. line is a local array that will be destroyed when the function returns. There are two problems with your suggested change (presumably with the following fix):
    Code:
    lineptr[nlines++]=line;
    First, there's only one line. This assignment points every pointer in the array to the same address, so after the loop, even if there are a dozen pointers, they all refer to the last line entered. Second, when the function returns, line is destroyed and all of the pointers will be dangling.

    That function looks extremely dicey. What book did you get it out of?
    My best code is written with the delete key.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1.
    Code:
    line[line-1]='\0'
    shoulde be
    Code:
    line[len-1]='\0'
    2.
    Code:
    lineptr(nlines++)=line;
    stores the pointer to the local variable line
    this address not changes
    so every pointer in the array stores the same address

    And after the function exits - the local variable is destroed, and the addres is invalid.

    On the other hand
    p is allocated for every string
    so the allocated address is new for each new line
    and this memory is valid till the corresponding free is called (somethere other place)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Well my teacher gave as this function and I think it's on K&R2 book. I didn't copy paste it and probably I did a few mistakes such as not declaring
    Code:
    #define MAXLEN 5000
    .
    Thanks for both answers

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Well my teacher gave as this function and I think it's on K&R2 book.
    I recognize the solution, but it's heavily butchered from the one in K&R.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. strcpy question
    By cman9999 in forum C Programming
    Replies: 9
    Last Post: 03-07-2003, 02:03 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM