473,545 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array corruption

Hi:

This prob "magically" stopped and I'm not sure if I can recreate it,
but thought I'd share it and maybe learn something.

#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
#include <fstream.h//iostream.h

// yes, I'll fix those includes, too

class Yi
{
public:
Yi();

int Number(char bits){return _myNumber[bits];
}

private:
int _myNumber[64];
int _myReverse[64];
//int place1; // prevents array element corruption.
char _myShortName[64][32];
char _myName[64][64];
};

#include <iostream.h>
#include <string.h>
#include "yi.structure.h "

// ditto with those includes

Yi::Yi()
{
int num[64] = {2,23,8,20,16,3 5,45,12,
15,52,39,53,62, 56,31,33,
7,4,29,59,40,64 ,47,6,
46,18,48,57,32, 50,28,44,
24,27,3,42,51,2 1,17,25,
36,22,63,37,55, 30,49,13,
19,41,60,61,54, 38,58,10,
11,26,5,9,34,14 ,43,1};

char short_name[64][32] = {"Creative","Re ceptive",

// blah blah

char name[64][64] = {"The Creative","The Receptive",
// more blah

for(int i=0;i<64;i++)
{
_myNumber[i] = num[i];
_myReverse[ num[i] ] = i;
strcpy(_myName[i],name[i]);
strcpy(_myShort Name[i],short_name[i]);
}
}

See that odd comment

//int place1; // prevents array element corruption.

?

That's what I had to do to make it work. What would happen was I'd
instanciate a Yi and look up the number of a symbol using

int Number(char bits){return _myNumber[bits];

based on 6 bits (char bits, would you guess?) which represent the
symbol.

Number() always returned 21 - saw it in the debugger. I get lost when
the debugger steps into the library code and couldn't figure out
exactly where the problem was, just that the return value of Number()
was always 21.

//int place1; // prevents array element corruption.

was the fix except at the time int _myReverse[64]; wasn't needed and
I wouldnt be surprised if int _myReverse[64]; is what's keeping things
working today.

Structure member alignment has always been set at 8 bytes FWIW

I thought the array was corrupted because of the fix, but am not sure
if I checked the whole array at the time. maybe it wassomething else.

Mike

Aug 20 '07 #1
6 1815
I just wanted to ask if anyone had any ideas what was going on and to
say

Thanks!

Mike

Sorry. Even lame yahoo lets one delete their own post and start from
scratch. Maybe I just don't see a delete link.

Aug 20 '07 #2
"Active8" <mi******@gmail .comwrote in message
news:11******** *************@5 7g2000hsv.googl egroups.com...
Hi:

This prob "magically" stopped and I'm not sure if I can recreate it,
but thought I'd share it and maybe learn something.

#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
#include <fstream.h//iostream.h

// yes, I'll fix those includes, too

class Yi
{
public:
Yi();

int Number(char bits){return _myNumber[bits];
}

private:
int _myNumber[64];
int _myReverse[64];
//int place1; // prevents array element corruption.
char _myShortName[64][32];
char _myName[64][64];
};

#include <iostream.h>
#include <string.h>
#include "yi.structure.h "

// ditto with those includes

Yi::Yi()
{
int num[64] = {2,23,8,20,16,3 5,45,12,
15,52,39,53,62, 56,31,33,
7,4,29,59,40,64 ,47,6,
46,18,48,57,32, 50,28,44,
24,27,3,42,51,2 1,17,25,
36,22,63,37,55, 30,49,13,
19,41,60,61,54, 38,58,10,
11,26,5,9,34,14 ,43,1};
<snip>
for(int i=0;i<64;i++)
{
_myNumber[i] = num[i];
_myReverse[ num[i] ] = i;
strcpy(_myName[i],name[i]);
strcpy(_myShort Name[i],short_name[i]);
}
}
Consider what happens when i == 21.
Then num[i] == 64.
When you do _myReverse[ num[i] ] = i, this is actually _myReverse[64]
= 21;
In other words, you are writing one element beyond the end of array
_myReverse.
This was previously writing over your member _myShortName, but after
you added the extra int as padding this is what gets overwritten
instead.

HTH
Mark
Aug 20 '07 #3
On Aug 20, 2:40 pm, "Mark Holland" <kenshin...@hto mail.comwrote:
"Active8" <mike....@gmail .comwrote in message

news:11******** *************@5 7g2000hsv.googl egroups.com...
Hi:
This prob "magically" stopped and I'm not sure if I can recreate it,
but thought I'd share it and maybe learn something.
#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
#include <fstream.h//iostream.h
// yes, I'll fix those includes, too
class Yi
{
public:
Yi();
int Number(char bits){return _myNumber[bits];
}
private:
int _myNumber[64];
int _myReverse[64];
//int place1; // prevents array element corruption.
char _myShortName[64][32];
char _myName[64][64];
};
#include <iostream.h>
#include <string.h>
#include "yi.structure.h "
// ditto with those includes
Yi::Yi()
{
int num[64] = {2,23,8,20,16,3 5,45,12,
15,52,39,53,62, 56,31,33,
7,4,29,59,40,64 ,47,6,
46,18,48,57,32, 50,28,44,
24,27,3,42,51,2 1,17,25,
36,22,63,37,55, 30,49,13,
19,41,60,61,54, 38,58,10,
11,26,5,9,34,14 ,43,1};

<snip>
for(int i=0;i<64;i++)
{
_myNumber[i] = num[i];
_myReverse[ num[i] ] = i;
strcpy(_myName[i],name[i]);
strcpy(_myShort Name[i],short_name[i]);
}
}

Consider what happens when i == 21.
Then num[i] == 64.
When you do _myReverse[ num[i] ] = i, this is actually _myReverse[64]
= 21;
In other words, you are writing one element beyond the end of array
_myReverse.
This was previously writing over your member _myShortName, but after
you added the extra int as padding this is what gets overwritten
instead.

HTH
Mark
Thanks Mark man. I'll double check that. Damn! I know better and just
hate seeing that num-1 in my index. But not as much as VB defaulting
to base index (?) of 1. Not sure if the reverse array was even there
yet. I commented out that padding and the very first element of
short_name[] is getting hosed. I forget studying the ansi/iso std
preping for brain bench, but IIRC class members get initialized from
bottom to top I might need to think this through backwards.

You know what? Even when I let it get corrupted, I out put bot long
and short names and they match, I wouldn't expect that if my index was
off by one.

I'll play around and come back if I have more probs or anything of
value to contrib.

And duh! The delete (remove) link is in "more options" right where I
left it.

Thanks Guru.
Mike

Aug 20 '07 #4

Active8 <mi******@gmail .comwrote in message...
Hi:
This prob "magically" stopped and I'm not sure if I can recreate it,
but thought I'd share it and maybe learn something.

#include "stdafx.h"
Non-standard header, don't use (in this NG).
#include <stdlib.h>
#include <math.h>
#include <fstream.hfil e://iostream.h
// yes, I'll fix those includes, too
#include <cstdlib // <stdlib.h>

Only include headers you are using.

Is this homework? If it is, just ignore the following suggestions.

/*
class Yi{ public:
Yi();
int Number(char bits){ return _myNumber[bits]; }
private:
int _myNumber[64];
int _myReverse[64];
file://int place1; // prevents array element corruption.
char _myShortName[64][32];
char _myName[64][64];
};
*/

// --- Yi.h ---
#include <string>
#include <vector>

class Yi{ public:
Yi( std::size_t ); // note: acts like default Ctor.
int Number( char bits ){
size_t indx( bits);
if( indx myNumber.size() ){
return 0;
} // if(indx)
return myNumber.at( indx );
} // Number(char)
private:
std::vector<int myNumber;
std::vector<int myReverse;
std::vector<std ::stringmyShort Name;
std::vector<std ::stringmyName;
};
// ......

// --- Yi.cpp ---
#include <string>
#include <algorithm // for copy()
#include "Yi.h" // or whatever you name it

int num[64] = {2,23,8,20,16,3 5,45,12,
15,52,39,53,62, 56,31,33,
7,4,29,59,40,64 ,47,6,
46,18,48,57,32, 50,28,44,
24,27,3,42,51,2 1,17,25,
36,22,63,37,55, 30,49,13,
19,41,60,61,54, 38,58,10,
11,26,5,9,34,14 ,43,1};

std::string short_name[64] = {"Creative","Re ceptive",
// // blah blah
};
std::string name[64] = {"The Creative","The Receptive",
// // more blah
};

Yi::Yi( std::size_t size = 65 ) : myReverse(size) { // note colon
std::copy( num, num + sizeof( num )/sizeof( *num ),
std::back_inser ter( myNumber ) ); // _myNumber[i]=num[i];
std::copy( name, name + sizeof( name )/sizeof( *name ),
std::back_inser ter( myName ) );
std::copy( short_name,
short_name + sizeof( short_name )/sizeof( *short_name ),
std::back_inser ter( myShortName ) );
for( size_t i(0); i < myNumber.size() ; ++i ){
myReverse.at( myNumber.at( i ) ) = i;
} // for(i)
} // Yi::Yi() Ctor
// ......

// --- YiMain.cpp ---
#include <iostream>
#include "Yi.h" // or whatever you name it

int main(){
Yi yipe; // note how def' size acts like default Ctor.
cout<<"Yi yipe yipe.Number('B'-'A')="
<<yipe.Number(' B'-'A')<<std::endl ;
return 0;
} // main()

// out: Yi yipe yipe.Number('B'-'A')=23

--
Bob R
POVrookie
Aug 20 '07 #5

"Active8" <mi******@gmail .comwrote in message
news:11******** *************@5 7g2000hsv.googl egroups.com...
Hi:

This prob "magically" stopped and I'm not sure if I can recreate it,
but thought I'd share it and maybe learn something.

#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
#include <fstream.h//iostream.h

// yes, I'll fix those includes, too

class Yi
{
public:
Yi();

int Number(char bits){return _myNumber[bits];
}

private:
int _myNumber[64];
int _myReverse[64];
//int place1; // prevents array element corruption.
char _myShortName[64][32];
char _myName[64][64];
};

#include <iostream.h>
#include <string.h>
#include "yi.structure.h "

// ditto with those includes

Yi::Yi()
{
int num[64] = {2,23,8,20,16,3 5,45,12,
15,52,39,53,62, 56,31,33,
7,4,29,59,40,64 ,47,6,
Mark is correct, the 64 in yoru data is the offense. Arrays in C and C++
are 0 bound, they go from 0 to length - 1. So to make this program work as
designed, each and every number should be one less. Here in the data is
probalby the best place.
46,18,48,57,32, 50,28,44,
24,27,3,42,51,2 1,17,25,
36,22,63,37,55, 30,49,13,
19,41,60,61,54, 38,58,10,
11,26,5,9,34,14 ,43,1};

char short_name[64][32] = {"Creative","Re ceptive",

// blah blah

char name[64][64] = {"The Creative","The Receptive",
// more blah

for(int i=0;i<64;i++)
{
_myNumber[i] = num[i];
_myReverse[ num[i] ] = i;
and here it becomes for one interatation:
_myReverse[ 64 ] = i;
but _myReverse only goes from [0] to [63]
strcpy(_myName[i],name[i]);
strcpy(_myShort Name[i],short_name[i]);
}
}

See that odd comment

//int place1; // prevents array element corruption.

?

That's what I had to do to make it work. What would happen was I'd
instanciate a Yi and look up the number of a symbol using

int Number(char bits){return _myNumber[bits];

based on 6 bits (char bits, would you guess?) which represent the
symbol.

Number() always returned 21 - saw it in the debugger. I get lost when
the debugger steps into the library code and couldn't figure out
exactly where the problem was, just that the return value of Number()
was always 21.

//int place1; // prevents array element corruption.

was the fix except at the time int _myReverse[64]; wasn't needed and
I wouldnt be surprised if int _myReverse[64]; is what's keeping things
working today.

Structure member alignment has always been set at 8 bytes FWIW

I thought the array was corrupted because of the fix, but am not sure
if I checked the whole array at the time. maybe it wassomething else.
Further comment, preceeding any variable with an underscore _ is considered
bad form. There are many instances where variable names preceeded by an
underscore are reserved. There are various rules so it's better just to not
do it at all. Personally, private member variables I append an underscore
at the end.

myShortNames_
myname_
Aug 21 '07 #6
On Aug 20, 7:28 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
Active8 <mike....@gmail .comwrote in message...
Hi:
This prob "magically" stopped and I'm not sure if I can recreate it,
but thought I'd share it and maybe learn something.
#include "stdafx.h"

Non-standard header, don't use (in this NG).
It's a VC thing. Goes with stdafx.cpp - Precompiled headers - they're
turned off now (they don't compile) but it's in the project in case I
use it. The stdafx.h includes stuff the whole app uses and doesn't
change often - think that's the idea. Everything gets precompiled up
to stdafx.h and it savdes time. This little proggy doesn't warrant all
that right now but when it grows up...
>
#include <stdlib.h>
#include <math.h>
#include <fstream.hfil e://iostream.h
// yes, I'll fix those includes, too

#include <cstdlib // <stdlib.h>

Only include headers you are using.
They're used elsewhere.
Is this homework? If it is, just ignore the following suggestions.
No. It going to be a windows app when it grows up. For now it's for
friends to try out I Ching without investing in books, coins, stalks,
etc.

So thanks for the alternate code. I should try to find STL solutions
"before" I start coding. I like what I see so far. Easy to read and
probably more foolproof. Thanks.

Mike
>
/*class Yi{ public:
Yi();
int Number(char bits){ return _myNumber[bits]; }
private:
int _myNumber[64];
int _myReverse[64];
file://int place1; // prevents array element corruption.
char _myShortName[64][32];
char _myName[64][64];
};

*/

// --- Yi.h ---
#include <string>
#include <vector>

class Yi{ public:
Yi( std::size_t ); // note: acts like default Ctor.
int Number( char bits ){
size_t indx( bits);
if( indx myNumber.size() ){
return 0;
} // if(indx)
return myNumber.at( indx );
} // Number(char)
private:
std::vector<int myNumber;
std::vector<int myReverse;
std::vector<std ::stringmyShort Name;
std::vector<std ::stringmyName;
};
// ......

// --- Yi.cpp ---
#include <string>
#include <algorithm // for copy()
#include "Yi.h" // or whatever you name it

int num[64] = {2,23,8,20,16,3 5,45,12,
15,52,39,53,62, 56,31,33,
7,4,29,59,40,64 ,47,6,
46,18,48,57,32, 50,28,44,
24,27,3,42,51,2 1,17,25,
36,22,63,37,55, 30,49,13,
19,41,60,61,54, 38,58,10,
11,26,5,9,34,14 ,43,1};

std::string short_name[64] = {"Creative","Re ceptive",
// // blah blah
};
std::string name[64] = {"The Creative","The Receptive",
// // more blah
};

Yi::Yi( std::size_t size = 65 ) : myReverse(size) { // note colon
std::copy( num, num + sizeof( num )/sizeof( *num ),
std::back_inser ter( myNumber ) ); // _myNumber[i]=num[i];
std::copy( name, name + sizeof( name )/sizeof( *name ),
std::back_inser ter( myName ) );
std::copy( short_name,
short_name + sizeof( short_name )/sizeof( *short_name ),
std::back_inser ter( myShortName ) );
for( size_t i(0); i < myNumber.size() ; ++i ){
myReverse.at( myNumber.at( i ) ) = i;
} // for(i)
} // Yi::Yi() Ctor
// ......

// --- YiMain.cpp ---
#include <iostream>
#include "Yi.h" // or whatever you name it

int main(){
Yi yipe; // note how def' size acts like default Ctor.
cout<<"Yi yipe yipe.Number('B'-'A')="
<<yipe.Number(' B'-'A')<<std::endl ;
return 0;
} // main()

// out: Yi yipe yipe.Number('B'-'A')=23

--
Bob R
POVrookie

Aug 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2703
by: Mark C. | last post by:
The company I work for has been running Access 2000 on a Windows NT server with Opportunistic Locking turned off on the server without issue for almost a year. We have just switched to a Windows 2000 file server with service pack 3 installed and are now experiencing at least 2 corruptions a day. The registry setting for Opportunistic Locking...
4
4605
by: DFS | last post by:
" has detected corruption in this file. To try to repair the corruption, first make a backup copy of the file. Then, on the Tools menu, point to Database Utilities and click Compact and Repair database. If you are currently trying to repair this corruption then you will need to recreate this file or restore it from a previous backup." I...
2
4433
by: jv | last post by:
I have a form that is used every day to create quotes. However, every 2 weeks or so a user would randomly get an error 9 - subscript out range message. I'm not using any array in this form. Does anyone know that else might possibly trigger this error besides array? Thanks Julie
47
4485
by: ship | last post by:
Hi We need some advice: We are thinking of upgrading our Access database from Access 2000 to Access 2004. How stable is MS Office 2003? (particularly Access 2003). We are just a small company and this is a big decision for us(!) It's not just the money it's committing to an new version of Access!
8
3394
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
15
1727
by: Yogi_Bear_79 | last post by:
Visual Studio .NET started complaing when the array was around 4000. I found that if I pasted the array in via notepad then opened Visual Studio it would work. Now my array is over 26,000 and Visual Studio just doesn't like it. Everytime I open the prokect I get the following error: "An Error occurred which the C# comipler is unable to...
8
2268
by: pranav.choudhary | last post by:
Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any error for the declaration int a; If the declaration is legal, then what will be the implications of saying a = 10;
23
2543
by: Dave G | last post by:
Since upgrading one of my clients from A97/W2000 to A2003/XP they have suffered no end of data corruption problems, mainly involving one of the main tables. The corruption can result in one record's fields turning into chinese characters. On running the compact/repair I can see that the corruption may be deeper than I expected because...
14
2861
by: Michel Rouzic | last post by:
Hi, I've recently met issues with my program which can only be explained by heap corruption, so I've tried debugging my program with Valgrind, and here's what I get with the following multidimensional array allocation code : typedef struct { int32_t speed;
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.