VS 2019 C6385, array, fstream

Hello World!

I´ve got this warning (C6385, Reading of invalid data from "sammlung": the readable size is "(unsigned int) * 48 + 4" bytes, but "96" bytes can be read.) in my program. Looked in docs but don't understand whats the problem. I've copyed the code in a .cpp file and run it with MinGW, no error showed and the code works fine.


This loop safes new data in array "sammlung"

1
2
3
4
5
	for (int i = (groesse - 1); i >= 0; i--)
	{
		sammlung[i + 1] = neuAbspeichern;
		i = -1;
	}


This loop take data from array and writes it down into a file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	f.open("vokabeln.txt", ios::out);

	for (int i = 0; i <= groesse; i++)
	{
		if (firstTry == false)
		{
			f << endl;          
		}
		else
		{
			firstTry = false;
		}

		f << sammlung[i].spanisch;   // here comes the warning
		f << endl;
		f << sammlung[i].deutsch;
		f << endl;
	}
	f.close();
	delete[] sammlung;


this is my first post, if important excerpts are missing, i will add them immediately.

Have a nice day!
Last edited on
 
for (int i = 0; i <= groesse; i++)


Should this be:

 
for (int i = 0; i < groesse; i++)


Also, it's better to use '\n rather than endl as endl does a flush every time - which isn't needed as the file is flushed when closed.

You don't need firstTry for this code. You can just use i:

1
2
3
4
5
6
7
8
for (int i = 0; i < groesse; ++i)
{
    if (i > 0)
        f << '\n';

    f << sammlung[i].spanisch << '\n';   // here comes the warning
    f << sammlung[i].deutsch << '\n';
}

Last edited on
 
for (int i = 0; i < groesse; i++)

I've tried this solution earlier, still the same warning. But what does this warning mean exactly? That the array could reach a size that fstream cannot transmit the array?


You don't need firstTry for this code. You can just use i:

First try prevents an empty line from being inserted before the first data record.
For what this warning means see this:

https://docs.microsoft.com/en-us/cpp/code-quality/c6385?view=msvc-160

But it desn't make sense within that context. sammlung seems to be dynamic array because of line 20. So how is groesse and sammlung defined?
First try prevents an empty line from being inserted before the first data record.


I know. But you can use the value of i instead as per my previous code.
Hello xCriminaL,

To start with it is always a good idea to provide complete code that can be compiled and run. This does not mean the entire program, but at least enough to show the problem and what leads up to it.

What you need to look at first is: for (int i = 0; i <= groesse; i++). Now I can only guess that "groesse" has a value of 20, but this may not be the case. The problem comes from using (<=). This may be useful in an if statement or while condition, but should be used sparingly in a for loop and not the norm.

Looking at the for loop a little differently:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
f.open("vokabeln.txt", ios::out);
// <--- How do you know it is open and usable? And how is "f" defined?

for (int idx = 0; idx <= groesse; idx++)
{
    if (firstTry == false)
    {
        f << '\n';
    }
    else
    {
        firstTry = false;
    }

    f << sammlung[idx].spanisch;   // here comes the warning
    f << '\n';
    f << sammlung[idx].deutsch;
    f << '\n';
}

f.close();

"i", "j" and "k" may be acceptable names for loop iterators, but sometimes a good name is better. When you look at lines 15 and 17 it makes more sense.

Since the array is indexed from (0)zero to 19 for a 20 element array starting at "idx" at (0) and going to (<=) means that it is doing 1 extra loop than it needs to, so when "idx" becomes 20 this is beyond the end of the array thus causing the error.

When using a for loop try to set it up to use the (<) only. You will save your-self many headaches.

After that as seeplus shows you can use the insertion operator(<<) to chain several different things into 1 "cout" statement. There is no need for a separate "cout" statement for every little bit. And tha same is true for writing to a file.

Depending on how you code is written, as a whole, the: f.close(); may not be needed as the dtor of the class will close the file when the function looses scope.

Andy
Topic archived. No new replies allowed.