Skip to main content
GameDev.net gamedev.net
🔒 Locked

array in struct unintelligible problem

Started by granki Jul 31, 2007 at 1:48 AM 10 replies 1k views
Original Post
granki
granki
hello, and firstly sorry for about my english, here is some code wihch i can't understand debuging results; struct my_struct_type{ char my_array1[10]; char my_array2[10]; } my_struct_type my_struct[10] = {}; public: Form1(void) { InitializeComponent(); my_struct[0].myarray1[0] = 'a'; my_struct[0].myarray1[1] = 'a'; my_struct[0].myarray1[2] = 'a'; my_struct[0].myarray1[3] = 'a'; my_struct[0].myarray1[4] = 'a'; my_struct[0].myarray1[5] = 'a'; my_struct[0].myarray1[6] = 'a'; my_struct[0].myarray1[7] = 'a'; my_struct[0].myarray1[8] = 'a'; my_struct[0].myarray1[9] = 'a'; my_struct[0].myarray1[10] = 0; / my_struct[3].myarray1[0] = 'a'; my_struct[3].myarray1[1] = 'a'; my_struct[3].myarray1[2] = 'a'; my_struct[3].myarray1[3] = 'a'; my_struct[3].myarray1[4] = 'a'; my_struct[3].myarray1[5] = 'a'; my_struct[3].myarray1[6] = 'a'; my_struct[3].myarray1[7] = 'a'; my_struct[3].myarray1[8] = 'a'; my_struct[3].myarray1[9] = 'a'; my_struct[3].myarray1[10] = 0; my_struct[5].myarray1[-4] = 'a'; my_struct[5].myarray1[-3] = 'a'; my_struct[5].myarray1[-2] = 'a'; my_struct[5].myarray1[-1] = 0; // //TODO: Add the constructor code here // } protected: /// /// Clean up any resources being used. /// ~Form1() { if (components) { delete components; } } at debugging, when i read these values, i see my_struct[0].myarray1 isnt "aaaaaaaaaa". it has not related values ("mgnıonondoş" etc) and my_struct[3].myarray1 isnt "aaaaaaaaaa", its first 4 value is null, and last 6 value has 'a', and myarray2's first 4 value is "aaaa" even though there is no code about myarray2. and my_struct[5].myarray1 is "aaa" when i write to index (-4)of array compiler save this value to index 0 of array. what is the cause of this slip? i use visual studio2005 c++ compiler.
Julian90
Julian90
Quote:
my_struct[0].myarray1[10] = 0;...my_struct[3].myarray1[10] = 0;...my_struct[5].myarray1[-4] = 'a';my_struct[5].myarray1[-3] = 'a';my_struct[5].myarray1[-2] = 'a';my_struct[5].myarray1[-1] = 0;

and my_struct[5].myarray1 is "aaa" when i write to index (-4)of array compiler save this value to index 0 of array.


... I can say nothing ...

If this isn't a joke then you need to stop coding and review the concept of arrays, you declared the array to contain 10 values but are writing to the 11th and then blatantly index the array knowingly with a negative number, it doesn't get much more undefined...

(When posting lots of code put it between [source][/source])

Edit: the original sounded patronizing.
KulSeran
KulSeran
first off char array[10] means you have array[0] to array[9] there is no array[10] and there is no array[-1]
the negative indexes may or may not be "undefined" behaviour. The indexes above the array size are "buffer overruns"
and also provide undefined behaviour.

You really should refresh yourself on how to write arrays. You are doing it ALL wrong.

(but since "undefined" doesnt mean it can't be explained in some specific cases. you are writing my_array1[10] = 0; which
corosponds to the same memory as my_array2[0])

(my_struct[5].myarray1[-4] = 'a';
my_struct[5].myarray1[-3] = 'a';
my_struct[5].myarray1[-2] = 'a';
my_struct[5].myarray1[-1] = 0;
is just bad. and you are likely writing to my_struct[4].myarray2[6]...
but it is REALLY bad to be doing this.)
granki
granki
ok you are right , but i think i cant tell my problem, all of this illegal index's purpose is only test!. when my code is my_struct[3].myarray[0] ='a'; there is no change at my_struct[3].myarray[0] at debug, but the value of my_struct[3].myarray[4] is 'a' !!! also when my code is my_struct[3].myarray[-4] ='a'; , the value of my_struct[3].myarray[0] is 'a'!!!!! i think there is a compiler problem or i have a virus, my purpose of this question is i want to know is anybody have got same problem with me?
ToohrVyk
ToohrVyk
Your code, and your explanation, mix undefined behaviour (there's no reason, ex ante, for my_struct[3].myarray[-4] to do anything sane) with things you classify as bugs, and it's becoming quite hard to follow. Could you please provide a CME1 of this behaviour?

1 CME: Complete Minimal Example, the smallest possible bit of code which demonstrates the problem you have.
granki
granki
ok, here is the code

#pragma oncestruct my_struct_type{	char myarray1[10];	char myarray2[10];};my_struct_type my_struct[10];void example_function(void){	my_struct[0].myarray1[0] = 'a';	my_struct[0].myarray1[1] = 'a';	my_struct[0].myarray1[2] = 0;	my_struct[2].myarray1[0] = 'a';	my_struct[2].myarray1[1] = 'a';	my_struct[2].myarray1[2] = 0;	my_struct[4].myarray1[-4] = 'a';	my_struct[4].myarray1[-3] = 'a';	my_struct[4].myarray1[-2] = 0;}namespace forgamedev {	using namespace System;	using namespace System::ComponentModel;	using namespace System::Collections;	using namespace System::Windows::Forms;	using namespace System::Data;	using namespace System::Drawing;	/// <summary>	/// Summary for Form1	///	/// WARNING: If you change the name of this class, you will need to change the	///          'Resource File Name' property for the managed resource compiler tool	///          associated with all .resx files this class depends on.  Otherwise,	///          the designers will not be able to interact properly with localized	///          resources associated with this form.	/// </summary>	public ref class Form1 : public System::Windows::Forms::Form	{	public:		Form1(void)		{			InitializeComponent();						example_function();			//			//TODO: Add the constructor code here			//		}	protected:		/// <summary>		/// Clean up any resources being used.		/// </summary>		~Form1()		{			if (components)			{				delete components;			}		}	private:		/// <summary>		/// Required designer variable.		/// </summary>		System::ComponentModel::Container ^components;#pragma region Windows Form Designer generated code		/// <summary>		/// Required method for Designer support - do not modify		/// the contents of this method with the code editor.		/// </summary>		void InitializeComponent(void)		{			this->components = gcnew System::ComponentModel::Container();			this->Size = System::Drawing::Size(300,300);			this->Text = L"Form1";			this->Padding = System::Windows::Forms::Padding(0);			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;		}#pragma endregion	};}


end debug:

http://img71.imageshack.us/img71/7123/debugmz7.jpg
KulSeran
KulSeran
You are still using [negative] indexes!
This is the root of your problem. You would be getting sane results if you didn't use them!
Even if you were getting funny debug before, those negatives are what is making it hard to know where
the problem lies. Take them out, and see if you still have the issue.
bjle
bjle
This doesn't affect how the code actually runs, but it is a bug in the VS2005 .NET C++ debugger. Put something like
char* watch = my_struct[0].my_array1;
, which the debugger will display correctly.
Zahlman
Zahlman
Quote:
Original post by granki
ok you are right , but i think i cant tell my problem, all of this illegal index's purpose is only test!


You can't get meaningful results out of such a test; that's what we're trying to tell you. The answer you get back could legally be *anything*. What it *is*, pragmatically, depends on many things:

- the compiler
- the compiler settings (potentially all of them, but especially release vs. debug mode)
- the resulting optimizations (since the compiler only has to think about what happens when the indices are valid, it could change what happens when they aren't, without thinking about it)
- the surrounding code (because it could cause the compiler to optimize in a different way).
Sc4Freak
Sc4Freak
Quote:
Original post by bjle
This doesn't affect how the code actually runs, but it is a bug in the VS2005 .NET C++ debugger. Put something like
char* watch = my_struct[0].my_array1;
, which the debugger will display correctly.

This only happens in release mode. In Debug mode, the debugger has always worked for me.
KulSeran
KulSeran
Yeah. Part of that is that in release mode, you don't have any debug symbols. And any symbols you do have can no longer
coincide directly with the code, since optimizations can destroy the original structure of the program.

Generally bugs in release are due to memory trashing/non-initialization that doesn't show up in debug due to the way the CRT initializes memory for you.
There are other sources, but that is the most common.
granki
granki
thanks for explanations.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.