C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2008, 01:06 PM   #31
Registered User
 
Join Date: Apr 2006
Location: United States
Posts: 3,201
@MK27 - well, oops.

I was pretty sure I was wrong when I hit send, but it always happens that way doesn't it? I can find some creative words to describe you too, by the way. I suggest you not dish out unwarranted insults.
__________________
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, 01:07 PM   #32
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Oh! And who could forget this little gem.

Example:
Code:
#include <stdlib.h>
#include <stdio.h>

#define IS_LESS_THAN <

int main(int the_number_of_command_line_arguments, char **the_command_line_parameter)
{
  char buffer[4096];

  if(the_number_of_command_line_arguments IS_LESS_THAN 3)
    return EXIT_FAILURE;

  snprintf(buffer, sizeof(buffer)-1, "copy %s %s", 
    the_command_line_parameter[1],
    the_command_line_parameter[2]);

  system(buffer);
  return EXIT_FAILURE;
}
master5001 is offline   Reply With Quote
Old 12-05-2008, 01:20 PM   #33
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by tabstop View Post
Except "before" is "malloc(1)". Since when does malloc put a null terminator at the end of memory? Never, is when.
But if that's all that happened we return 0 to indicate we reached EOF without adding to buffer, so the one byte buffer that we never wrote to is freed.

My explanation was a bit off but the code remains brilliant.
__________________

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:22 PM   #34
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by whiteflags View Post
@MK27 - well, oops.

I was pretty sure I was wrong when I hit send, but it always happens that way doesn't it? I can find some creative words to describe you too, by the way. I suggest you not dish out unwarranted insults.
Hey, I didn't mean I thought charlatanism was bad or that calling someone a charlatan is an insult.

I do it myself whenever possible

ps. there should be an "edit" button on the lower right...
__________________

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:26 PM   #35
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Oooops, I didn't notice the line number mumbo jumbo. MK27's code hardly seems brilliant. I mean come on now dude...

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 line;

    ifile = fopen(argv[1], "r");
    ofile = fopen(argv[2], "w");

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

      if(ifile)
        fclose(ifile);

      if(ofile)
        fclose(ofile);

      return EXIT_FAILURE;
    }

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

    line = 0;
    while(fgets(buffer, 0x80000, ifile))
    {
      fprintf(ofile, "%3d. %s", ++line, buffer);
    }

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

  return EXIT_SUCCESS;
}
master5001 is offline   Reply With Quote
Old 12-05-2008, 01:30 PM   #36
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Quote:
Originally Posted by MK27 View Post
Sometimes you get what you asked for:

edit: there's actually three very minor bugs in this, and Salem found them all (see subsequent posts).
I do not see any minor errors. I only see crippling ones and ones that make the program HIGHLY under-efficient if it even works to begin with.

You obviously know where the edit button is, so please fix your code.
master5001 is offline   Reply With Quote
Old 12-05-2008, 01:30 PM   #37
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by MK27 View Post
But if that's all that happened we return 0 to indicate we reached EOF without adding to buffer, so the one byte buffer that we never wrote to is freed.

My explanation was a bit off but the code remains brilliant.
Well, it's not freed by you. The person using streamline will have to free it.

You can say (I guess you are saying) that it's an undocumented (as of yet) feature that your function will leave buffer in an invalid state when \n is immediately followed by EOF, and I would agree with you that one fix is to then document that when your function returns 0, buffer is invalid and should not be accessed. I will leave it to you to determine whether that's a bug in the code or a bug in the comments (since your comment about what return 0 means at the bottom is incorrect).
tabstop is offline   Reply With Quote
Old 12-05-2008, 01:41 PM   #38
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by tabstop View Post
Well, it's not freed by you. The person using streamline will have to free it.
I'm going to claim that it's actually me and I did do it. Honest.

But, don't you think it would be going too far, after having actually written some half-brilliant code which I should never have done because it might get used in someone's homework, to then provide our negligent student with some kind of API documentation to complete the charade?
__________________

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:45 PM   #39
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
I notice Master5001 managed to streamline things a bit while sacrificing security. b/t/w, why does
Code:
malloc(0x80000)
equal .5mb?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 12-05-2008 at 01:48 PM.
MK27 is offline   Reply With Quote
Old 12-05-2008, 01:46 PM   #40
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Quote:
Originally Posted by MK27 View Post
I'm going to claim that it's actually me and I did do it. Honest.

But, don't you think it would be going too far, after having actually written some half-brilliant code which I should never have done because it might get used in someone's homework, to then provide our negligent student with some kind of API documentation to complete the charade?
I hate to be the psyche student on you but your code is neither brilliant nor worth using for homework purposes. I was going to simply correct your code but the entire way it works was too flawed to even fix it. You do realize that allocation is one of the most expensive operations you can do, right?
master5001 is offline   Reply With Quote
Old 12-05-2008, 01:49 PM   #41
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Quote:
Originally Posted by MK27 View Post
I notice Master5001 managed to streamline things a bit. b/t/w, why does
Code:
malloc(0x80000)
equal .5mb?
Because 0x80000 is half a mebabyte in hexidecimal. Figure if 0x100000 is 1MG, then 0x100000 / 2 = 0x80000. Right? .5MB = 2^19, right?
master5001 is offline   Reply With Quote
Old 12-05-2008, 01:53 PM   #42
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by master5001 View Post
I was going to simply correct your code but the entire way it works was too flawed to even fix it. You do realize that allocation is one of the most expensive operations you can do, right?
I am super glad you told me this because I was just going to start a new thread asking that very question. So it's much better to go malloc(1000) than for(i=0;i<1000;i++) malloc(1) ????!

ps. yes, so diagnosed, you know about the meds I guess?
__________________

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:57 PM   #43
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by MK27 View Post
I'm going to claim that it's actually me and I did do it. Honest.

But, don't you think it would be going too far, after having actually written some half-brilliant code which I should never have done because it might get used in someone's homework, to then provide our negligent student with some kind of API documentation to complete the charade?

Since the whole exercise was already going too far, that's irrelevant.

I realize you were happy to have a solution that (seemed to) work, and you wanted to share; and if you had just done so, you probably would still have had a couple people make noises about doing people's homework, but that's mostly small potatoes (everybody has a line, but in different places), and using code as a "teachable moment" can be useful.

The flip side is that you have become a regular here -- you've started 25 threads in the last two months, after all -- and a known quantity. And posting the code that you did, calling it "brilliant" and "bug-free"; well, that's a teachable moment, too. Not just that pride goeth before a fall, but maybe to see what bug-free code actually is.

I'm getting way too philosophical for a Friday, so I'll stop.
tabstop is offline   Reply With Quote
Old 12-05-2008, 02:00 PM   #44
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Haha you made me laugh :-) In all seriousness, your code isn't "not-brilliant" because of how you did it. It is problematic because of the fact that global buffers are just plain messy. Simply put your code is quite excellent except that it requires the caller to do stuff to clean up after each and every call to the function. And the function is slower because of the fact that it must allocate buffers each and every call (well, probably not, malloc may be over-allocating).

Its better to do malloc(1000) than malloc(1) and incrementally increase the buffer size. What you can do is have a factor to increase your malloc by and just allocate chunks at a time.
master5001 is offline   Reply With Quote
Old 12-05-2008, 02:02 PM   #45
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by tabstop View Post
you've started 25 threads in the last two months, after all
Wow. Well, at least the_number_of_people_who_found_most_of_those_helpf ul>=1
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 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:11 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