and the relevancy of this post to the thread is....... :thumbd:
Printable View
Thanks for all the feedback. Sorry if I wasn't clearer in post #9 of this thread. I was able to get a function working that seemed to solve the stack overflow issue.
The issue with that function, however, was not being able to use a variable for the size of the buffer. I needed to use the integer of the largest buffer. Below are the bare-bones examples...
Code://Doesn't compile.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
void extract(int length){
char buffer[length];
}
int main(){
extract (150000);
return 0;
}
I also tried the other suggested buffer method below and it worked. It made the full program run about 10 seconds faster.Code://Compiles.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
void extract(int length){
char buffer[150000];
}
int main(){
extract (150000);
return 0;
}
Before my original post, I tried this method. However, I must've had a syntax error elsewhere that caused the program not to compile. This method, from what I read elsewhere, is promoted for reentrancy. I'm not sure if that's an issue in this case.Code://Compiles.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
void extract(size_t length){
char* buffer = new char[length];
delete[] buffer;
}
int main(){
extract (150000);
return 0;
}
I also tried using std:string to create a buffer, but couldn't get the syntax right.
The instances where the code wouldn't compile made me wonder if I was using incorrect syntax or, since I'm using Visual C++ 2008, newer syntax. But if char buffer[length] and std::strings (and any other buffers) should work with 2008, I'm still interested in trying other buffer methods that may improve performance or are better for "best practices."
Thanks again for any insights on this.
Why do you care about the size of the buffer. Just make sure it's big enough for the largest file, or read the file in chunks.
you cannot create a global or local scope buffer with a size that is variable like this.
there are non-portable ways to allocate a variable amount of memory on the stack (such as _alloca() and _malloca on VC), but this may not be available on your compiler.
The solution is as you already found out in the next attempt. to allocate the memory dynamically. from the free store using new/delete is the Obvious choice, but there are others. (usually non portable).
an even better way, and one that "looks" as though it magically does what you were trying is to use the C++ container class std::vector.
the nice part is, it does the new/delete for you, and it automatically cleans up after itself even if you get thrown out of the function with an exception (this is known as RAII and is an important concept in proper C++ development).Code:void extract(int length){
std::vector<char> buffer(length);
// use buffer.data() to get a pointer to the first element in that buffer.
}