I get a writing access violation of 0x00000… when trying to assign a value into my struct

huangapple go评论61阅读模式
英文:

I get a writing access violation of 0x00000... when trying to assign a value into my struct

问题

这段代码中存在一些问题,主要是与结构体指针的初始化和使用相关的问题。下面是代码中的问题部分的中文翻译:

  1. 问题出现在以下代码行:
temp[wordcount]->WORD = cword;

这个错误是因为你没有为temp[wordcount]分配内存,所以它是一个野指针。在使用temp数组之前,你应该为它的每个元素分配内存。

  1. 另一个问题是在下面的代码行:
temp = new Word_Count_STRUCT*[wordcount + 1]();

你使用了( )来初始化,这会将所有元素初始化为nullptr,但你并没有为每个元素分配内存。你应该为每个元素分配内存,例如:

temp = new Word_Count_STRUCT*[wordcount + 1];
for (int i = 0; i < wordcount + 1; ++i) {
    temp[i] = new Word_Count_STRUCT;
}

这些问题可能导致访问冲突和内存泄漏。你需要仔细检查代码,确保为结构体指针分配和释放内存的正确性,以避免这些问题。希望这些信息对你有所帮助。

英文:

for part of a school lab I need to read in unique words and their corresponding count with a struct. I am new to structs so please bear with me. I am getting an access violation when I try to write the adress of the current word to the character pointer inside of the current instance of my struct. I have read that this is due to dereferencing a nullptr. I have tried to understand this, but I just don't get it. I have resized arrays just like this on regular char** arrays for accepting new words. I am at a loss, any help would be greatly appreciated. The input file used here is just random words separated by non letter characters but not - or , Here is my code:

#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;fstream&gt;
#include &lt;limits&gt;

using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::right;
using std::left;

using std::ifstream;
using std::ofstream;

const int BUFFER = 100; //I figure this buffer is big enough for any given word

struct Word_Count_STRUCT
{
	char* WORD = nullptr;
	int COUNT = 0;
};

int main()
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	//Input for phrase
	ifstream iphrase;

	//Output to CSV (word count)
	ofstream o_count;

	//Word Exceptions
	ifstream xinWord;


	char wordbuffer[BUFFER] = { &#39;
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;fstream&gt;
#include &lt;limits&gt;
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::right;
using std::left;
using std::ifstream;
using std::ofstream;
const int BUFFER = 100; //I figure this buffer is big enough for any given word
struct Word_Count_STRUCT
{
char* WORD = nullptr;
int COUNT = 0;
};
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//Input for phrase
ifstream iphrase;
//Output to CSV (word count)
ofstream o_count;
//Word Exceptions
ifstream xinWord;
char wordbuffer[BUFFER] = { &#39;\0&#39; };
char ch = 0;
Word_Count_STRUCT** uniquewords = nullptr;
Word_Count_STRUCT** temp = nullptr;
int k = 0;
int wordcount = 0;
char* cword = nullptr; //Current Word
bool NextWord_flag = false;
bool interwordpunct = false;
bool NewWord_flag = true;
iphrase.open(&quot;C:\\Users\\me\\Desktop\\henroE.txt&quot;);
if (iphrase.is_open())
{
while (!iphrase.eof())
{
iphrase.get(ch);
if (isalpha(ch) || ch == &#39;\&#39;&#39; || ch == &#39;-&#39;)
{
wordbuffer[k] = ch;
++k;
NextWord_flag = true;
if (ch == &#39;\&#39;&#39; || ch == &#39;-&#39;)
interwordpunct = true;
}
if ( (NextWord_flag == true) &amp;&amp; (!isalpha(ch)) &amp;&amp; (interwordpunct == false) )
{
k = 0;
cword = new char[strlen(wordbuffer) + 1];
strcpy(cword, wordbuffer);
memset(wordbuffer, &#39;\0&#39;, sizeof(wordbuffer));
for (int i = 0; (i &lt; wordcount) &amp;&amp; (NewWord_flag == true); ++i)
{
int cmp = _stricmp(uniquewords[i]-&gt;WORD, cword);
if (cmp == 0)
{
NewWord_flag = false;
uniquewords[i]-&gt;COUNT++;
delete[] cword;
}
}
if (NewWord_flag == true)
{
temp = new Word_Count_STRUCT * [wordcount + 1]();
for (int i = 0; i &lt; wordcount; ++i)
{
temp[i] = uniquewords[i];
}
delete[] uniquewords;
temp[wordcount]-&gt;WORD = cword;
temp[wordcount]-&gt;COUNT++;
uniquewords = temp;
++wordcount;
NextWord_flag = false;
}
interwordpunct = false;
NewWord_flag = true;
}
}
}
&#39; }; char ch = 0; Word_Count_STRUCT** uniquewords = nullptr; Word_Count_STRUCT** temp = nullptr; int k = 0; int wordcount = 0; char* cword = nullptr; //Current Word bool NextWord_flag = false; bool interwordpunct = false; bool NewWord_flag = true; iphrase.open(&quot;C:\\Users\\me\\Desktop\\henroE.txt&quot;); if (iphrase.is_open()) { while (!iphrase.eof()) { iphrase.get(ch); if (isalpha(ch) || ch == &#39;\&#39;&#39; || ch == &#39;-&#39;) { wordbuffer[k] = ch; ++k; NextWord_flag = true; if (ch == &#39;\&#39;&#39; || ch == &#39;-&#39;) interwordpunct = true; } if ( (NextWord_flag == true) &amp;&amp; (!isalpha(ch)) &amp;&amp; (interwordpunct == false) ) { k = 0; cword = new char[strlen(wordbuffer) + 1]; strcpy(cword, wordbuffer); memset(wordbuffer, &#39;
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;fstream&gt;
#include &lt;limits&gt;
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::right;
using std::left;
using std::ifstream;
using std::ofstream;
const int BUFFER = 100; //I figure this buffer is big enough for any given word
struct Word_Count_STRUCT
{
char* WORD = nullptr;
int COUNT = 0;
};
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//Input for phrase
ifstream iphrase;
//Output to CSV (word count)
ofstream o_count;
//Word Exceptions
ifstream xinWord;
char wordbuffer[BUFFER] = { &#39;\0&#39; };
char ch = 0;
Word_Count_STRUCT** uniquewords = nullptr;
Word_Count_STRUCT** temp = nullptr;
int k = 0;
int wordcount = 0;
char* cword = nullptr; //Current Word
bool NextWord_flag = false;
bool interwordpunct = false;
bool NewWord_flag = true;
iphrase.open(&quot;C:\\Users\\me\\Desktop\\henroE.txt&quot;);
if (iphrase.is_open())
{
while (!iphrase.eof())
{
iphrase.get(ch);
if (isalpha(ch) || ch == &#39;\&#39;&#39; || ch == &#39;-&#39;)
{
wordbuffer[k] = ch;
++k;
NextWord_flag = true;
if (ch == &#39;\&#39;&#39; || ch == &#39;-&#39;)
interwordpunct = true;
}
if ( (NextWord_flag == true) &amp;&amp; (!isalpha(ch)) &amp;&amp; (interwordpunct == false) )
{
k = 0;
cword = new char[strlen(wordbuffer) + 1];
strcpy(cword, wordbuffer);
memset(wordbuffer, &#39;\0&#39;, sizeof(wordbuffer));
for (int i = 0; (i &lt; wordcount) &amp;&amp; (NewWord_flag == true); ++i)
{
int cmp = _stricmp(uniquewords[i]-&gt;WORD, cword);
if (cmp == 0)
{
NewWord_flag = false;
uniquewords[i]-&gt;COUNT++;
delete[] cword;
}
}
if (NewWord_flag == true)
{
temp = new Word_Count_STRUCT * [wordcount + 1]();
for (int i = 0; i &lt; wordcount; ++i)
{
temp[i] = uniquewords[i];
}
delete[] uniquewords;
temp[wordcount]-&gt;WORD = cword;
temp[wordcount]-&gt;COUNT++;
uniquewords = temp;
++wordcount;
NextWord_flag = false;
}
interwordpunct = false;
NewWord_flag = true;
}
}
}
&#39;, sizeof(wordbuffer)); for (int i = 0; (i &lt; wordcount) &amp;&amp; (NewWord_flag == true); ++i) { int cmp = _stricmp(uniquewords[i]-&gt;WORD, cword); if (cmp == 0) { NewWord_flag = false; uniquewords[i]-&gt;COUNT++; delete[] cword; } } if (NewWord_flag == true) { temp = new Word_Count_STRUCT * [wordcount + 1](); for (int i = 0; i &lt; wordcount; ++i) { temp[i] = uniquewords[i]; } delete[] uniquewords; temp[wordcount]-&gt;WORD = cword; temp[wordcount]-&gt;COUNT++; uniquewords = temp; ++wordcount; NextWord_flag = false; } interwordpunct = false; NewWord_flag = true; } } }

I get an error on this line:

temp[wordcount]-&gt;WORD = cword;

I also get an error on the int value COUNT as well if I comment the line above it out. So I am guessing it is something with how I initialized the struct.
Worth noting that if I do not initialize this call:

temp = new Word_Count_STRUCT * [wordcount + 1]();

and instead just leave it as

temp = new Word_Count_STRUCT * [wordcount + 1];

I get another access violation but for reading instead of writing at 0xFFFFF...

At a loss, thank you for any help I get a writing access violation of 0x00000… when trying to assign a value into my struct

答案1

得分: 0

你犯了一些错误。首先,使用固定长度的字符缓冲而不是C++字符串已经过时大约20年了,除非你非常小心,否则会导致缓冲区溢出错误。

但这是一个问题:

temp = new Word_Count_STRUCT * [wordcount + 1]();
for (int i = 0; i &lt; wordcount; ++i)
{
    temp[i] = uniquewords[i];
}
delete[] uniquewords;

但你在哪里分配了uniquewords?你只是声明了它。

你还在循环外分配了cword,但在循环内删除了它,这似乎也很可疑。

但请注意,你分配的只是指针。我没有看到你实际分配用于存储数据的结构。

英文:

You've got a number of things wrong. First, using fixed-length character buffers instead of C++ strings is about 20 years out of date and WILL cause buffer overflow errors unless you are exceedingly careful.

But this is an issue:

                temp = new Word_Count_STRUCT * [wordcount + 1]();
                for (int i = 0; i &lt; wordcount; ++i)
                {
                    temp[i] = uniquewords[i];
                }
                delete[] uniquewords;

But where did you allocate uniquewords? You declared it.

You also allocate cword outside a loop but the delete it inside a loop -- which seems really fishy, too.

But note that all you've allocated are pointers. I don't see you actually allocating the structure you're trying to put data in.

huangapple
  • 本文由 发表于 2023年2月10日 07:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定