473,413 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

memory problem

size = myTextFile.tellg();
myTextFile.seekg (0, ios::beg);
char buffer[size+1];
myTextFile.read (buffer, size);
myTextFile.close();
buffer[size] = '\0';

the above code works great and always worked great.
now, i tried to apply it to a 27MB text file instead of the 300K one
it was used to doing and i get a fault exception. i am 99% positive
its the size of the file. which makes sense. but my entire program
depends of the buffer containing the entire text for searching and
extraction and parsing purposes.
is there a way around this where i could search direct from the drive
instead of a buffer?
Jul 22 '05 #1
2 999
* Sean Bartholomew:
size = myTextFile.tellg();
myTextFile.seekg (0, ios::beg);
char buffer[size+1];
Note that this uses a _non-standard_ extension from C99. C++ does
not have dynamic size arrays. In C++ just use a std::vector<char>.

If you're using g++ it's possible that explicitly specifying c++
and "pedantic" mode will help you catching those things, or there
might be other compiler options.

myTextFile.read (buffer, size);
myTextFile.close();
buffer[size] = '\0';

the above code works great and always worked great.
What's the declaration of 'size'?

Not that it should cause the described misbehavior, but also, do
you seek to the end before 'tellg', i.e. previous to shown code?

now, i tried to apply it to a 27MB text file instead of the 300K one
it was used to doing and i get a fault exception.


The most probable cause is that you're trying to allocate 27 MiB on
the stack. When you use std::vector, as you should, those 27 MiB will
instead be allocated on the heap, which has much more room. Also, if
allocation fails you'll then get a std::bad_alloc exception (I think).

Less likely: if the stream is in an error state then 'size' ends up as
-1, which could then be reinterpreted as a huge unsigned value. And if
by any chance 'size' is a 'short' it could end up negative anyway. For
that matter, unlikely is it is it _could_ happen with 'size' as 'int',
on an old compiler -- but (1) stack -> heap, and (2) check for error.

Unfortunately the standard streams do not really support using exceptions
to report errors (if you turn it on you get an exception when _encountering_
end-of-file), but you could make a stream wrapper that translates failures
to exceptions, and then use that wrapper instead of a "raw" stream.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

Just a guess, but perhaps your stack doesn't allow you to have
27 megs of space ? Perhaps you can allocate buffer with the
free store instead ( like buffer = new char[27meg] )

"Sean Bartholomew" <ad****@rivaband.com> wrote in message
news:b8**************************@posting.google.c om...
size = myTextFile.tellg();
myTextFile.seekg (0, ios::beg);
char buffer[size+1];
myTextFile.read (buffer, size);
myTextFile.close();
buffer[size] = '\0';

the above code works great and always worked great.
now, i tried to apply it to a 27MB text file instead of the 300K one
it was used to doing and i get a fault exception. i am 99% positive
its the size of the file. which makes sense. but my entire program
depends of the buffer containing the entire text for searching and
extraction and parsing purposes.
is there a way around this where i could search direct from the drive
instead of a buffer?

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Amadeus | last post by:
Hello Everybody! I have a problem with MySQL servers running RedHat 9 (smp kernel 2.4.20) on Intel and MySQL server 4.0.14 (problem also appears on binary distr 4.0.15 and on 4.0.15 I bilt myself...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
16
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. ...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
9
by: Bruno Barberi Gnecco | last post by:
I'm using PHP to run a CLI application. It's a script run by cron that parses some HTML files (with DOM XML), and I ended up using PHP to integrate with the rest of the code that already runs the...
9
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It...
17
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.