0 / 0 / 0
Регистрация: 13.09.2013
Сообщений: 6
1

Попытка чтения или записи в защищенную память

13.09.2013, 15:10. Показов 770. Ответов 7
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Здравствуйте, ошибка выходит на последней строке данного кода, я думаю ошибка именно здесь:
C++
1
2
3
4
5
6
adj = new int* [n+1];
             for (i=0;i<n+1;i++)
                 adj[i] = new int [n+1];
             for (i=0;i<=n+1;i++)
                 for (j=0;j<=n+1;j++)
                     adj[i][j]=0;
массив создавал опираясь на материалы из интернета, так что не ручаюсь за работоспособность)
Ах да, всё это дело происходит в windows form applications
Form1.h выглядит так:
C++
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#pragma once
#include <iostream>
#include <vector>
 
namespace Kypc {
 
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace std;
 
    bool usd[100];
    
    int n,m,i,k,j,f,s,cnt;
    int **adj;
    void sled(int j) {
        k=j+1;
        while(k<n+1){
            if (!usd[k]){
                if (adj[j][k]==1){
                    usd[k]=true;
                    sled(k);}}
            k++;}
        k=j-1;
        while(k>0){
            if (!usd[k]){
                if (adj[j][k]==1){
                    usd[k]=true;
                    sled(k);}}
            k--;}
        for(k=1;k<=n;k++){
            if (adj[j][k]==1)
                if (k==1) break;
                else sled(k);}
    }
 
 
    /// <summary>
    /// Сводка для Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: добавьте код конструктора
            //
        }
 
    protected:
        /// <summary>
        /// Освободить все используемые ресурсы.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::TextBox^  textBox3;
    private: System::Windows::Forms::Label^  label4;
 
    private: System::Windows::Forms::Button^  button3;
    private: System::Windows::Forms::RichTextBox^  richTextBox1;
 
 
 
    private: System::Windows::Forms::Label^  label5;
    private: System::Windows::Forms::Button^  button4;
    private: System::Windows::Forms::TextBox^  textBox4;
 
    private:
        /// <summary>
        /// Требуется переменная конструктора.
        /// </summary>
        System::ComponentModel::Container ^components;
 
#pragma region Windows Form Designer generated code
        /// <summary>
        /// Обязательный метод для поддержки конструктора - не изменяйте
        /// содержимое данного метода при помощи редактора кода.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label3 = (gcnew System::Windows::Forms::Label());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->textBox3 = (gcnew System::Windows::Forms::TextBox());
            this->label4 = (gcnew System::Windows::Forms::Label());
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
            this->label5 = (gcnew System::Windows::Forms::Label());
            this->button4 = (gcnew System::Windows::Forms::Button());
            this->textBox4 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(122, 141);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(160, 47);
            this->button1->TabIndex = 0;
            this->button1->Text = L"Считать данные";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Visible = false;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(36, 141);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(79, 20);
            this->textBox1->TabIndex = 1;
            this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
            this->textBox1->Visible = false;
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(36, 168);
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(80, 20);
            this->textBox2->TabIndex = 2;
            this->textBox2->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
            this->textBox2->Visible = false;
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(12, 144);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(18, 13);
            this->label1->TabIndex = 3;
            this->label1->Text = L"от";
            this->label1->Visible = false;
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(11, 171);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(19, 13);
            this->label2->TabIndex = 4;
            this->label2->Text = L"до";
            this->label2->Visible = false;
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12));
            this->label3->Location = System::Drawing::Point(12, 109);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(188, 20);
            this->label3->TabIndex = 5;
            this->label3->Text = L"Ввод координат ребер:";
            this->label3->Visible = false;
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(122, 39);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(160, 23);
            this->button2->TabIndex = 6;
            this->button2->Text = L"Считать";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
            // 
            // textBox3
            // 
            this->textBox3->Location = System::Drawing::Point(16, 42);
            this->textBox3->Name = L"textBox3";
            this->textBox3->Size = System::Drawing::Size(50, 20);
            this->textBox3->TabIndex = 7;
            this->textBox3->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
            // 
            // label4
            // 
            this->label4->AutoSize = true;
            this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12));
            this->label4->Location = System::Drawing::Point(12, 9);
            this->label4->Name = L"label4";
            this->label4->Size = System::Drawing::Size(208, 20);
            this->label4->TabIndex = 8;
            this->label4->Text = L"Ввод количества вершин:";
            // 
            // button3
            // 
            this->button3->BackColor = System::Drawing::Color::Red;
            this->button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(204)));
            this->button3->ForeColor = System::Drawing::Color::Blue;
            this->button3->Location = System::Drawing::Point(16, 246);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(100, 53);
            this->button3->TabIndex = 10;
            this->button3->Text = L"EXIT";
            this->button3->UseVisualStyleBackColor = false;
            this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
            // 
            // richTextBox1
            // 
            this->richTextBox1->Location = System::Drawing::Point(306, 42);
            this->richTextBox1->Name = L"richTextBox1";
            this->richTextBox1->Size = System::Drawing::Size(259, 257);
            this->richTextBox1->TabIndex = 11;
            this->richTextBox1->Text = L"";
            // 
            // label5
            // 
            this->label5->AutoSize = true;
            this->label5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12));
            this->label5->Location = System::Drawing::Point(302, 9);
            this->label5->Name = L"label5";
            this->label5->Size = System::Drawing::Size(144, 20);
            this->label5->TabIndex = 15;
            this->label5->Text = L"Диалоговое окно:";
            // 
            // button4
            // 
            this->button4->BackColor = System::Drawing::Color::Lime;
            this->button4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(204)));
            this->button4->ForeColor = System::Drawing::Color::Blue;
            this->button4->Location = System::Drawing::Point(122, 246);
            this->button4->Name = L"button4";
            this->button4->Size = System::Drawing::Size(160, 53);
            this->button4->TabIndex = 16;
            this->button4->Text = L"SHOW";
            this->button4->UseVisualStyleBackColor = false;
            this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
            // 
            // textBox4
            // 
            this->textBox4->Location = System::Drawing::Point(72, 42);
            this->textBox4->Name = L"textBox4";
            this->textBox4->Size = System::Drawing::Size(45, 20);
            this->textBox4->TabIndex = 17;
            this->textBox4->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->BackColor = System::Drawing::Color::DarkOrange;
            this->ClientSize = System::Drawing::Size(577, 311);
            this->Controls->Add(this->textBox4);
            this->Controls->Add(this->button4);
            this->Controls->Add(this->label5);
            this->Controls->Add(this->richTextBox1);
            this->Controls->Add(this->button3);
            this->Controls->Add(this->label4);
            this->Controls->Add(this->textBox3);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
            this->PerformLayout();
 
        }
#pragma endregion
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
             n = Convert::ToInt32(textBox3->Text);
             m = Convert::ToInt32(textBox4->Text);
             adj = new int* [n+1];
             for (i=0;i<n+1;i++)
                 adj[i] = new int [n+1];
             for (i=0;i<=n+1;i++)
                 for (j=0;j<=n+1;j++)
                     adj[i][j]=0;
             label1->Visible = true;
             label2->Visible = true;
             label3->Visible = true;
             textBox1->Visible = true;
             textBox2->Visible = true;
             button1->Visible = true;
         
}
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
             for (i=0;i<n;i++) delete[] adj[i];
             delete[] adj;
             this->Close();
         }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                f = Convert::ToInt32(textBox1->Text);
                s = Convert::ToInt32(textBox2->Text);
                if(f==s) richTextBox1->Text += "Вершины совпадают, введите еще раз."+Environment::NewLine;
                else if((adj[f][s]==1)||(adj[f][s]==1)) richTextBox1->Text += "Это ребро уже есть, введите еще раз."+Environment::NewLine;
                     else {
                adj[f][s]=1;
                adj[s][f]=1;}
                textBox1->Text = "";
                textBox2->Text = "";
                richTextBox1->Text += System::Convert::ToString(f)+" "+System::Convert::ToString(s)+Environment::NewLine;
         }
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
             cnt = 0;
                 for (i=1; i<=n; i++) {
                    if (!usd[i]) {usd[i]=true;
                                  ++cnt;
                                  sled(i);}}
             richTextBox1->Text = "Ответ: ";
             richTextBox1->Text += System::Convert::ToString(cnt);
         }
};
}
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
13.09.2013, 15:10
Ответы с готовыми решениями:

Попытка чтения или записи в защищенную память
Добрый день! Вылетает на delete, возникла какая то проблема с памятью, только вот какая ? ...

Динамический массив: "Попытка чтения или записи в защищенную память"
Здравстувуйте. Пишу программу построения графиков. В целях оптимизации вынес функцию вывода...

Вылет программы: "Попытка чтения или записи в защищенную память"
Добрый день. при выполнении программы у меня часто вылетает с ошибкой: &quot;Попытка чтения или записи...

System.AccessViolationException: Попытка чтения или записи в защищенную память - попытка 2
В предыдущей теме некто вообразил, что код зацикливается, поэтому приходится выкладывать более...

7
5498 / 4893 / 831
Регистрация: 04.06.2011
Сообщений: 13,587
13.09.2013, 15:18 2
C++
1
2
3
4
5
6
adj = new int*[n+1];
for (i = 0; i < n + 1; i++)
    adj[i] = new int[n + 1];
for (i = 0; i < n + 1; i++)
    for (j = 0; j < n+1; j++)
        adj[i][j] = 0;
1
0 / 0 / 0
Регистрация: 13.09.2013
Сообщений: 6
13.09.2013, 15:23  [ТС] 3
Цитата Сообщение от alsav22 Посмотреть сообщение
C++
1
2
3
4
5
6
adj = new int*[n+1];
for (i = 0; i < n + 1; i++)
    adj[i] = new int[n + 1];
for (i = 0; i < n + 1; i++)
    for (j = 0; j < n+1; j++)
        adj[i][j] = 0;
О_О но как? добавили пробелов, убрали равно и всё?
0
5498 / 4893 / 831
Регистрация: 04.06.2011
Сообщений: 13,587
13.09.2013, 15:25 4
Пробелы - для удобочитаемости кода. Последний допустимый индекс массива должен быть на единицу меньше размера массива, а у вас получался равен размеру. Это выход за границу массива.
0
0 / 0 / 0
Регистрация: 13.09.2013
Сообщений: 6
13.09.2013, 15:29  [ТС] 5
Ааа, вспомнил, там вроде находится нулевой элемент, спасибо выручили
0
5498 / 4893 / 831
Регистрация: 04.06.2011
Сообщений: 13,587
13.09.2013, 15:31 6
Цитата Сообщение от Proska Посмотреть сообщение
там вроде находится нулевой элемент
Там - это где?
0
0 / 0 / 0
Регистрация: 13.09.2013
Сообщений: 6
13.09.2013, 15:36  [ТС] 7
в последнем элементе..хотя нет, это же в строках..тогда я опять ошибся)
0
5498 / 4893 / 831
Регистрация: 04.06.2011
Сообщений: 13,587
13.09.2013, 15:47 8
Просто запомните, что индексация массива начинается с 0, значит последний индекс будет на 1 меньше размера массива:
C++
1
2
3
4
5
int arr[10]; // размер массива 10. 
            // Первый элемент расположен по индексу 0 (arr[0]), 
            // последний по индексу 9 (arr[9])
arr[0] = 3;
arr[9] = 5;
1
13.09.2013, 15:47
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
13.09.2013, 15:47
Помогаю со студенческими работами здесь

System.AccessViolationException: Попытка чтения или записи в защищенную память - попытка 3
Уже простите за некомпилируемый код, просто я переустановил Windows, и Visual Studio удалилась,...

Попытка чтения или записи в защищенную память
Попытка чтения или записи в защищенную память. Это часто свидетельствует о том, что другая память...

Попытка чтения или записи в защищенную память
Здравствуйте. Толи c# тупит толи что но возникает ошибка Попытка чтения или записи в защещенную...

Попытка чтения или записи в защищенную память
Дело в том что у меня Windows 8, при попытке запуска проекта выдает ошибку: &quot;Необработанное...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
8
Ответ Создать тему
Опции темы

КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru