Mysterious Error: Can You See Why?

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Error
Click For Summary
The discussion revolves around a mysterious error encountered in a C++ program where two identical code blocks are used, but only one compiles successfully. The user initially copied the content from a working `main()` function to a non-working one, yet the latter produced a compile error due to a missing `int` before `main()`. Participants emphasized the importance of checking compiler error messages, as they can guide users to the source of the issue, which in this case was the incorrect function signature. Additionally, it was clarified that compile errors prevent the creation of an executable file, while link errors occur after an object file is generated. The conversation highlights the need for careful attention to detail in coding and understanding compiler feedback.
  • #61
yungman said:
I tried your suggestion, it still erase the old data in str every time a new line is read from file and write to str.
Of course it does. Each iteration of the loop resets str to a new value.
yungman said:
I don't see this can achieve what I want...to read the stream from file and store the complete content into either an array or vector.
I already said why this isn't a good idea for all but toy programs. If you run your program on a large file (several terabytes) the program will run out of memory.
 
  • Like
Likes pbuk and sysprog
Technology news on Phys.org
  • #62
Goodness gracious @Mark44 several terabytes? That seems to me to be like a really long movie. You could keep identification info on everyone on Earth in that much space. :wink: But yeah I recognize that nowadays some systems have petabytes and exabytes ##-## back in the day when I was barely a teenager (early '70s) we considered ourselves lucky that suddenly the IBM System 370 at the university had 12 megabytes of hand-threaded magnetic core memory ##\dots##
 
  • #63
sysprog said:
Goodness gracious @Mark44 several terabytes? That seems to me to be like a really long movie. You could keep identification info on everyone on Earth in that much space. :wink: But yeah I recognize that nowadays some systems have petabytes and exabytes − back in the day when I was barely a teenager (early '70s) we considered ourselves lucky that suddenly the IBM System 370 at the university had 12 megabytes of hand-threaded magnetic core memory …
I see that you can buy an 8TB Seagate SATA hard drive for $155 at the moment.

With regard to yungman's desire to keep all the data in memory, Windows imposes some limits on memory usage. (See https://software.intel.com/content/...dows. There are three kinds of memory limits:)
For 32-bit programs:
Static data - 2GB
Dynamic data - 2GB
Stack data - 1GB
But this doesn't mean you have 5GB to play with. The sum total of all three types has to be less than 2GB, and practically speaking it's about 1.75GB due to memory used by Windows.

For 64-bit programs, the limits are the same for static and stack data, but higher for dynamic data, but dependent on the particular version of Windows (i.e., Win 10 Enterprise, Win 10 Home, Win 8, Server editions, etc.).
 
  • Like
Likes sysprog
  • #64
sysprog said:
Goodness gracious @Mark44 several terabytes? That seems to me to be like a really long movie. You could keep identification info on everyone on Earth in that much space. :wink: But yeah I recognize that nowadays some systems have petabytes and exabytes ##-## back in the day when I was barely a teenager (early '70s) we considered ourselves lucky that suddenly the IBM System 370 at the university had 12 megabytes of hand-threaded magnetic core memory ##\dots##
Well if it is a movie file then treating it as a \n separated text file is not going to work anyway.

Which is kind of the point - the whole reason streams exist is so that you don't have to load a whole file into memory to process it.
 
  • Like
Likes sysprog
  • #65
Mark44 said:
Output statements can be helpful at times, but they are a very primitive way to debug a program.
Yes you are right - the debugging capabilities of a powerful IDE such as VS are much more useful than cout.
 
  • Like
Likes sysprog
  • #66
Mark44 said:
Of course it does. Each iteration of the loop resets str to a new value.
I already said why this isn't a good idea for all but toy programs. If you run your program on a large file (several terabytes) the program will run out of memory.
Not all files are that long, I just want to know how to do it. What if people want to look at bigger portion of the file to do modification, you cannot just read one line at a time. What you suggested is just slightly better than the original way the book use. Have to use common sense not to do it if it is the whole book! Most are just short files.

Like I said, I found two ways, the new way is even a little better than using vector, I just getline.(file, st1) to read the file into string st1; then str.append(st1) to assemble back the whole file. This will give the space between words, just cannot do new line.
 
Last edited:
  • #67
yungman said:
Like I said, I found two ways, the new way is even a little better than using vector, I just getline.(file, st1) to read the file into string st1; then str.append(st1) to assemble back the whole file. This will give the space between words, just cannot do new line.
This might work, but I didn't try it. Each time after you call str.append(st1), append a newline character to str.
 
  • Like
Likes sysprog and yungman
  • #68
Mark44 said:
This might work, but I didn't try it. Each time after you call str.append(st1), append a newline character to str.
Thanks, I just want to find a way to do it before I move on. I sure learn a lot digging back into strings.
 
  • #69
This is plain C rather than C++ ##-## it's an example of nifty memory conservation ##-## (I copied this example from a wikipedia article and added some comments):
C:
void XorSwap( int* x, int* y )
{
/* ensure that you're not swapping a location with itself 
(3 XORs would zero it out as surely as 1 XOR would) */
if (x != y)
  {
    *x ^= *y; /* xor x with y and store the result at x */
    *y ^= *x; /* xor y with x and store the result at y */
    *x ^= *y; /* xor x with y and store the result at x */
/* x and y have been swapped without a 3rd memory location */
  }
}
For @yungman, who is a hardware guy: this is analogous to using 3 XOR (or 12 NAND (or XNOR (or with inverters AND or OR))) gates to keep a crossover planar.

from https://www.chegg.com/homework-help/questions-and-answers/complete-crossover-gadget-figure-511-hence-reduction-circuit-sat-planar-circuit-sat-design-q28238847:

1600201594822.png


The point that I'm trying to make here is that trying to not treat memory as if it were inexhaustibly freely available is something that programmers have conscientiously and inventively borne in mind, wherefore, I think that it might be good for you to try to follow some of the examples, and not try to read an entire file into local memory, only so that you can then display its contents ##-## I think that this point may be worth a few think-time moments for you.
 
Last edited:
  • #70
sysprog said:
This is plain C rather than C++ ##-## it's an example of nifty memory conservation ##-## (I copied this example from a wikipedia article and added some comments):
C:
void XorSwap( int* x, int* y )
{
/* ensure that you're not swapping a location with itself
(3 XORs would zero it out as surely as 1 XOR would) */
if (x != y)
  {
    *x ^= *y; /* xor x with y and store the result at x */
    *y ^= *x; /* xor y with x and store the result at y */
    *x ^= *y; /* xor x with y and store the result at x */
/* x and y have been swapped without a 3rd memory location */
  }
}
For @yungman, who is a hardware guy: this is analogous to using 3 XOR (or 12 NAND (or XNOR (or with inverters AND or OR))) gates to keep a crossover planar.

from https://www.chegg.com/homework-help/questions-and-answers/complete-crossover-gadget-figure-511-hence-reduction-circuit-sat-planar-circuit-sat-design-q28238847:

View attachment 269463

The point that I'm trying to make here is that trying to not treat memory as if it were inexhaustibly freely available is something that programmers have conscientiously and inventively borne in mind, wherefore, I think that it might be good for you to try to follow some of the examples, and not try to read an entire file into local memory, only so that you can then display its contents ##-## I think that this point may be worth a few think-time moments for you.
My point is NOT about whether reading the whole file is good or not good. The point is I want to be able to read the whole file IF I want to, not that I have to. What bordered me was there are at least 4 sample programs in the book that "claim" reading from file and they are NOT. That bugs me to no end and I have to spend a day to find a way that can actually read the whole file. The book to me is misrepresentation. People can read it and thinking they actually read the whole file, but it's NOT. They just read a sentence and print out and erase it right away with another sentence.

I found the way and can read the delimiters also.
C++:
void showContents(fstream& file, string& str)
{
    string str1;
    while (!file.eof())
    {
        getline(file, str1);
        str.append(str1);
    }
    cout << "Now read back from str: \n\n" << str << "\n\n";
}

I know if I want to do search character of a string str using str.find(), I can pull in one sentence at a time, no need to pull the entire file. I just want to know I can do it.
 
  • #71
Reading the file iteratively (read a part, do something with it, then read the next part) is different from reading the whole file into memory before doing anything with any part of it. As @Mark44 pointed out, you can't do that with a very large file ##-## please look at the Hubbard C++ data structures book that you recently purchased ##-## it has many coding examples and explanations. I think that your reading of some it may help to further inform you for your interpretation of the Gaddis book code.
 
Last edited:

Similar threads

  • · Replies 65 ·
3
Replies
65
Views
7K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 57 ·
2
Replies
57
Views
4K
Replies
12
Views
2K
Replies
10
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 4 ·
Replies
4
Views
1K