CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,561

    Re: Crash Reading & Writing More Files

    Quote Originally Posted by camycent.solutions View Post
    I think it is a compiler problem.If your compiler is ok then you can try this code

    // cin with strings
    #include <iostream>
    #include <string>
    using namespace std;

    int main ()
    {
    string mystr;
    cout << "What's your name? ";
    getline (cin, mystr);
    cout << "Hello " << mystr << ".\n";
    cout << "What is your favorite team? ";
    getline (cin, mystr);
    cout << "I like " << mystr << " too!\n";
    return 0;
    }
    and the relevancy of this post to the thread is.......
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.1)

  2. #17
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,624

    Re: Crash Reading & Writing More Files

    Quote Originally Posted by camycent.solutions View Post
    I think it is a compiler problem.If your compiler is ok then you can try this code
    It's almost never a compiler problem. In this case, it's clearly a stack space problem.

  3. #18
    Join Date
    Jun 2011
    Posts
    17

    Re: Crash Reading & Writing More Files

    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;
    }
    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;
    }
    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(size_t length){
    char* buffer = new char[length];
    delete[] buffer;
    }
    
    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.

    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.

  4. #19
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,624

    Re: Crash Reading & Writing More Files

    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.

  5. #20
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Crash Reading & Writing More Files

    Quote Originally Posted by Mr. Smith View Post
    Code:
    //Doesn't compile.
    
    void extract(int length){
    char buffer[length];
    }
    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.
    Code:
    void extract(int length){
    std::vector<char> buffer(length);
    
    // use buffer.data() to get a pointer to the first element in that buffer.
    }
    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).

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured