【问题标题】:ESP calling convention issues C++ESP 调用约定问题 C++
【发布时间】:2011-09-16 21:48:13
【问题描述】:

对于家庭作业,我应该读取一个文件并对字符串进行排序。为此,我使用了选择排序并且它有效。现在,在它调用 selSort 函数之后,它就崩溃了。我已经没有解决这个问题的想法了,谁能帮帮我?

// 2_8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <wchar.h>
#include <fstream>
#include <iostream>
using namespace std;

class convert
{
private:
    ifstream myFile;
    TCHAR charArray[1000][25];
    int size;

public:
    convert()
    {
        myFile.open("rand.txt");
        for(int x=0;x<1000;x++)
        {
            for(int y=0;y<25;y++)
            {
                charArray[x][y] = NULL;
            }
        }
        if(!myFile)
        {
            cout << ("File not open") << endl;
        }
        else
        {
            cout << ("File opened") << endl;
        }
    }

    void readFile()
    {
        int x = 0;
        int y = 0;
        int result =0;

        if(myFile.is_open())
        {
            TCHAR tempChar;
            while(!myFile.eof())
            {
                tempChar = myFile.get();
                if(tempChar != 32)
                {
                    charArray[x][y++] = tempChar;
                }
                else
                {
                    size=x++;
                    y = 0;
                }
            }
        }
        result = selSort(charArray,size);

        if(result)
        {
            cout << "We did it!!!!";
        }
    }

    void printString()
    {
        for(int x=0;x<1000;x++)
        {
            for(int y=0;y<25;y++)
            {
                cout << charArray[x][y];
            }
            cout << endl;
        }
    }

    int selSort(TCHAR thArray[][25], int length)
    {
        TCHAR tempArray[1][25];


        for(int x=0;x<1;x++)
        {
            for(int y=0;y<25;y++)
            {
                tempArray[1][25] = NULL;
            }
        }
        for(int x=0;x<length;x++)
        {
            int best = 0;

            for(int y=1;y<length;y++)
            {
                int result = _tcscmp(thArray[y],thArray[best]);
                if (result == 1)
                {
                    best = y;
                }
                for (int t=0;t < _tcslen(thArray[best]);t++)
                {
                    tempArray[0][t] = thArray[best][t];
                }
                for(int w=0;w < _tcslen(thArray[x]);w++)
                {
                    thArray[best][w]=thArray[x][w];

                }
                for(int e=0;e < _tcslen(thArray[x]);e++)
                {
                    thArray[x][e]=tempArray[0][e];

                }
            }
        }
        return 1;
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    convert c1;

    c1.readFile();

    system("pause");
    return 0;
}

【问题讨论】:

  • 有什么理由在这个程序中使用 tmain 和 TCHAR 吗?

标签: c++ visual-studio-2010 calling-convention stack-pointer


【解决方案1】:
int selSort(TCHAR thArray[][25], int length)
{
    TCHAR tempArray[1][25];

    // ...

    tempArray[1][25] = NULL;   // In the for loop
 }

tempArray 中没有第二行。如果一个数组有 n 行,那么它的索引从 0 到 n-1 开始。可能你的意思是-

tempArray[x][y] = NULL;

不要编写循环,而是使用算法头中的std::fill 函数来用唯一元素填充数组元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2013-07-03
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    相关资源
    最近更新 更多