COCKOS
CONFEDERATED FORUMS
Cockos : REAPER : NINJAM : Forums
Forum Home : Register : FAQ : Members List : Search :
Old 01-13-2024, 04:48 PM   #1
Kite
Human being with feelings
 
Join Date: Apr 2010
Location: Portland OR
Posts: 217
Default C++ question - overlapping arrays?

I need to do something rather unorthodox in C++. Can't find the answer anywhere online.

I need to define an array that overlaps another array, so both arrays read from and write to the same memory locations. Related, I also need to define an array of chars that overlaps an array of strings.

Something like this:
Code:
byte originalArray[256];
byte myArray = originalArray + 128; // an array of bytes, it starts halfway in

if (myArray[0] == myArray[1]) {     // read from originalArray[128] and [129]
  myArray[2] = 3;                   // write to originalArray[130]
}
Do I need to define the length of myArray? If so, how?

2nd example, for strings:
Code:
char originalArray [10][32];        // 10 null-terminated strings, length <= 31
char myArray = originalArray + 128; // an array of chars, not null-terminated

if (myArray[0] == ' ') {            // read/write 1st char of 5th string
  myArray[0] = '!';
}
I know this is not good programming practice. But I absolutely positively have to do it this way! (Long story.)
__________________
alt-tuner: a microtonal midi plug-in: www.TallKite.com/alt-tuner.html
The Kite Guitar, a playable 41-ET guitar: www.KiteGuitar.com
Kite is offline   Reply With Quote
Old 01-13-2024, 07:37 PM   #2
jacksoonbrowne
Human being with feelings
 
jacksoonbrowne's Avatar
 
Join Date: Aug 2017
Location: Ottawa, Canada
Posts: 586
Default

Code:
char * myArray = (char *)originalArray + 128; // an array of chars, not null-terminated
__________________
AKA: Roy Wallingford
jacksoonbrowne is offline   Reply With Quote
Old 01-14-2024, 12:22 AM   #3
Kite
Human being with feelings
 
Join Date: Apr 2010
Location: Portland OR
Posts: 217
Default

Thanks, will test this out soon!
__________________
alt-tuner: a microtonal midi plug-in: www.TallKite.com/alt-tuner.html
The Kite Guitar, a playable 41-ET guitar: www.KiteGuitar.com
Kite is offline   Reply With Quote
Old 01-18-2024, 04:23 AM   #4
Kite
Human being with feelings
 
Join Date: Apr 2010
Location: Portland OR
Posts: 217
Default

Works great!

Next question, what if I want to access myArray[i][j]? Something like this:

Code:
byte originalArray[200];
byte * myArray[0] = (byte *)originalArray + 100; 
byte * myArray[1] = (byte *)originalArray + 108; 

if (myArray[0][2] == myArray[1][2]) {  // read from originalArray[102] and [110]
  myArray[0][3] = 4;                   // write to originalArray[103]
}
or maybe this:

Code:
byte originalArray[2][200];
byte * myArray[0] = (byte *)originalArray + 100;  

if (myArray[0][2] == myArray[1][2]) {  // read from originalArray[0][102] and [1][102]
  myArray[0][3] = 4;                   // write to originalArray[0][103]
}
__________________
alt-tuner: a microtonal midi plug-in: www.TallKite.com/alt-tuner.html
The Kite Guitar, a playable 41-ET guitar: www.KiteGuitar.com

Last edited by Kite; 01-18-2024 at 05:21 AM.
Kite is offline   Reply With Quote
Old 01-18-2024, 05:45 PM   #5
Kite
Human being with feelings
 
Join Date: Apr 2010
Location: Portland OR
Posts: 217
Default

Also, is it possible for a data structure to overlap an array of bytes?

Code:
byte originalArray[200];

struct note {
  byte pitch;       // midi note number
  byte duration;    // measured in 16th notes
};
note * Notes[61] = (byte *)originalArray;

n = 10;
if (Notes[n].pitch > 60) {   // read from originalArray[20]
  Notes[n].duration = 8;     // write to originalArray[21]
}
And is it possible to mix data types?
Code:
byte originalArray[200];
signed char * myArray1 = (byte *)originalArray;
    boolean * myArray2 = (byte *)originalArray + 20;
        int * myArray3 = (byte *)originalArray + 60;
In my use case, I would never ever read from or write to originalArray directly. I would only read/write myArray. So I wouldn't be attempting to interpret a byte as a boolean, or four bytes as an int.

And finally, what about just accessing one element of originalArray as a simple variable, via an alias? Something like:

Code:
byte originalArray[200];
byte * myVar1 = originalArray + 10;
byte * myVar2 = originalArray + 11;

if (myVar1 == 0) {       // read from originalArray [10]
  myVar2 = 3;            // write to originalArray [11]
}
Perhaps there is some general method of making a variable or an array or a data structure (or an array of structures, a structure of arrays, etc. etc.) overlap an array of bytes?
__________________
alt-tuner: a microtonal midi plug-in: www.TallKite.com/alt-tuner.html
The Kite Guitar, a playable 41-ET guitar: www.KiteGuitar.com
Kite is offline   Reply With Quote
Old 01-18-2024, 06:21 PM   #6
jacksoonbrowne
Human being with feelings
 
jacksoonbrowne's Avatar
 
Join Date: Aug 2017
Location: Ottawa, Canada
Posts: 586
Default

Quote:
And is it possible to mix data types?
Code:
byte originalArray[200];
signed char * myArray1 = (byte *)originalArray;
    boolean * myArray2 = (byte *)originalArray + 20;
        int * myArray3 = (byte *)originalArray + 60;
Yes but you need to properly cast like this example.
Rule is that the right side (cast) must match the left side type.

Code:
char            originalArray[200];
signed char   * myArray1 = (signed char *)    originalArray;
bool          * myArray2 = (bool *)           originalArray + 20;
int           * myArray3 = (int  *)           originalArray + 60;
unsigned char * myArray4 = (unsigned char *)  originalArray + 80;
__________________
AKA: Roy Wallingford
jacksoonbrowne is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 03:45 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.