someone can see this and someone can't see the magic on STL vector

hi all, first of all I am very new to the C++ programming and the STL
programming. But I did this exercise on C++ STL vectors.

This is the exercise that I have did .
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

#include <iostream.h>
#include <vector.h>


struct vector_element
{
       int a ;
       int b ;
       vector_element( int aa , int bb )
       {
                       a = aa ;
                       b = bb;
        }
};

    // push create a empty vector
    vector<vector_element>* vector_1 = new vector<vector_element> ;
    

int main(int argc, char *argv[])
{
    

    // let's fill it up
    for ( int i = 0 ; i < 100 ; i++ )
    {
        // create a new vector element and initialize it
        vector_element *prt_vector_element = new vector_element ( i , i+1);  
        // Then we have to just push it
        vector_1->push_back ( *prt_vector_element);
    }
    
    // Just getting an iterator to the Vector
    vector<vector_element>::iterator iterator_;
       
    // now we are getting the elements out from the vector
    for ( iterator_ = vector_1->begin() ;iterator_ != vector_1->end() ; iterator_++)
         cout << ( * iterator_ ).a << (*iterator_).b << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}



and the magic is this code is not working .... and get an access violation exception under the windows .....

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <windows.h>
using namespace std ;
/* these is line struct */
struct line
{
      
       int length ;
       char *  str1  ;
       
       line ( char str[] , int length )
       {
            // create the new arreay
            str = new char [length+1] ;
            
            // then copy it
            for ( int i = 0 ; i < length ; i++ )
                str1[i] = str [i];
            str [length ] = 0;
            this->length = length ;
       }
};

/* this is the lines structure then */
struct  lines 
{
       vector<line> * lines_vector ;
       int a ;
       // the constructor will create get a char* and torkernize it into the 
       //   lines and make line objects and push them into the vector.
 
       char * getline (int line ) 
       {
            // this will output line number 'line'
            //line & requested_line ;
            //requested_line =  lines_vector[line];
            return (lines_vector->operator[] (line) ).str1  ;
       }
       
       int get_num_lines () 
       {
           return lines_vector->size();
       }
 
       lines ( char * str , int length ) 
       {
            // thus we are using the lines_vector thus initialize it
            lines_vector = new vector<line>;
                     
             //lines_vector = new vector<line>;
             int current_position = 0 ;
             int current_line_position = 0 ;
             char *current_line = new char[200] ; // we are assuming that the current line is not
             // greater than the 200 characters... however this is NUTS
             for ( ; current_position < length ; current_position++)
             {
                 // if we current char '\n' then
                 if ( str[current_position] == '\n' )
                 {
                         current_line [current_line_position] = 0;
                         line * new_line = new line ( current_line , current_line_position + 1  );
                         cout << "current line is " << current_line << "current length is : " << strlen ( current_line)  << endl;
                         lines_vector->push_back (  *new_line );       
                         cout << "debug -- line 2" << endl ;   
                         current_line_position = 0  ;          
                 }else{
                         current_line [ current_line_position  ] = str [current_position];
                         current_line_position++ ;
                 }
      
             }      
            
       }
};

/* This is the Model of our window */
struct Model 
{
       int first_print_line   ;
       TEXTMETRIC tm ;
       int cyCaps   ;
       
       // this will holds the number of lines on the screen
       int num_lines  ;
       int num_lines_in_file  ;
       // the current y size of the screen 
       int window_height  ;
       int window_width  ;
       
       // this is the lines 
       lines * lines_object ;
       
       char * getline ( int line )
       {
            return  lines_object->getline(line);
       }
       
       Model (HWND hwnd ) 
       {
             RECT rect ; 
             
             // Get Device Context
             HDC hdc = GetDC ( hwnd ) ;
             //GetTextMetrics ( hdc , &tm );
             GetClientRect ( hwnd , &rect ) ;
             window_height = rect.bottom - rect.top ;
             window_width = rect.right - rect.left ;
             // then calculate the cyCaps from it;
             cyCaps = tm.tmHeight + tm.tmExternalLeading ;           
             // then we can know how many lines that we can actually 
             // print 
             num_lines = window_height / cyCaps ;
             ReleaseDC ( hwnd , hdc ) ; 
             /* This function just open the file and fill the lines 
               Structure 
             */
             first_print_line = 0 ;
             char*  str  = new char [2001];
             // just open the file and read it to the str array 
             ifstream infile ;
             infile.open( "reading.txt" ) ;
             
             /*
             MessageBox(NULL , "File open" , infile.is_open() ? "File has been Opened" : "file is not open " , MB_OK );
             //infile>>.get ( str , 2000) ;
             infile.read ( str , 2000);
           
             // debug ---
             char * str11  = new char [200];
             for ( int i = 0 ; i < 100 ; i++ )
                 str11[i] = str[i];
             str11[99] ='\n'; 
             cout << str11 << endl;  
             // debug --
             */
             
             
             lines_object = new lines ( str , 2001 ) ;
             num_lines_in_file = lines_object->get_num_lines();
       }      
     
       // THE WM_SIZE mesage should call this function.
       void WM_PAINT_callback ( HWND hwnd )
       {
             RECT rect ;
             GetClientRect ( hwnd , &rect ) ;
             window_height = rect.bottom - rect.top ;
             window_width = rect.right - rect.left ; 
                          // then we can know how many lines that we can actually 
             // print 
             num_lines = window_height / cyCaps ;
       }
} * window_model ;


int main()
{
   // create a model 
   char * myString = "This is the case\nThis is you\nand you gonna to be a \ncool\nthis is it\nit is that\n";
   lines*  new_lines = new lines ( myString , strlen ( myString));
   
   cout <<( new_lines->getline ( 3));
   system("PAUSE");
   return 0;
}


And the funny thing is for the second program the DEV C++ IDE debugger is not working for the code. I am not a pro in C++ programming , so I'm not telling that this is a bug in the C++ IDE.

In the second program these are the lines that getting the exception ....Even
the debugger is not working .. so then I try the source code cout tags to debug
and watch variables....
1
2
3
4
5
6
                        current_line [current_line_position] = 0;
                         line * new_line = new line ( current_line , current_line_position + 1  );
                         cout << "current line is " << current_line << "current length is : " << strlen ( current_line)  << endl;
                         lines_vector->push_back (  *new_line );       
                         cout << "debug -- line 2" << endl ;   
                         current_line_position = 0  ;          


actually telling the line is
lines_vector->push_back( *new_line);
But the problem is same thing is wroking nice on first,
The problem in your first code is that you can't write the code

1
2
// push create a empty vector
vector<vector_element>* vector_1 = new vector<vector_element> ;


outside of any function.
Why don't you just declare the vector as

 
vector<vector_element> vector_1;


and in your loop just create vector_elements on the stack

1
2
3
vector_element ve;

vector_1.push_back(ve);


since the contents of your struct doesn't have any ptrs etc.
Topic archived. No new replies allowed.