直接运行:
退出代码-1073740940 (0xC0000374)
调试出来:
退出代码0
这是什么原因?
#include
#include
#include
#include
typedef struct SL
{
int *elem;
int TableLen;
}SSTable;
//随机数的生成
void ST_Init(SSTable &ST,int len)
{
ST.TableLen=len;
ST.elem= (int*)malloc(sizeof (SSTable));
int i;
srand(time(NULL));
for (i=0;irand()%100;
}
}
void ST_print(SSTable ST)
{
for(int i=0;iprintf("%5d",ST.elem[i]);
}
printf("\n");
}
//两个数的交换
//交换时要取地址
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
//大根堆
void LargeRootPile(int A[],int j,int len)
{
int dad=j;
int son=2*dad+1;
while (son//!容易出错
{
if (son+11])
{
son++;
}
if (A[son]>A[dad])
{
swap(A[son],A[dad]);
dad=son;
son=2*dad+1;
}else
{
break;
}
}
}
//先将A变成大根堆
void HeapSort(int A[],int len)
{
int i;
//使得A成为大根堆
for(i=len/2-1;i>=0;i--)//i取0的时候还是有的
{
LargeRootPile(A,i,len);
}
//A成为大根堆后就要根头与叶尾进行交换
swap(A[0],A[len-1]);
//根头与叶尾进行交换后,继续进行变成大根堆,继续交换;
//
for(i=len-1;i>1;i--)
{
LargeRootPile(A,0,i);//大根堆长度减1了,不会影响后面交换数时,把交换过的又交换
//这时候是从头根进行大根堆,不是从最后一个父亲根进行大根堆;
swap(A[0],A[i-1]);//进行交换时,这里的要进行大根堆总数长度已经改变了
}
}
int main() {
SSTable ST;
ST_Init(ST,10);
//将A固定,还不是随机数去用
int A[10]={ 3, 87, 2, 93, 78, 56, 61, 38, 12, 40 };
memcpy(ST.elem,A,sizeof (A));//头文件是#include
ST_print(ST);
HeapSort(ST.elem,10);
ST_print(ST);
return 0;
}
我觉得是内存溢出的问题,试着malloc一下elem元素空间
第16行应为:ST.elem = (int*)malloc(sizeof(int) * len); // 原代码:ST.elem = (int*)malloc(sizeof(SSTable));
不知道你这个问题是否已经解决, 如果还没有解决的话:#include<stdio.h>
#include<string.h>
/*
* 该函数用来统计子串substr在字符串str中出现的次数
* 统计后的次数以函数值的方式返回
*/
int freq_substring(char* str,char* substr)
{
int f=0;
int i,j=0,k,l,l1;
l = strlen(str);
l1 = strlen(substr);
for(i=0;i<l;i++)
{
if(str[i]!=substr[0])
continue;
else
{
for(j=0;j<l1;j++)
{
if(str[i+j] == substr[j])
{
if(j+1 == l1)
f++;
else
continue;
}
else
break;
}
}
}
return f;
}