C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2008, 11:24 AM   #16
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 476
use splint
slingerland3g is offline   Reply With Quote
Old 12-05-2008, 11:47 AM   #17
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by slingerland3g View Post
Well MK27 you will be spoon feeding for a very long time then.

"Give a man a fish he is fed for a day, teach a man to fish he is fed for life"
google charalatan

What are we teaching today?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 12-05-2008, 11:57 AM   #18
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,357
Quote:
Originally Posted by MK27
google charalatan
That word is in Spanish, apparently, and means "rattler", according to the online source I checked. You probably mean charlatan, though the connection in both cases seems rather tenous.

The point of both Adak and slingerland3g is that we should avoid giving a complete answer to a homework problem until it has been shown that a genuine attempt has been made for a complete solution.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 12-05-2008, 12:03 PM   #19
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> if (argc<2) fatal(-1,"Two filenames required.");
If argc is 2, what is the value of argv[2] here?
> if ((fstOUT=fopen(argv[2],"w"))==NULL)

> if ((fstRO=fopen(argv[1],"ro"))==NULL)
Which non-standard compiler extension requires "o" in the mode?

> case -1: fprintf(fstOUT, "%d. %s\n",ln,buffer); // the "special case"
Yes, very special when you have this line in the called function.
> if (buffer==NULL) return -1; // last check

> buffer=realloc(buffer,++i+1); // more memory for next iteration
And if realloc fails, what happened to the memory you used to have?


Whilst it doesn't appear to be a problem in this instance, the fact that i starts at 0 and the amount of memory starts at 1 is a recipe for off-by-one errors at some point.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 12-05-2008, 12:13 PM   #20
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by laserlight View Post
That word is in Spanish, apparently, and means "rattler", according to the online source I checked. You probably mean charlatan, though the connection in both cases seems rather tenous.
Yes, I checked the spelling on line and accidently used the spanish one. The meaning is similiar, altho in english it would be more akin to someone that chatters/rattles on about things which s/he may not actually know. However, it's not prejudicial (I'm not a mean, angry person) but rather implies one is pretending or acting that way on purpose. It might even be considered a skill.

Quote:
Originally Posted by laserlight View Post
The point of both Adak and slingerland3g is that we should avoid giving a complete answer to a homework problem until it has been shown that a genuine attempt has been made for a complete solution.
Well, I know the policy of the cboard moderators is not to do people's homework. If you want to make it a rule that nobody should and that you will remove posts that do, then go ahead. Philosophically, I don't think it will make the forum better for anyone, tho. To be honest, if someone wants to copy something they don't understand and hand it in somewhere, then I hope they aren't paying to much for their own non-education (I'm not trying to help people "cheat").

Anyway, the OP does not claim to be a student with homework, which means s/he may not have the benefit of a class of peers and all the examples and advice one can find at an institution of higher learning, so thought I was being helpful, and, in fact, inviting questions with my unorthodox programming style

Which I am beginning to believe is pretty much "bug free" for sure now.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 12-05-2008, 12:28 PM   #21
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Not to ruin a perfectly good round of public humiliation on another member, but I should point out I posted code yesterday that does basically exactly this task. Though mine works a little differently than MK27's since it is designed to remove or find and replace something from a different file.
master5001 is offline   Reply With Quote
Old 12-05-2008, 12:28 PM   #22
Registered User
 
Join Date: Apr 2006
Location: United States
Posts: 3,201
Why was this reopened for discussion.

>> Which I am beginning to believe is pretty much "bug free" for sure now.

buffer=realloc(buffer, ++i+1);

The value of i is undefined. An expression must not reference a stored value more than once, in order to be defined. But that may not be the exact wording... it's in the Statements section of the Standard. Anyway, aside from what Salem pointed out earlier about the realloc statement. He's right about that - if realloc fails, you leak your own memory.

Lengthening the buffer one character at a time seems rather braindead. You might as well read character by character.

Not to mention that your streamline function will return leaving the string without a terminating zero, sometimes. And also, if streamline returns zero (which should happen if the line does not end in a newline) then you only succeed partially. Consider the last line in a file for example, which may not be followed with a newline and not copied by the program.
__________________
Os iusti meditabitur sapientiam
Et lingua eius loquetur indicium

"There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii)

http://www.myspace.com/whiteflags99
whiteflags is offline   Reply With Quote
Old 12-05-2008, 12:28 PM   #23
POeT GuY
 
Matus's Avatar
 
Join Date: Feb 2008
Location: Bz
Posts: 213
Lol, Hey I'm "Fluent" in spanish agreed with MK27 , enjoy guys, heading to class atm
__________________
PoEms R InsPiRatiOns of LIfE ExpErienCes!!

Matus is offline   Reply With Quote
Old 12-05-2008, 12:30 PM   #24
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
++i + 1 does not produce an undefined value for i. If he put ++i += i + 1, that would be a problem.
master5001 is offline   Reply With Quote
Old 12-05-2008, 12:46 PM   #25
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by Salem View Post
> if (argc<2) fatal(-1,"Two filenames required.");
If argc is 2, what is the value of argv[2] here?
Yep, there's a bug. It should be <3.

Quote:
> if ((fstRO=fopen(argv[1],"ro"))==NULL)
Which non-standard compiler extension requires "o" in the mode?
Hmm...right again. Should just be "r"

Quote:
> case -1: fprintf(fstOUT, "%d. %s\n",ln,buffer); // the "special case"
Yes, very special when you have this line in the called function.
> if (buffer==NULL) return -1; // last check
Wow, three in a row! I'm out. This should be return -2 of course.

Quote:
> buffer=realloc(buffer,++i+1); // more memory for next iteration
And if realloc fails, what happened to the memory you used to have?
If realloc fails, the program is over because the function returns -2. All the allocation is checked. If the program is over, it doesn't matter what happened to what used to be.

Quote:
Whilst it doesn't appear to be a problem in this instance, the fact that i starts at 0 and the amount of memory starts at 1 is a recipe for off-by-one errors at some point.
You have it backwards (the first character is 0, but that still requires 1 byte). Also, this ensures that we won't get a double free if the EOF is after a \n, since nothing is written into the (one byte) buffer.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 12-05-2008, 12:57 PM   #26
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by whiteflags View Post
buffer=realloc(buffer, ++i+1);

The value of i is undefined. An expression must not reference a stored value more than once, in order to be defined. But that may not be the exact wording... it's in the Statements section of the Standard. Anyway,
i is defined, and only referenced once here.

Quote:
Not to mention that your streamline function will return leaving the string without a terminating zero, sometimes.
Definitely incorrect, which is why I like words like charlatan. If it did that (at EOF after \n), then it did nothing to the string, which means the string still has a terminator from before.

Quote:
And also, if streamline returns zero (which should happen if the line does not end in a newline) then you only succeed partially. Consider the last line in a file for example, which may not be followed with a newline and not copied by the program.
This is very very clearly addressed in the code and notes. If streamline reaches EOF after writing to buffer,

buffer[i]='\0';

Quote:
Lengthening the buffer one character at a time seems rather braindead. You might as well read character by character.
I am reading character by character.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 12-05-2008, 12:58 PM   #27
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> If realloc fails, the program is over because the function returns -2. All the allocation is checked. If the program is over
Yeah today maybe.
But sooner or later, you'll use realloc in the same way and you won't be exiting the program.
THEN you'll have a problem.

> You have it backwards (the first character is 0, but that still requires 1 byte).
Like I said, I didn't think there was a problem, just that the code was messy and error prone.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 12-05-2008, 01:00 PM   #28
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Example:
Code:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
  if(argc != 3)
    puts("Usage: copyfile [from] [to]");
  else
  {
    FILE *ifile, *ofile;
    char *buffer;
    size_t size, total;

    ifile = fopen(argv[1], "rb");
    ofile = fopen(argv[2], "wb");

    if(!ifile || !ofile)
    {
      perror("Could not complete this operation.");

      if(ifile)
        fclose(ifile);

      if(ofile)
        fclose(ofile);

      return EXIT_FAILURE;
    }

    if(!(buffer = malloc(0x1000))) /* 4k is sufficient */
    {
      fputs("Out of memory!\n", stderr);
      fclose(ifile);
      fclose(ofile);
      return EXIT_FAILURE;
    }

    total = 0;
    while((size = fread(in, 1, 0x1000, ifile)))
    {
      fwrite(out, 1, size, ofile);
      total += size;
      printf("\r%d bytes copied", total);
    }

    free(buffer);
    fclose(ifile);
    fclose(ofile);
  }

  return EXIT_SUCCESS;
}
But that is just my two cents.
master5001 is offline   Reply With Quote
Old 12-05-2008, 01:04 PM   #29
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by Salem View Post
> If realloc fails, the program is over because the function returns -2. All the allocation is checked. If the program is over
Yeah today maybe.
But sooner or later, you'll use realloc in the same way and you won't be exiting the program.
THEN you'll have a problem.
What if I promise not to?

Quote:
> You have it backwards (the first character is 0, but that still requires 1 byte).
Like I said, I didn't think there was a problem, just that the code was messy and error prone.
Messy! Is not!
Anyway, if by error prone you mean "there's some mistakes you could have made by doing it this way, but didn't" then I will take that as a compliment, Salem.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 12-05-2008, 01:04 PM   #30
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by MK27 View Post
i is defined, and only referenced once here.


Definitely incorrect, which is why I like words like charlatan. If it did that (at EOF after \n), then it did nothing to the string, which means the string still has a terminator from before.
Except "before" is "malloc(1)". Since when does malloc put a null terminator at the end of memory? Never, is when.
tabstop is offline   Reply With Quote
Reply

Tags
copy, data, file, mode, modify

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
File transfer- the file sometimes not full transferred shu_fei86 C# Programming 13 03-13-2009 12:44 PM
gcc link external library spank C Programming 6 08-08-2007 03:44 PM
Possible circular definition with singleton objects techrolla C++ Programming 3 12-26-2004 10:46 AM
spell check in C using a dictionary file goron350 C Programming 10 11-25-2004 06:44 PM
gcc problem bjdea1 Linux Programming 13 04-29-2002 06:51 PM


All times are GMT -6. The time now is 04:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22