表达式:_CrtlsValidHeapPointer(block) C++ matrix with MASM32

问题描述 投票:0回答:1

在 C++ 代码中,我为矩阵分配内存,并在代码中间的主版本结束时调用汇编程序插入过程 matrix_transformations。当我释放内存时出现错误。如果我在 masm 代码中释放内存,错误就在那里,如果没有,它在 main 末尾的 C++ 中。

#include <iostream>
#include <string>
#include <limits>


extern "C" void __fastcall matrix_transformations(int** p_matrix, int u_size);

int input_int(bool allow_negative);

void input_matrix(int** matrix, int size);

void output_matrix(int* const* const  matrix, int size);

using std::cout;
using std::endl;
using std::cin;
using std::string;



int input_int(bool allow_negative = true)
{
    int num;
    while (true)
    {
        
        std::cin >> num;
        if (std::cin.fail())
        {
            // Input is not a valid integer
            std::cin.clear(); // Clear the error flag
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input. Please try again.\n";
        }
        else if (!allow_negative && num < 0)
        {
            // Input is negative but negatives are not allowed
            std::cout << "Negative input not allowed. Please try again.\n";
        }
        else
        {
            // Input is valid
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            return num;
        }
    }
}

void input_matrix(int** const matrix, const int size)
{
    for (auto i = 0; i < size; ++i)
        for (auto j = 0; j < size; ++j)
        {
            std::cout << "M[" << i << "][" << j << "]: ";
            matrix[i][j] = input_int();
        }
}


void output_matrix(int* const* const matrix, const int size)
{
    for (auto i = 0; i < size; ++i)
    {
        for (auto j = 0; j < size; ++j)
            cout << matrix[i][j] << ' ';
        cout << endl;
    }
}

int main(int argc, char** argv)
{
    cout << "Enter size of square matrix: ";
    int size = input_int(false);


    int** matrix = new int* [size];
    for (auto i = 0; i < size; ++i)
        matrix[i] = new int[size];

    input_matrix(matrix, size);
    output_matrix(matrix, size);


    matrix_transformations(matrix, size);

    cout << endl << endl;
    output_matrix(matrix, size);


    for (auto i = 0; i < size; ++i)
        delete[] matrix[i];
    delete[] matrix;


    return 0;
}

.586
.model flat, C
option casemap:none

include masm32rt.inc

.code
ALIGN 8

; Sets default language features such as calling convention.
OPTION LANGUAGE: SYSCALL 
@matrix_transformations@8 PROC
    ; ecx - pointer to matrix
    ; edx - size
    
     
    local hHeap       :dword 

    local hHeap2      :dword

    local pArr        :dword ; Pointer to upper array of matrix triangle

    local pArr2       :dword ; Pointer to lower array of matrix triangle

    local triangleLen :dword ; Length of the matrix triangle

    ; Test if the size of the matrix is odd
    test edx, 1 
    .if !ZERO?
        ; If the matrix has even elements, we don't need to do anything.

        ; Get the center of the matrix
        mov eax, edx
        shr eax, 1     ; center of matrix
        
        ; Calculate the size of a row in bytes
        mov  ebx, sizeof dword
        push edx
        mul  ebx 
        pop  edx
        ; Get the element at the center of the matrix
        mov esi, ecx
        mov edi, [esi + eax]  ; center of rows
        mov eax, [edi + eax]  ; center of cols

    
        ; Check if the center element is zero
        .if eax == 0 
                ; If the center element is zero, we need to transform the matrix.

                ; Create two heaps to allocate memory
                push ecx ; save pointer to array
                push edx ; save size to array
                invoke HeapCreate, HEAP_GENERATE_EXCEPTIONS, 0, 0
                mov hHeap, eax
                invoke HeapCreate, HEAP_GENERATE_EXCEPTIONS, 0, 0
                mov hHeap2, eax
                pop edx ; restore 
                pop ecx
                ; Calculate the length of the matrix triangle
                xor eax, eax
                mov ebx, edx
    
                .while ebx > 1
                    sub ebx, 2
                    add eax, ebx
                .endw
            shl eax, 2
            mov triangleLen, eax
        

            push ecx ; save pointer to array
            push edx ; save size to array
            ; Allocate memory for the upper triangle of the matrix
            invoke HeapAlloc, hHeap, HEAP_ZERO_MEMORY, triangleLen 
            mov pArr, eax
            ; Allocate memory for the lower triangle of the matri
            invoke HeapAlloc, hHeap, HEAP_ZERO_MEMORY, triangleLen 
            mov pArr2, eax

            pop edx ; restore 
            pop ecx



            ; Fill the upper triangle of the matrix
            push ecx
            push edx
            mov ebx, edx
            mov esi, ecx
            mov eax, 0 
            mov ecx, 1
            sub ebx, 2
            .while ebx != -1 ; we sub from odd num -2  
                
                
                mov edi, [esi] 
                push ecx

                imul ecx, 4
                add edi, ecx
                pop ecx
                push ebx ; 
                mov edx, [pArr]
                .while ebx != 0
                    mov [edx+eax], edi
                    add eax, sizeof dword
                    add edi, sizeof dword
                    dec ebx
                .endw
                pop ebx
                add esi, sizeof dword
                inc ecx
                sub ebx, 2

            .endw
            pop edx
            pop ecx
            ;------------------------------------------;


            ;---Lower triangle to 2nd array------------;

            push ecx
            push edx
            mov ebx, edx
            mov esi, ecx
            push ebx
            dec ebx
            imul ebx, sizeof dword
            add esi, ebx
            pop ebx
            mov eax, 0 
            mov ecx, 1
            sub ebx, 2

            

            .while ebx != -1 ; we sub from odd num -2  
                
                
                mov edi, [esi] 
                push ecx
                imul ecx, 4
                add edi, ecx
                pop ecx
                push ebx ; 
                mov edx, [pArr2]
                .while ebx != 0
                    mov [edx+eax], edi
                    add eax, sizeof dword
                    add edi, sizeof dword
                    dec ebx
                .endw
                pop ebx
                sub ebx, 2
                sub esi, 4 ;sizeof dword
                inc ecx

            .endw
            pop edx
            pop ecx

            ;------------------------------------------;
            ; exchange numbers ;
                mov ebx, triangleLen
                shr ebx, 2
                push ecx


                mov esi, [pArr]
                mov edi, [pArr2]
            .while ebx != 0

                    
                    mov eax, [esi]
                    mov ecx, [eax]
                    mov eax, [edi]

                    xchg ecx, [eax]

                    mov eax, [esi]
                    mov [eax], ecx


                    add esi, sizeof dword
                    add edi, sizeof dword
                    dec ebx
            .endw

            pop ecx



            ;------------------------------------------;
            ;invoke HeapFree, hHeap, 0, pArr
            ;invoke HeapFree, hHeap2, 0, pArr2
            ;invoke HeapDestroy, hHeap
            ;invoke HeapDestroy, hHeap2
        .endif 
    .endif 

    ret

@matrix_transformations@8 ENDP
OPTION LANGUAGE: C


END

我试图通过在 MASM32 过程中分配一个局部变量来摆脱动态内存,但我仍然在 C++ 中遇到 delete[] 错误

c++ assembly x86 dynamic-memory-allocation masm32
1个回答
0
投票

我的解决方案是用 alloc 和 free 代替 new 和 delete 的使用。

© www.soinside.com 2019 - 2024. All rights reserved.