gpt4 book ai didi

c - AVR 模拟器中的意外指针重新分配

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:22 25 4
gpt4 key购买 nike

以下函数 populateArpeggioArray 接受一个指向包含多个成员的 typedef 结构的指针,这些成员的身份不一定与我遇到的问题相关。在将指向结构的指针传递给函数 sortNoteStack 之前,populateArpeggioArray 应该对结构的内部元素执行一些操作。

我发现 sortNoteStack 从未对该结构进行操作,因为 populateArpeggioArray 在将更改后的值传递给 sortNoteStack 之前,有时会更改指向该结构的指针的值。

据我所知,代码的格式似乎正确,对结构元素执行的所有操作都使用了正确的指针解除引用。据我所知,没有一行代码能够修改指向结构的指针的值。使用 const 前缀声明结构没有帮助,因为它的成员因此也被锁定为固定值。

我认为这可能是模拟器的问题,而不是代码的问题,但如果我编写的代码存在一些潜在的问题,我当然想了解我的意思做错了。

感谢您的帮助。

-尼克

void populateArpeggioArray(arpeggio* arp) {
uint8_t i = 0,j=0, newNoteFlag=0;

for(i=0; i<12; i++) {
newNoteFlag = 0;
if(globals.keyboardCurrentState[i]) { /* Check to see if the current state shows that a keyboard button is pressed. */
arp->sequenceLength++; /* Temporarily increase the sequence length */
for(j=0;j < arp->sequenceLength;j++) { /* Check the pitch of each note currently in the note stack */
if(globals.keyboardNotes[i] == arp->notesUnordered[j].pitch) { /* If the currently selected note is already present in the note stack, */
arp->sequenceLength--;
newNoteFlag = 1; /* undo the temporary sequence length increase */
}
}
if(!newNoteFlag) {
arp->notesOrdered[arp->sequenceLength].pitch = globals.keyboardNotes[i]+liveArpeggio.transposeShift;
arp->notesUnordered[arp->sequenceLength].pitch = globals.keyboardNotes[i]+liveArpeggio.transposeShift; /* Add the new pitch to the appended note */
arp->notesOrdered[arp->sequenceLength].length = 111;
arp->notesUnordered[arp->sequenceLength].length = 111; /* Give the new note a default length. TEMP */
}
}
}
sortNoteStack(&arp);
}

最佳答案

使用sortNoteStack(&arp),您传递的是指针的地址,而不是结构的地址。你想传递结构的地址(指针的值)。因此,使用 sortNoteStack(arp)

关于c - AVR 模拟器中的意外指针重新分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18046648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com