2

順序付けされた挿入関数を使用して、C で連結リストを作成します。配列リストは個々のリンクされたリストの配列であり、10000 の乱数を生成する必要があり、300 または 400 の数を生成できる場合もあれば、失敗してバッファ オーバーランの例外が発生する場合もあります。これを取得する理由は何ですか?

メモリを解放する必要があるためだと思いましたが、割り当てているすべてのメモリが必要なようで、何も残っていません。

エラーが発生すると、コール スタックに次の行が表示されます。

struct Node *newNode = (struct Node *)malloc(sizeof(*newNode));

例外の原因です。

生成される数字が少なくても適切に機能します。100個の数字を実行すると、出力は次のようになります 。

// Program 6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <time.h>
#include <stdlib.h>


#define MAX 200

void orderedInsert(struct Node **, int);
void printList(struct Node **, int);


struct List{
    int size;
    struct Node *front;
};

struct Node{
    int value;
    struct Node *next;
};

void main(){

struct List lists[MAX];
int i, random;

for(i = 0; i < MAX; i++){
    lists[i].front = 0;
    lists[i].size = 0;
}
srand(time(NULL));

for(i = 0; i < 100; i++){
    random = rand() % 10000000;
    orderedInsert( &(lists[random%MAX].front), random);
    (lists[i].size)++;
}

for(i = 0; i < MAX; i++){
    printf("%d ", i);
    printList( &(lists[i].front), lists[i].size);
}


scanf_s("%d", NULL);

}


void orderedInsert(struct Node **front, int value){

struct Node *newNode = (struct Node *)malloc(sizeof(*newNode));
struct Node *temp, 
            *prev;

newNode->value = value;


if(*front == NULL){
    *front = newNode;
    newNode->next = 0;
    return;
}

if((*front)->value > newNode->value){
    newNode->next = *front;
    *front = newNode;
    return;
}
temp = (*front)->next;
prev = *front;

while(temp != NULL && temp->value < newNode->value){
    prev = temp;
    temp = temp->next;
}
newNode->next = temp;
prev->next = newNode;

}

void printList(struct Node **front, int value){

struct Node *temp;
temp = *front;

if(temp){
    printf("The list contains elements: %d", temp->value);
    temp = temp->next;

    while(temp != NULL){
        printf(", %d", temp->value);
        temp = temp->next;
    }

    }
    printf("\n");

}

必要な場合は、完全なコール スタックを次に示します。

    msvcr110d.dll!_crt_debugger_hook(int _Reserved) Line 57 C
    Program 6.exe!__raise_securityfailure(_EXCEPTION_POINTERS * ExceptionPointers) Line 67  C
    Program 6.exe!__report_gsfailure() Line 235 C
    msvcr110d.dll!ValidateLocalCookies(void (unsigned int) * CookieCheckFunction, _EH4_SCOPETABLE * ScopeTable, char * FramePointer) Line 198   C
    msvcr110d.dll!_except_handler4_common(unsigned int * CookiePointer, void (unsigned int) * CookieCheckFunction, _EXCEPTION_RECORD * ExceptionRecord, _EXCEPTION_REGISTRATION_RECORD * EstablisherFrame, _CONTEXT * ContextRecord, void * DispatcherContext) Line 329 C
    Program 6.exe!_except_handler4(_EXCEPTION_RECORD * ExceptionRecord,          _EXCEPTION_REGISTRATION_RECORD * EstablisherFrame, _CONTEXT * ContextRecord, void * DispatcherContext) Line 94 C
    ntdll.dll!77e2b499()    Unknown
    [Frames below may be incorrect and/or missing, no symbols loaded for        ntdll.dll]  
    ntdll.dll!77e2b46b()    Unknown
    ntdll.dll!77e2b40e()    Unknown
    ntdll.dll!77de0133()    Unknown
    msvcr110d.dll!malloc(unsigned int nSize) Line 56    C++
>   Program 6.exe!orderedInsert(Node * * front, int value) Line 59  C
    Program 6.exe!main(...) Line 42 C
    Program 6.exe!__tmainCRTStartup() Line 536  C
    cd001c1d()  Unknown

別のエラーが発生しました: プログラム 6.exe の 0x100B26B6 (msvcr110d.dll) で未処理の例外: 0xC0000005: アクセス違反の読み取り場所 0x0146F78F.

このコール スタック:

>   msvcr110d.dll!_nh_malloc_dbg_impl(unsigned int nSize, int nhFlag, int      nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 239 C++
    Program 6.exe!orderedInsert(Node * * front, int value) Line 59  C
    Program 6.exe!main(...) Line 42 C
    Program 6.exe!__tmainCRTStartup() Line 536  C
    a500201d()  Unknown

これは完全なコール スタックではありません。完全なコール スタックは数マイルの長さです。

4

1 に答える 1