Problem with assure(out, "macro.out");

I am using C++ Builder to study C++. When I apply the code listed below, sometimes the macro is created with results. Other times the macro file is empty and the error message reads "access violation at 0x7c90eddc: write of address 0x000e0f1c' Process stopped. Use Step or Run to continue". I can see the referemce on the CPU window but I have no ide what to do. STEP or RUN do not work and I have to press CONT ALT DEL to close C++ Builder. I have tested and verified that the problem is caused by line 8.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "C:\general\require.h"
#include <fstream>
using namespace std;
#define BAND(x) (((x)>5 && (x)<10) ? (x) : 0)

int main() {
ofstream out("macro.out");
assure(out, "macro.out");
for(int i = 4; i < 11; i++) {
int a = i;
out << "a = " << a << endl << '\t';
out << "BAND(++a)=" << BAND(++a) << endl;
out << "\t a = " << a << endl;
}
} ///:~ 
What does 'assure' do?
The code for assure is in require.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//: :require.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Test for error conditions in programs
// Local "using namespace std" for old compilers
#ifndef REQUIRE_H
#define REQUIRE_H
#include <cstdio>
#include <cstdlib>
#include <fstream>

inline void require(bool requirement, 
  const char* msg = "Requirement failed") {
  using namespace std;
  if (!requirement) {
    fputs(msg, stderr);
    fputs("\n", stderr);
    exit(1);
  }
}

inline void requireArgs(int argc, int args, 
  const char* msg = "Must use %d arguments") {
  using namespace std;
   if (argc != args + 1) {
     fprintf(stderr, msg, args);
     fputs("\n", stderr);
     exit(1);
   }
}

inline void requireMinArgs(int argc, int minArgs,
  const char* msg = 
    "Must use at least %d arguments") {
  using namespace std;
  if(argc < minArgs + 1) {
    fprintf(stderr, msg, minArgs);
    fputs("\n", stderr);
    exit(1);
  }
}
  
inline void assure(std::ifstream& in, 
  const char* filename = "") {
  using namespace std;
  if(!in) {
    fprintf(stderr,
      "Could not open file %s\n", filename);
    exit(1);
  }
}

inline void assure(std::ofstream& in, 
  const char* filename = "") {
  using namespace std;
  if(!in) {
    fprintf(stderr,
      "Could not open file %s\n", filename);
    exit(1);
  }
}
#endif // REQUIRE_H ///:~ 
Just in case some one is trying to help, the program runs after adding line 3:
#include <queue>
Now I have to understand the problem and the solution.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "C:\general\require.h"
#include <fstream>
#include <queue>
using namespace std;
#define BAND(x) (((x)>5 && (x)<10) ? (x) : 0)

int main() {
ofstream out("macro.out");
assure(out, "macro.out");
for(int i = 4; i < 11; i++) {
int a = i;
out << "a = " << a << endl << '\t';
out << "BAND(++a)=" << BAND(++a) << endl;
out << "\t a = " << a << endl;
}

} ///:~
I can't see what on earth queue has got to do with this at all.

(I'm going to have a look at that book)
When the program fails the error message is:

"access violation at 0x7c90eddc: write of address 0x000e0f1c' Process stopped. Use Step or Run to continue"

I checked the reference in the CPU window --------- 7c90eddc poits to line "push ebx"

I fund code that sugests that to use function "push", <queue> has to be included.

What I cannot workout is at what point in my code "push" is called.
"push" is assembly, so it's not in "your" code as such.
It's compiler generated code to push the value of %ebx (which is a register; tiny (2 byte) piece of storage space on the CPU (on the die itself, so it's faster to access even than Ln Cache) http://en.wikipedia.org/wiki/Processor_register ) onto a stack of some sort.

Example:
1
2
push %eax # Push eax onto the stack
pop %eax # Remove eax from the stack 
Last edited on
I have compiled the unaltered code outside Borland Builder without problem.
Topic archived. No new replies allowed.