日期:2014-05-16  浏览次数:20764 次

一个无法再简单的快排程序,为什么在对一个大的整数数组进行排序的时候老是段错,小的却不会。50分!
以下是快排程序,我放了两个数组进去排序,第二个是根据第一个变而变,实际是记录第一个数组排序的位置变化,所以实际是对第一个整数数组进行排序:
void
swapIntArray4Sort(const   int   pos1,   const   int   pos2,   UINT32   *corpWeight,   UINT32   *recCorpPos)
{
assert(0   <=   pos1   &&   0   <=   pos2);

swapInteger(   (int   *)&corpWeight[pos1],   (int   *)&corpWeight[pos2]);
swapInteger(   (int   *)&recCorpPos[pos1],   (int   *)&recCorpPos[pos2]);
}

void
quickSort4CorpWeight(int   left,   int   right,   UINT32   *corpWeight,   UINT32   *recCorpPos)
{      
     
if   (left   > =   right)   /*   do   nothing   if   array   contains   */
      {
  return;                 /*   fewer   than   two   elements   */
    }

/*   move   partition   elem   */
swapIntArray4Sort(left,   (left   +   right)   /   2,   corpWeight,   recCorpPos);
   
UINT32   last   =   0;
last   =   left;

UINT32   i   =   0;

      for   (i   =   left   +   1;   i   <=   right;   i++)   /*   partition   */
      {
  if   (corpWeight[i]   <   corpWeight[left])
{
swapIntArray4Sort(++last,   i,   corpWeight,   recCorpPos);
}
      }
           
swapIntArray4Sort(left,   last,   corpWeight,   recCorpPos); /*   restore   partition   elem   */

quickSort4CorpWeight(left,   last   -   1,   corpWeight,   recCorpPos);
quickSort4CorpWeight(last   +   1,   right,   corpWeight,   recCorpPos);
}


------解决方案--------------------
问题在于数组太大时,递归耗尽了栈空间,VC默认栈1M,看来需要更改这个默认设置。
以下来自
http://www.vckbase.com/bbs/prime/viewprime.asp?id=163
问题提出]

VC++下怎样设置栈的大小

[问题解答]

方法一:STACKSIZE 定义.def文件

语法:STACKSIZE reserve[,commit]
reserve:栈的大小;commit:可选项,与操作系统有关,在NT上只一次分配物理内存的大小

方法二:设定/STACK

打开工程,依次操作菜单如下:Project-> Setting-> Link,在Category 中选中Output,然后
在Reserve中设定堆栈的最大值和commit。

注意:reserve默认值为1MB,最小值为4Byte;commit是保留在虚拟内存的页文件里面,它设置的较
大会使栈开辟较大的值,可能增加内存的开销和启动时间

------解决方案--------------------
用--stack选项,自己man一下就知道了。

LD(1) GNU Development Tools LD(1)

NAME
ld - Using LD, the GNU linker

SYNOPSIS
ld [options] objfile ...

DESCRIPTION
...
--stack reserve
--stack reserve,commit
Specify the amount of memory to reserve (and optionally commit) to
be used as stack for this program. The default is 2Mb reserved, 4K
committed. [This option is specific to the i386 PE targeted port
of the linker]

------解决方案--------------------
错误提示是什么啊?堆栈溢出和内存越界的提示是不同的啊.