home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 3935 < prev    next >
Encoding:
Text File  |  1992-07-27  |  3.1 KB  |  108 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!walter!att!cbfsb!cbnewsf.cb.att.com!rajeev
  3. From: rajeev@cbnewsf.cb.att.com (rajeev.dolas)
  4. Subject: Re: I am stumped by this one!!!
  5. Message-ID: <1992Jul27.134826.5592@cbfsb.cb.att.com>
  6. Sender: news@cbfsb.cb.att.com
  7. Organization: AT&T
  8. References: <1992Jul24.204944.18516@cbfsb.cb.att.com> <Brx394.4qL@undergrad.math.waterloo.edu> <13744@auspex-gw.auspex.com>
  9. Distribution: usa
  10. Date: Mon, 27 Jul 1992 13:48:26 GMT
  11. Lines: 95
  12.  
  13. In article <13744@auspex-gw.auspex.com> guy@Auspex.COM (Guy Harris) writes:
  14. >>>        cp_line = (char *)malloc(sizeof(strlen(pars_wbuf)));
  15. >>
  16. >> You're allocing the sizeof an int, which is likely four bytes.
  17. >> You also forgot to alloc space for the '\0'. Try it again with:
  18. >>
  19. >>    cp_line = (char*)malloc(sizeof(strlen(pars_wbuf) + 1));
  20. >
  21. >No, try it again with:
  22. >
  23. >>    cp_line = (char*)malloc(strlen(pars_wbuf) + 1);
  24. >
  25. >because, as you noted, the "sizeof" doesn't belong there - it turns the
  26. >argument into the size of the result of "strlen", which isn't the length
  27. >of the string.
  28. >
  29. >(Also, try not leaving every single line of the original posting in your
  30. >followup; it makes it rather hard to find the stuff that *doesn't* come
  31. >from the original posting....)
  32.  
  33.     I realize the error on sizeof instead of strlen() for malloc. But
  34.     I don't think that is the cause of SEGV error. If you look at my
  35.     original posting, you will notice that I am able to copy selected
  36.     characters to cp_line.
  37.  
  38.     In my original code I wasn't even using the cp_line. I was reading
  39.     a char pointed to by *pbuf and was writing that to the file. I
  40.     got the SEGV error on the putc()!!! Now I am using fputs() to
  41.     write a string instead of a char and I get the same error on
  42.     fputs(). Also fputs(tmp_line,stdout) works fine while 
  43.     fputs(tmp_line,fp1) does not!!! That's what is driving me nuts.
  44.     
  45.     I am including the relevant portion of the code to clarify my
  46.     point and not to drive the 2400 baud-ers nuts :-)
  47.  
  48.     /* str_comp is my function which returns -1 if match is not found */
  49.     while((fgets(pars_wbuf, 80, fp2) != NULL) &&
  50.         ((parse_ret = str_comp(cmp, pars_wbuf)) != 0))
  51.     {
  52.         pars_wbuf[strlen(pars_wbuf)] = '\0';
  53.         pbuf = pars_wbuf;
  54.         cp_line = (char *)malloc(sizeof(strlen(pars_wbuf)));
  55.         strcpy(cp_line, "\0");
  56.         tmp_line = cp_line;
  57.  
  58.         if((comp = line_comp(pars_wbuf)) == 0)
  59.             ;
  60.         else 
  61.         {
  62.             /* Traverse thru pbuf, skip "
  63.             while(*pbuf != '\0')
  64.             {
  65.                 if(*pbuf != 0x0D)
  66.                 {
  67.                     /* I was writing a char at a time to
  68.                        the file before. This part works
  69.                        on Amdahl but not on a sun.    */
  70.  
  71.                     /**
  72.                     ch = *pbuf;
  73.                     fprintf(stdout, "%c", ch); 
  74.                     if((he = putc(ch, fp1)) < 0)
  75.                     {
  76.                          printf("pars_conf: Write error\n");
  77.                         return(-13);
  78.                     }
  79.                     **/
  80.  
  81.                     /* Now I save it to a buffer */
  82.                     *cp_line++ = *pbuf;
  83.                 }
  84.                 *pbuf++;
  85.             }
  86.             /* If I do a fputs to stdout, it works fine */
  87.             /**
  88.             fputs(tmp_line, stdout);
  89.             fflush(stdout);
  90.             **/
  91.  
  92.             /* dbxtool shows the following error on this instr -
  93.              signal SEGV in malloc at 0xf773634
  94.              malloc+0x150:    st    %o0, [%15]
  95.              */
  96.             fputs(tmp_line, fp1);
  97.         }
  98.         free(cp_line);
  99.     }
  100.     
  101. }
  102.  
  103.  
  104. Thanks again
  105.  
  106. Raj Dolas.
  107.  
  108.