尝试释放内存部分时,我似乎正在崩溃我的程序。以下是我的链表的整体结构:
typedef struct {
char *dataitem;
struct listelement *link;
int16_t wordSize;
int16_t (*libWord)[Q];
char gpioValue;
struct listelement *syllables;
}listelement;
调用此函数时程序崩溃:
recordedWordsPointer = RemoveItem(recordedWordsPointer); // get rid of any junk stored in the recorded buffer
在哪里:
volatile listelement *recordedWordsPointer;
已将值存储在 libWord 中,如果有另一个链接,则指向下一个链接,否则为 NULL。下面显示了进入函数时发生的情况:
listelement * RemoveItem (listelement * listpointer) {
cpu_irq_disable();
listelement * tempp = listpointer;
while( listpointer->syllables != NULL ){
RemoveSyllable(listpointer->syllables);
}
if( listpointer != NULL ){
tempp = listpointer -> link;
free (listpointer->dataitem);
free (listpointer->libWord);
free (listpointer);
}
cpu_irq_enable();
return tempp;
}
void RemoveSyllable (listelement * listpointer) {
while( listpointer->syllables != NULL ){
RemoveSyllable(listpointer->syllables);
}
free (listpointer->dataitem);
free (listpointer->libWord);
free (listpointer);
listpointer = NULL;
return;
}
我想知道我是否做错了什么导致内存崩溃?
谢谢!
编辑:
我被要求展示我如何构建内存位置来提供帮助。我使用以下两个功能:
listelement * AddItem (listelement * listpointer, char* name, int16_t size, int16_t wordLength, int16_t (*words)[Q]) {
// returns listPointer at the beginning of list
listelement * lp = listpointer;
listelement * listPointerTemp;
char ErrorHandler = NULL;
// are we at the end of the list?
if (listpointer != NULL) {
// move down to the end of the list
while (listpointer -> link != NULL)
listpointer = listpointer -> link;
listPointerTemp = listpointer;
listpointer -> link = (struct listelement *) malloc (sizeof (listelement));
// on fail end links becomes NULL already above
if(listpointer -> link != NULL){
listpointer = listpointer -> link;
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> syllables = NULL;
listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));
if(listpointer -> dataitem != NULL){
for(int i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> dataitem[size] = NULL;
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> link = NULL;
}
}else{
free(listpointer);
listPointerTemp -> link = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return lp;
} else {
listpointer = (struct listelement *) malloc (sizeof (listelement));
if(listpointer != NULL){
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> syllables = NULL;
listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));
if(listpointer -> dataitem != NULL){
for(int16_t i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> dataitem[size] = NULL;
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> link = NULL;
}
}else{
free(listpointer);
listPointerTemp -> link = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return listpointer;
}
}
listelement* AddSyllable (listelement * listpointer, char* name, int16_t size, int16_t wordLength, int16_t (*words)[Q]) {
// returns listPointer at the beginning of list
listelement * lp = listpointer;
listelement * listPointerTemp;
char ErrorHandler = NULL;
// are we at the end of the list?
if (listpointer != NULL) {
// move down to the end of the list
while (listpointer -> syllables != NULL)
listpointer = listpointer -> syllables;
listPointerTemp = listpointer;
listpointer -> syllables = (struct listelement *) malloc (sizeof (listelement));
// on fail end links becomes NULL already above
if(listpointer -> syllables != NULL){
listpointer = listpointer -> syllables;
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));
if(listpointer -> dataitem != NULL){
for(int i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> dataitem[size] = NULL;
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> syllables = NULL;
}
}else{
free(listpointer);
listPointerTemp -> syllables = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return lp;
} else {
listpointer = (struct listelement *) malloc (sizeof (listelement));
if(listpointer != NULL){
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));
if(listpointer -> dataitem != NULL){
for(int16_t i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> dataitem[size] = NULL;
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> syllables = NULL;
}
}else{
free(listpointer);
listPointerTemp -> syllables = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return listpointer;
}
}