public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* assertion "ptr != MAP_FAILED" failed while using mmap
@ 2003-06-25 15:17 Alex Vinokur
  2003-06-25 16:51 ` Elfyn McBratney
  2003-06-26 19:21 ` Comparative performance : Reading contents from file into one string Alex Vinokur
  0 siblings, 2 replies; 11+ messages in thread
From: Alex Vinokur @ 2003-06-25 15:17 UTC (permalink / raw)
  To: cygwin

===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================

Here is some function.

--------------------------------------
void read_file (char* filename_i)
{
int fd = open(filename_i, O_RDONLY);
  assert (fd > 2);

off_t sz = lseek(fd, 0, SEEK_END);
char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);

  assert (ptr != MAP_FAILED);  // Here assertion failed
  if (ptr != MAP_FAILED)
  {
    string str(ptr, ptr+sz);
    munmap(ptr, sz);
  }

  close(fd);
}
--------------------------------------

Assertion  "ptr != MAP_FAILED)" failed.
What might cause that?

Thanks,
--
   ==========================================
   Alex Vinokur
     mailto:alexvn@connect.to
     http://www.simtel.net/pub/oth/19088.html
     http://sourceforge.net/users/alexvn
   ==========================================






--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-25 15:17 assertion "ptr != MAP_FAILED" failed while using mmap Alex Vinokur
@ 2003-06-25 16:51 ` Elfyn McBratney
  2003-06-25 16:55   ` Ronald Landheer-Cieslak
  2003-06-26 19:21 ` Comparative performance : Reading contents from file into one string Alex Vinokur
  1 sibling, 1 reply; 11+ messages in thread
From: Elfyn McBratney @ 2003-06-25 16:51 UTC (permalink / raw)
  To: Alex Vinokur; +Cc: cygwin

On Wed, 25 Jun 2003, Alex Vinokur wrote:

> ===========================================
> Windows 2000
> CYGWIN_NT-5.0 1.3.22(0.78/3/2)
> GNU gcc version 3.2 20020927 (prerelease)
> ===========================================
>
> Here is some function.
>
> --------------------------------------
> void read_file (char* filename_i)
> {
> int fd = open(filename_i, O_RDONLY);
>   assert (fd > 2);
>
> off_t sz = lseek(fd, 0, SEEK_END);
> char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);
>
>   assert (ptr != MAP_FAILED);  // Here assertion failed
>   if (ptr != MAP_FAILED)
>   {
>     string str(ptr, ptr+sz);
>     munmap(ptr, sz);
>   }
>
>   close(fd);
> }
> --------------------------------------
>
> Assertion  "ptr != MAP_FAILED)" failed.
> What might cause that?

This is just a stab in the dark, of course, but surely `ptr != MAP_FAILED'
would indicate that the mmap did not fail? Assertions (assert()) are based on
true or false, so the above assert is false in that `ptr != MAP_FAILED'.

Elfyn

-- 


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-25 16:51 ` Elfyn McBratney
@ 2003-06-25 16:55   ` Ronald Landheer-Cieslak
  2003-06-25 21:40     ` Elfyn McBratney
  0 siblings, 1 reply; 11+ messages in thread
From: Ronald Landheer-Cieslak @ 2003-06-25 16:55 UTC (permalink / raw)
  To: cygwin; +Cc: Alex Vinokur

On Wed, 25 Jun 2003, Elfyn McBratney wrote:

> On Wed, 25 Jun 2003, Alex Vinokur wrote:
> 
> > ===========================================
> > Windows 2000
> > CYGWIN_NT-5.0 1.3.22(0.78/3/2)
> > GNU gcc version 3.2 20020927 (prerelease)
> > ===========================================
> >
> > Here is some function.
> >
> > --------------------------------------
> > void read_file (char* filename_i)
> > {
> > int fd = open(filename_i, O_RDONLY);
> >   assert (fd > 2);
> >
> > off_t sz = lseek(fd, 0, SEEK_END);
> > char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);
> >
> >   assert (ptr != MAP_FAILED);  // Here assertion failed
> >   if (ptr != MAP_FAILED)
> >   {
> >     string str(ptr, ptr+sz);
> >     munmap(ptr, sz);
> >   }
> >
> >   close(fd);
> > }
> > --------------------------------------
> >
> > Assertion  "ptr != MAP_FAILED)" failed.
> > What might cause that?
> 
> This is just a stab in the dark, of course, but surely `ptr != MAP_FAILED'
> would indicate that the mmap did not fail? Assertions (assert()) are based on
> true or false, so the above assert is false in that `ptr != MAP_FAILED'.
> 
> Elfyn
> 
> 
Ehm..

If ptr != MAP_FAILED is not true, that means ptr == MAP_FAILED.
assert(ptr != MAP_FAILED) thus fails if mmap fails..

unless I'm missing something..

rlc



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-25 16:55   ` Ronald Landheer-Cieslak
@ 2003-06-25 21:40     ` Elfyn McBratney
  2003-06-26  5:06       ` Igor Pechtchanski
  0 siblings, 1 reply; 11+ messages in thread
From: Elfyn McBratney @ 2003-06-25 21:40 UTC (permalink / raw)
  To: cygwin

On Wed, 25 Jun 2003, Ronald Landheer-Cieslak wrote:

> On Wed, 25 Jun 2003, Elfyn McBratney wrote:
>
> > On Wed, 25 Jun 2003, Alex Vinokur wrote:
> >
> > > ===========================================
> > > Windows 2000
> > > CYGWIN_NT-5.0 1.3.22(0.78/3/2)
> > > GNU gcc version 3.2 20020927 (prerelease)
> > > ===========================================
> > >
> > > Here is some function.
> > >
> > > --------------------------------------
> > > void read_file (char* filename_i)
> > > {
> > > int fd = open(filename_i, O_RDONLY);
> > >   assert (fd > 2);
> > >
> > > off_t sz = lseek(fd, 0, SEEK_END);
> > > char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);
> > >
> > >   assert (ptr != MAP_FAILED);  // Here assertion failed
> > >   if (ptr != MAP_FAILED)
> > >   {
> > >     string str(ptr, ptr+sz);
> > >     munmap(ptr, sz);
> > >   }
> > >
> > >   close(fd);
> > > }
> > > --------------------------------------
> > >
> > > Assertion  "ptr != MAP_FAILED)" failed.
> > > What might cause that?
> >
> > This is just a stab in the dark, of course, but surely `ptr != MAP_FAILED'
> > would indicate that the mmap did not fail? Assertions (assert()) are based on
> > true or false, so the above assert is false in that `ptr != MAP_FAILED'.
> >
> > Elfyn
> >
> >
> Ehm..
>
> If ptr != MAP_FAILED is not true, that means ptr == MAP_FAILED.
> assert(ptr != MAP_FAILED) thus fails if mmap fails..
>
> unless I'm missing something..

But what's the point in 'assert (ptr != NULL)'? Surely the best way to test for
failure is the other way round (s/!=/==/), if ptr == MAP_FAILED assert, or
continue...No?

Elfyn
-- 


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-25 21:40     ` Elfyn McBratney
@ 2003-06-26  5:06       ` Igor Pechtchanski
  2003-06-26  6:08         ` Alex Vinokur
  0 siblings, 1 reply; 11+ messages in thread
From: Igor Pechtchanski @ 2003-06-26  5:06 UTC (permalink / raw)
  To: cygwin

On Wed, 25 Jun 2003, Elfyn McBratney wrote:

> On Wed, 25 Jun 2003, Ronald Landheer-Cieslak wrote:
>
> > On Wed, 25 Jun 2003, Elfyn McBratney wrote:
> >
> > > On Wed, 25 Jun 2003, Alex Vinokur wrote:
> > >
> > > > ===========================================
> > > > Windows 2000
> > > > CYGWIN_NT-5.0 1.3.22(0.78/3/2)
> > > > GNU gcc version 3.2 20020927 (prerelease)
> > > > ===========================================
> > > >
> > > > Here is some function.
> > > >
> > > > --------------------------------------
> > > > void read_file (char* filename_i)
> > > > {
> > > > int fd = open(filename_i, O_RDONLY);
> > > >   assert (fd > 2);
> > > >
> > > > off_t sz = lseek(fd, 0, SEEK_END);
> > > > char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);
> > > >
> > > >   assert (ptr != MAP_FAILED);  // Here assertion failed
> > > >   if (ptr != MAP_FAILED)
> > > >   {
> > > >     string str(ptr, ptr+sz);
> > > >     munmap(ptr, sz);
> > > >   }
> > > >
> > > >   close(fd);
> > > > }
> > > > --------------------------------------
> > > >
> > > > Assertion  "ptr != MAP_FAILED)" failed.
> > > > What might cause that?
> > >
> > > This is just a stab in the dark, of course, but surely `ptr != MAP_FAILED'
> > > would indicate that the mmap did not fail? Assertions (assert()) are based on
> > > true or false, so the above assert is false in that `ptr != MAP_FAILED'.
> > >
> > > Elfyn
> >
> > Ehm..
> >
> > If ptr != MAP_FAILED is not true, that means ptr == MAP_FAILED.
> > assert(ptr != MAP_FAILED) thus fails if mmap fails..
> >
> > unless I'm missing something..
>
> But what's the point in 'assert (ptr != NULL)'? Surely the best way to test for
> failure is the other way round (s/!=/==/), if ptr == MAP_FAILED assert, or
> continue...No?
>
> Elfyn

Umm, Elfyn, the semantics of "assert()" is that you assert some predicate
to be true.  If the predicate is indeed true, the program continues
normally.  If the predicate is false, the program fails.
The predicate in this case is "ptr != MAP_FAILED".  Thus, the predicate
was false when the assertion failed, and ptr == MAP_FAILED.

To the OP: this means that mmap() did fail for some reason.  It should
have set errno to indicate why.  You should check that instead of
asserting -- mmap does fail occasionally.  Also FYI, once you assert, the
following test for "ptr != MAP_FAILED" is redundant -- the program will
not get there otherwise.
	Igor
-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski, Ph.D.
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"I have since come to realize that being between your mentor and his route
to the bathroom is a major career booster."  -- Patrick Naughton


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-26  5:06       ` Igor Pechtchanski
@ 2003-06-26  6:08         ` Alex Vinokur
  2003-06-26  8:31           ` Alex Vinokur
  2003-06-26  9:23           ` Brian Dessent
  0 siblings, 2 replies; 11+ messages in thread
From: Alex Vinokur @ 2003-06-26  6:08 UTC (permalink / raw)
  To: cygwin


"Igor Pechtchanski" <pechtcha@cs.nyu.edu> wrote in message news:Pine.GSO.4.44.0306251849140.22307-100000@slinky.cs.nyu.edu...
[snip]
>
> Umm, Elfyn, the semantics of "assert()" is that you assert some predicate
> to be true.  If the predicate is indeed true, the program continues
> normally.  If the predicate is false, the program fails.
> The predicate in this case is "ptr != MAP_FAILED".  Thus, the predicate
> was false when the assertion failed, and ptr == MAP_FAILED.
>
> To the OP: this means that mmap() did fail for some reason.  It should
> have set errno to indicate why.  You should check that instead of
> asserting -- mmap does fail occasionally.  Also FYI, once you assert, the
> following test for "ptr != MAP_FAILED" is redundant -- the program will
> not get there otherwise.
> Igor
[snip]


Here is updated function.
--------------------------------------
void read_file (char* filename_i)
{
int fd = open(filename_i, O_RDONLY);
  assert (fd > 2);

off_t sz = lseek(fd, 0, SEEK_END);
char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);

   errno = 0;
  if (ptr != MAP_FAILED)
  {
    string str(ptr, ptr+sz);
    munmap(ptr, sz);
  }
  else
  {
    assert (ptr == MAP_FAILED);
    printf ("=== Error : %u %s ===\n", errno, strerror (errno));
  }
  assert (ptr != MAP_FAILED);  // Here assertion failed

  close(fd);
}
--------------------------------------

The program prints:
=== Error : 0 No error ===

Regards,
   =====================================
   Alex Vinokur
     mailto:alexvn@connect.to
     http://mathforum.org/library/view/10978.html
   =====================================






--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-26  6:08         ` Alex Vinokur
@ 2003-06-26  8:31           ` Alex Vinokur
  2003-06-26  9:53             ` Brian Dessent
  2003-06-26  9:23           ` Brian Dessent
  1 sibling, 1 reply; 11+ messages in thread
From: Alex Vinokur @ 2003-06-26  8:31 UTC (permalink / raw)
  To: cygwin


"Alex Vinokur" <alexvn@connect.to> wrote in message news:bddr4g$on$1@main.gmane.org...
>
> "Igor Pechtchanski" <pechtcha@cs.nyu.edu> wrote in message news:Pine.GSO.4.44.0306251849140.22307-100000@slinky.cs.nyu.edu...
> [snip]
> >
> > Umm, Elfyn, the semantics of "assert()" is that you assert some predicate
> > to be true.  If the predicate is indeed true, the program continues
> > normally.  If the predicate is false, the program fails.
> > The predicate in this case is "ptr != MAP_FAILED".  Thus, the predicate
> > was false when the assertion failed, and ptr == MAP_FAILED.
> >
> > To the OP: this means that mmap() did fail for some reason.  It should
> > have set errno to indicate why.  You should check that instead of
> > asserting -- mmap does fail occasionally.  Also FYI, once you assert, the
> > following test for "ptr != MAP_FAILED" is redundant -- the program will
> > not get there otherwise.
> > Igor
> [snip]
>
>
> Here is updated function.
> --------------------------------------
> void read_file (char* filename_i)
> {
> int fd = open(filename_i, O_RDONLY);
>   assert (fd > 2);
>
> off_t sz = lseek(fd, 0, SEEK_END);
    errno = 0;
> char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);
>
--------------------------------
Must be removed
>    errno = 0;
--------------------------------
>   if (ptr != MAP_FAILED)
>   {
>     string str(ptr, ptr+sz);
>     munmap(ptr, sz);
>   }
>   else
>   {
>     assert (ptr == MAP_FAILED);
>     printf ("=== Error : %u %s ===\n", errno, strerror (errno));
>   }
>   assert (ptr != MAP_FAILED);  // Here assertion failed
>
>   close(fd);
> }
> --------------------------------------
>
> The program prints:
=== Error : 22 Invalid argument ===

Which argument?

[snip]

   ==========================================
   Alex Vinokur
     mailto:alexvn@connect.to
     http://www.simtel.net/pub/oth/19088.html
     http://sourceforge.net/users/alexvn
   ==========================================






--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-26  6:08         ` Alex Vinokur
  2003-06-26  8:31           ` Alex Vinokur
@ 2003-06-26  9:23           ` Brian Dessent
  1 sibling, 0 replies; 11+ messages in thread
From: Brian Dessent @ 2003-06-26  9:23 UTC (permalink / raw)
  To: cygwin

Alex Vinokur wrote:

> char* ptr = (char*)mmap(0, sz, PROT_READ, 0, fd, 0);
> 
>    errno = 0;
>   if (ptr != MAP_FAILED)
>   {
>     string str(ptr, ptr+sz);
>     munmap(ptr, sz);
>   }
>   else
>   {
>     assert (ptr == MAP_FAILED);
>     printf ("=== Error : %u %s ===\n", errno, strerror (errno));
>   }

> The program prints:
> === Error : 0 No error ===

Of course it does, since you set errno = 0, what else could you possibly
expect?  Remove that line if you want to know why mmap() fails.

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-26  8:31           ` Alex Vinokur
@ 2003-06-26  9:53             ` Brian Dessent
  2003-06-26 11:33               ` Alex Vinokur
  0 siblings, 1 reply; 11+ messages in thread
From: Brian Dessent @ 2003-06-26  9:53 UTC (permalink / raw)
  To: cygwin

Alex Vinokur wrote:

> > The program prints:
> === Error : 22 Invalid argument ===
> 
> Which argument?

The flags argument requires at least one of MAP_SHARED or MAP_PRIVATE, I
think.

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: assertion "ptr != MAP_FAILED" failed while using mmap
  2003-06-26  9:53             ` Brian Dessent
@ 2003-06-26 11:33               ` Alex Vinokur
  0 siblings, 0 replies; 11+ messages in thread
From: Alex Vinokur @ 2003-06-26 11:33 UTC (permalink / raw)
  To: cygwin


"Brian Dessent" <brian@dessent.net> wrote in message news:3EFA8DC0.441238CB@dessent.net...
> Alex Vinokur wrote:
>
> > > The program prints:
> > === Error : 22 Invalid argument ===
> >
> > Which argument?
>
> The flags argument requires at least one of MAP_SHARED or MAP_PRIVATE, I
> think.
>
> Brian
>

It works fine with MAP_SHARED.
Thanks.

   =====================================
   Alex Vinokur
     mailto:alexvn@connect.to
     http://mathforum.org/library/view/10978.html
   =====================================






--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Comparative performance : Reading contents from file into one string
  2003-06-25 15:17 assertion "ptr != MAP_FAILED" failed while using mmap Alex Vinokur
  2003-06-25 16:51 ` Elfyn McBratney
@ 2003-06-26 19:21 ` Alex Vinokur
  1 sibling, 0 replies; 11+ messages in thread
From: Alex Vinokur @ 2003-06-26 19:21 UTC (permalink / raw)
  To: cygwin


Here are some C/C++ Performance Tests performed with using C/C++ Program Perfometer.
http://sourceforge.net/projects/cpp-perfometer
http://alexvn.freeservers.com/s1/perfometer.html


Reading contents from file into one string
------------------------------------------

===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================


################ Comparative performance : Summary ################

  #==================================================================
  # Read contents from file into one string
  #------------------------------------------------------------------
  #   Resource Name       : user time used (via rusage)
  #   Resource Cost Unit  : milliseconds (unsigned long long)
  #   Resource State Unit : timeval
  #==================================================================
   : ----------------------------------------------------------------------
   : ReadFile1 : getline                    (size = 0)      ->           43
   : ReadFile1 : getline                    (size = 10)     ->           63
   : ReadFile1 : getline                    (size = 100)    ->           53
   : ReadFile1 : getline                    (size = 1000)   ->           86
   : ReadFile1 : getline                    (size = 10000)  ->          397
   : ReadFile1 : getline                    (size = 100000) ->         3892
   : ReadFile2 : vector, reading char       (size = 0)      ->           53
   : ReadFile2 : vector, reading char       (size = 10)     ->           53
   : ReadFile2 : vector, reading char       (size = 100)    ->           40
   : ReadFile2 : vector, reading char       (size = 1000)   ->           80
   : ReadFile2 : vector, reading char       (size = 10000)  ->          304
   : ReadFile2 : vector, reading char       (size = 100000) ->         3227
   : ReadFile3 : string, reading char       (size = 0)      ->           76
   : ReadFile3 : string, reading char       (size = 10)     ->           86
   : ReadFile3 : string, reading char       (size = 100)    ->           73
   : ReadFile3 : string, reading char       (size = 1000)   ->           80
   : ReadFile3 : string, reading char       (size = 10000)  ->          237
   : ReadFile3 : string, reading char       (size = 100000) ->         2046
   : ReadFile4 : vector, reading whole file (size = 0)      ->           60
   : ReadFile4 : vector, reading whole file (size = 10)     ->           87
   : ReadFile4 : vector, reading whole file (size = 100)    ->           86
   : ReadFile4 : vector, reading whole file (size = 1000)   ->          110
   : ReadFile4 : vector, reading whole file (size = 10000)  ->          170
   : ReadFile4 : vector, reading whole file (size = 100000) ->         1378
   : ReadFile5 : mmap                       (size = 0)      ->           23
   : ReadFile5 : mmap                       (size = 10)     ->           20
   : ReadFile5 : mmap                       (size = 100)    ->           40
   : ReadFile5 : mmap                       (size = 1000)   ->           23
   : ReadFile5 : mmap                       (size = 10000)  ->           33
   : ReadFile5 : mmap                       (size = 100000) ->           83
   : ReadFile6 : iterator                   (size = 0)      ->           56
   : ReadFile6 : iterator                   (size = 10)     ->           57
   : ReadFile6 : iterator                   (size = 100)    ->           40
   : ReadFile6 : iterator                   (size = 1000)   ->           73
   : ReadFile6 : iterator                   (size = 10000)  ->          393
   : ReadFile6 : iterator                   (size = 100000) ->         4713
   : ----------------------------------------------------------------------

###################################################################


################ C++ code : BEGIN ################


// ========================================
// ----------------------------------------
// Functions ReadFile1 - ReadFile6 are
//    lightly changed functions from the arctile :
// --------
// From: "Tom Hines" <tom_hines@yahoo.com>
// Subject: Re: How to efficiently read contents from file into one string?
// Newsgroup: comp.lang.c++.moderated
// Date: Thursday, January 30, 2003 3:55 PM
// ----------------------------------------


// ---------
static void ReadFile1 (const string& filename_i, unsigned long& str_size_o)
{
  // --- Using getline() ---

string line, str;
ifstream infile (filename_i.c_str());
  assert (infile.is_open());

  while (getline (infile, line))
  {
    str.append(line);
    line.erase();
  }

  str_size_o = str.size();
}


// ---------
static void ReadFile2(const string& filename_i, unsigned long& str_size_o)
{
  // --- Using vector char at a time ---

ifstream infile (filename_i.c_str());
  assert (infile.is_open());

vector<char> v;
char ch;

  while (infile.get(ch)) if (ch != '\n') v.push_back(ch);

string str (v.empty() ? string() : string (v.begin(), v.end()));

  str_size_o = str.size();
}

// ---------
static void ReadFile3(const string& filename_i, unsigned long& str_size_o)
{
  // --- Using string, char at a time ---

ifstream infile (filename_i.c_str(), ios::in | ios::ate);
  assert (infile.is_open());

streampos sz = infile.tellg();

 infile.seekg(0, ios::beg);

char ch;
string str(sz, '0');

int i = 0;
  for (i = 0; infile.get(ch); ) if (ch != '\n') str[i++] = ch;

  str.erase (i);

  str_size_o = str.size();
}

// ---------
static void ReadFile4(const string& filename_i, unsigned long& str_size_o)
{
  // --- Using vector, reading whole file at once ---

ifstream infile (filename_i.c_str(), ios::in | ios::ate);
  assert (infile.is_open());

streampos sz = infile.tellg();

  infile.seekg(0, ios::beg);

vector<char> v(sz);

  infile.read(&v[0], sz);
  // v.erase(remove(v.begin(), v.end(), '\n'), v.end());

string str (v.empty() ? string() : string (v.begin(), v.end()).c_str());

  str_size_o = str.size();
}

// ---------
static void ReadFile5(const string& filename_i, unsigned long& str_size_o)
{
  // --- Using mmap ---
int fd = open(filename_i.c_str(), O_RDONLY);
  assert (fd > 2);

off_t sz = lseek(fd, 0, SEEK_END);
char* ptr = (char*)mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);

  str_size_o = 0;

  if (ptr == MAP_FAILED)
  {
    close(fd);
    return;
  }

  assert (ptr != MAP_FAILED);

string str(ptr, ptr + sz);
  munmap(ptr, sz);

  // str.erase(remove(str.begin(), str.end(), '\n'), str.end());
  // str.erase(remove(str.begin(), str.end(), '\r'), str.end());

  close(fd);

  str_size_o = str.size();

}

// ---------
static void ReadFile6 (const string& filename_i, unsigned long& str_size_o)
{
  // --- Using iterator ---
ifstream infile (filename_i.c_str());
  assert (infile.is_open());

  infile >> noskipws;

istream_iterator<char> iter(infile), eos;
string str(iter, eos);

  // str.erase(remove(str.begin(), str.end(), '\n'), str.end());

  str_size_o = str.size();
}

################ C++ code : END ##################


   ==========================================
   Alex Vinokur
     mailto:alexvn@connect.to
     http://www.simtel.net/pub/oth/19088.html
     http://sourceforge.net/users/alexvn
   ==========================================






--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2003-06-26 16:32 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-25 15:17 assertion "ptr != MAP_FAILED" failed while using mmap Alex Vinokur
2003-06-25 16:51 ` Elfyn McBratney
2003-06-25 16:55   ` Ronald Landheer-Cieslak
2003-06-25 21:40     ` Elfyn McBratney
2003-06-26  5:06       ` Igor Pechtchanski
2003-06-26  6:08         ` Alex Vinokur
2003-06-26  8:31           ` Alex Vinokur
2003-06-26  9:53             ` Brian Dessent
2003-06-26 11:33               ` Alex Vinokur
2003-06-26  9:23           ` Brian Dessent
2003-06-26 19:21 ` Comparative performance : Reading contents from file into one string Alex Vinokur

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).