Dear everyone,
I want to make a little music playing program that can read notes from a file. I already made this program, but I coded it without preparation and testing while coding, and a unsolvable problem occured. I remade the whole program and tested it all the time. I wrote this piece of code, but it seems that it has some difficulties with the pseudo-multidimensional (it actually is a array of arrays instead of a 'real' 2D C++ array, because you cannot allocate a real multidimensional array) dynamic array which preserves the notes. the clog statements show that the other values are ok, but after the program writes two (wrong) contents of the array mentioned above, it crashes. Perhaps I need to clean (set all the variables to 0) the array before using it, but I assign values to it before using, so I don't think that is necessary.
Ah, thanks for the tip about the increment! I am deleting songArr because s has a copy of songArr, and it doesn't make any difference...
Well, I am able to access arr (in s), but this shows up if I run the program:
arr doesn't contain the desired values, any idea what's wrong? I can't believe it's my algorithm because I remade it twice. Am I stumbling on my limited C++ knowledge again?
arr is given the value held by SA. That is, arr points to the same memory that SongArr points to in main. That is, arr does not point to a copy of the contents of SongArr; it points to the same memory.
What you do is make each element of SongArr (in it's guise as Song::arr) point to a different place in memory (via new and assignment in the constructor.) Then, you assign the content of that new memory to the... content of the new memory.
So array1[x][y]=array2[x][y]; doesn't mean
'change the value in array1 on place x,y to the value in array2 on place x,y'
but
'set the location of the value in array1 on place x,y to the location of the value in array2 on place x,y'
isn't it?
If so, how do I tell the compiler the first message? if I use new I'll still have to use the statement array1[x][y]=array2[x][y];. Please explain, I'm experienced in C++, but dynamic memory just isn't my strongest point...
So array1[x][y]=array2[x][y]; doesn't mean
'change the value in array1 on place x,y to the value in array2 on place x,y'
It means that alright. But when array1 and array2 point to the same place in memory, array1[x][y] = array2[x][y]; is the same as array1[x][y] = array1[x][y], which is essentially a no-op.
If you replace arr(SA) with arr(newunsignedchar*[MSB]) you should be in business, but you're using a lot of dynamic memory allocations where you don't really need to which makes the code a lot hairier than it should be.