标题:C 程序运行报错求助
取消只看楼主
hujianquan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-5-27
结帖率:0
 问题点数:0 回复次数:0 
C 程序运行报错求助
各位老师,我的程序如下,问题出现在当全局变量#define N 100000以上时总是报错,我想算更大,这问题怎么解决呢?报错:Unhandled exception at 0x002816a2 in Percolation.exe: 0xC0000005: Access violation reading location 0xccccccd4.
我用的是VS 2010编译。谢谢!
程序代码:
#include< stdio.h >
#include< math.h >
#include< malloc.h >
#include< stdlib.h >
#include< time.h >
#define LEN_np sizeof( struct np )
#define LEN_node sizeof( struct node )
#define LEN_np_kind sizeof( struct np_kind )
#define LEN_node_set sizeof( struct node_set )
#define alpha 0  //参数 alpha
#define N 100000 //系统规模
#define r 2 * N  //加的变数目

long extern npmax = 1;
//节点信息
struct node{
    long node_index;
    long node_size;//集团规模
    long color;
    struct node *node_next;
};
//集团类数目信息
struct np{
    long np_size;//npi
    long ng_size;//ngi
    struct np *np_next;
};

struct node_set{
    long node_ind;
    struct node_set *node_set_next;
};

//集团信息
struct np_kind
{
    long np_color; //集团编号
    struct node_set *node_set_point; //节点编号集合
    struct np_kind * np_kind_next;
};

void main( )
{
    int startt,finishh;
    struct node *Adj[ N ];
    struct np *NP, *head_NP, *break_NP;
    struct np_kind *Information, *Information_temp, *Information_last, *head_Information;
    struct node_set *node_set_temp;

    int i, k;//i在初始化邻接矩阵时用,j在初始化NP时用, k在添边时使用
    long *Select( long *select_node, struct np *NP, struct node *A[ N ] );//声明选点函数
    struct np_kind *Update_Information( struct np_kind * head_Information, struct node*Adj[],long *edge_point );//声明更新Information函数
    long Update_Adj( struct node *Adj[ ], struct np_kind  *head_Information, long *edge_point ); //更新Adj
    struct np* Update_NP( struct node *Adj[ ], struct np *head_NP, long *edge_point ); //更新NP

    long break_condition;//跳出循环
    long edge[ 2 ];//保存被选择的点

    long *edge_point;
    long node_size_temp; //新生成的集团的规模
    edge[ 0 ] = 0;
    edge[ 1 ] = 0;
    srand( ( unsigned )time( NULL ) );  //初始化随机数发生器种子
    startt = clock();/////////////////////////////////////////////////////时间起点

    //开辟链表并初始化邻接矩阵
    for( i = 0; i < N; i++ )
    {
        Adj[ i ] = ( struct node* )malloc( LEN_node );
        Adj[ i ] -> node_index = i + 1;
        Adj[ i ] -> node_size = 1;
        Adj[ i ] -> color = i + 1;
        Adj[ i ] -> node_next = NULL;

        Information_temp = ( struct np_kind* )malloc( LEN_np_kind );
        node_set_temp =  ( struct node_set* )malloc( LEN_node_set );
        node_set_temp -> node_ind = i + 1;
        node_set_temp -> node_set_next = NULL;

        Information_temp -> np_color = i + 1;
        Information_temp -> node_set_point = node_set_temp;
        Information_temp -> np_kind_next = NULL;


        if ( i == 0 )
        {
            Information = Information_temp;
        }
        else
        {
            Information_last -> np_kind_next = Information_temp; 
        }

            Information_last = Information_temp;
    }
            head_Information = Information;
            
    //初始化NP
    NP =( struct np* )malloc( LEN_np );
    NP -> np_size = 1;
    NP -> ng_size = N;
    NP -> np_next = NULL;
    head_NP = NP;
    
    
    break_condition = 0;
    for ( k = 0; k < r; k ++ )
    {
        edge_point = Select( edge, head_NP, Adj );
        //测试edge_point
        //printf( "final selected nodes indexes are: %d\t%d\n",edge_point[ 0 ], edge_point[ 1 ] );
        //测试edge_point

        
    
        //更新NP,Information
    if(  Adj[ edge_point[ 0 ] ] -> color != Adj[ edge_point[ 1 ] ] -> color )
    {
        //更新Information
        head_Information = Update_Information(  head_Information, Adj,edge_point );
        head_NP = Update_NP( Adj, head_NP, edge_point ); //更新NP
    }
    /*//测试Information
        test_head_Information = head_Information;
        printf("the Information is:\n");
        while( test_head_Information -> np_kind_next != NULL )
    {
        node_set_temp = test_head_Information -> node_set_point;
        printf( "the np_color is: %d\t", test_head_Information -> np_color );
        printf( "the node_ind is: \t"); 
        system( "pause" );
        while( node_set_temp -> node_set_next != NULL )
        {
            printf( "%d\t",node_set_temp-> node_ind);
             system( "pause" );
            node_set_temp = node_set_temp -> node_set_next;
        }
         printf( "%d\t",node_set_temp-> node_ind);
         printf( "\n");
         system( "pause" );
        test_head_Information =test_head_Information -> np_kind_next ;
    }
    node_set_temp = test_head_Information -> node_set_point;
    printf( "the np_color is: %d\t", test_head_Information -> np_color );
    system( "pause" );
    printf( "the node_ind is: \t");
    while( node_set_temp -> node_set_next!= NULL )
    {
        printf( "%d\t",node_set_temp->node_ind);
         system( "pause" );
         node_set_temp = node_set_temp -> node_set_next;
    }
    printf( "%d\t",node_set_temp->node_ind);
    printf( "\n");
    system("pause");//测试Information





        //测试NP
    test_head_NP = head_NP;
    printf("the NP is:\n" );
    printf("np_size \t  ng_size \t \n" );
    while( test_head_NP -> np_next != NULL )
    {
        printf(" %d \t %d \t \n" ,test_head_NP -> np_size, test_head_NP -> ng_size );
        test_head_NP =test_head_NP -> np_next;
    }
    printf(" %d \t %d \t \n" ,test_head_NP -> np_size, test_head_NP -> ng_size );
    
    system("pause");
        //测试NP
        */

        //更新Adj
        node_size_temp = Update_Adj( Adj,head_Information, edge_point); //更新Adj
    /*    //测试Adj
    printf(" The Adj is:\n");
    for( i = 0; i < N; i ++ )
    {
      test_Adj = Adj[ i ];
      printf(" %d-th node adject's information is : \n", i );
      printf("node_index \t color \t node_size \n");
      while( test_Adj -> node_next != NULL )
      {
          printf("%d \t %d \t %d\n", test_Adj -> node_index, test_Adj -> color,test_Adj -> node_size  );
          test_Adj = test_Adj -> node_next ;
      }

 
          printf("%d \t %d \t %d\n", test_Adj -> node_index, test_Adj -> color,test_Adj -> node_size  );
    }
    system("pause");
    //测试Adj
    */

        if( ( node_size_temp > npmax ) || ( node_size_temp == npmax ) )
        {
            npmax = node_size_temp;
        }
        
        printf( "the npmax is:%d\n", npmax );
            //system( "pause" );

            //结束循环,即停止加边
        break_NP = head_NP;
        break_condition = 0;
        while( break_NP -> np_next != NULL )
        {
            break_condition = break_condition + break_NP -> ng_size;
            break_NP = break_NP -> np_next;
        }
        break_condition = break_condition + break_NP -> ng_size;
        
        if ( break_condition == 1 )
            break;


    //system("pause");

    }
    
  finishh = clock();
  printf( " time is: %f", finishh - startt );
  system( "pause" );
};


//选点函数
long *Select( long *select_node, struct np* head_NP , struct node  *A[ ])
{ 
    double Get_random(   );
    long Node_position( double rand_value, struct np *head_NP, struct node * A[ ], int i_order, int node_index1 );
    double rand_value;
    //选择两个点
    int i;/////////////////////////////////////////////
    
    for ( i = 0; i < 2; i ++ )
    {
        rand_value = Get_random(   );
        if( i == 0)
        {
            /*printf( "the first rand_value is: %f\n",rand_value );
            system( "pause" );*/
            select_node[ i ] = Node_position( rand_value, head_NP, A, 0, 0 );
            //printf( "the first node is: %d\n",select_node[ i ] );//测试
        }
        if( i == 1 )
        {
            /*printf( "\nthe second rand_value is: %f\n",rand_value );
            system( "pause" );*/
            select_node[ i ] = Node_position( rand_value,head_NP, A, 1, select_node[ 0 ] );
            //printf( "\nthe second node is: %d\n",select_node[ i ] );//测试
            
        }
    }
    return ( select_node );
}
    



 //产生随机数的函数
double Get_random(   )
{
    double rand_value;  
    rand_value = rand()/(double)(RAND_MAX);
    return( rand_value );
}


//确定被选择的节点的标号
long Node_position( double rand_value, struct np *head_NP, struct node  *A[ ], int i_order, int node_index1 )
{
    struct np *temp1,*temp2;//分别在计算集团种类的平均值,几率归一化因子时使用;
    struct node *temp3;
    int i, k, m; //i统计集团种类,k判断节点编号,m用来初始化A_temp
    double  x, temp_x, sum_position, normalization_temp, normalization_temp_x, normalization2;//normalization,x在计算几率归一化时使用,sum_Np在计算集团种类的平均值时使用,
    static double sum_Np, normalization;
    //sum_position在确定节点的位置时使用
    long A_temp[ N ];
    

    
    
    if ( i_order == 0 )
    {
        //计算集团种类的平均值
        temp1 = head_NP;
        sum_Np = 0;
        i = 0;
        while( temp1 -> np_next != NULL )
        {
            sum_Np = sum_Np + temp1 -> np_size;
            i = i + 1;
            temp1 = temp1 -> np_next;
        }
        i = i + 1;
        sum_Np = sum_Np + temp1 -> np_size;
        sum_Np = sum_Np / i;
        sum_Np = sum_Np / npmax;

        

        //计算归一化系数
        temp2 = head_NP;
        normalization  = 0;
        while( temp2 -> np_next != NULL )
        {
            x = ( double )temp2 -> np_size / ( double ) npmax;
            temp_x = -( double )alpha * ( x - sum_Np ) * ( x - sum_Np );
            normalization = normalization + x * exp( temp_x ) * temp2 -> ng_size;
            temp2 = temp2 -> np_next;
        }
        x = ( double )temp2 -> np_size / ( double )npmax;
        temp_x = -( double )alpha * ( x - sum_Np ) * ( x - sum_Np );
        normalization = normalization + x * exp( temp_x ) * temp2 -> ng_size;

    
        //确定节点的位置
        sum_position = 0;    
        for( k = 0; k < N; k ++ )
        {   
            x = ( double )A[ k ] -> node_size / ( double )npmax;
            temp_x = -alpha * ( x - sum_Np ) * ( x - sum_Np );
        
            sum_position = sum_position + ( double )1 / ( double )npmax *exp( temp_x ) / normalization;
        
            if ( ( sum_position > rand_value) || ( sum_position == rand_value ) )
                break;

        }

    }

    else  //选择第二个点
    {
        //初始化A_temp矩阵
        for ( m = 0; m < N; m ++ ) 
        {
            A_temp[ m ] = m + 1;
        }
        
        //找出已经存在连接关系的点并置0
        temp3 = A[ node_index1 ];
        normalization_temp = 0;
        while( temp3 -> node_next != NULL )
        {
            
            A_temp[ ( temp3 -> node_index ) - 1 ]  = 0;
            x = ( double )temp3 -> node_size /  ( double )npmax;
            normalization_temp_x = -alpha * ( x - sum_Np ) * ( x - sum_Np );
            normalization_temp = normalization_temp + ( double )1 / npmax *exp( normalization_temp_x );
            temp3 = temp3 -> node_next;
        }
            
            A_temp[ temp3 -> node_index - 1 ]  = 0;
            x = ( double )temp3 -> node_size / ( double )npmax;
            normalization_temp_x = -( double )alpha * ( x - sum_Np ) * ( x - sum_Np );
            normalization_temp = normalization_temp + ( double )1 / npmax *exp( normalization_temp_x );
            normalization2 = normalization - normalization_temp;

            /*//测试A_temp
             printf(" A_temp are:");
            for( m = 0; m < N; m ++ )
                printf(" %d\t", A_temp[ m ]);
            //测试A_temp*/
           
        //确定节点的位置
        sum_position = 0;    
        for( k = 0; k < N; k ++ )
        {   
            if( A_temp[ k ] == 0 )
                continue;

            else
            {
                x = ( double )A[ k ] -> node_size / ( double )npmax;
                temp_x = -( double )alpha * ( x - sum_Np ) * ( x - sum_Np );
        
                sum_position = sum_position + ( double )1 / npmax *exp( temp_x ) / normalization2;
        
                if ( ( sum_position > rand_value) || ( sum_position == rand_value ) )
                    break;
            }
            
        }
    }
    return( k );
}


//更新Information
struct np_kind *Update_Information( struct np_kind * head_Information, struct node*Adj[],long *edge_point )
{
    struct node *p1, *p2;
    struct node_set *node_set_temp1, *node_set_temp3, *node_set_temp4;
    long color_temp;
    struct np_kind *Information_temp,*Information_temp1, *Information_before;
    
    p1 = Adj[ edge_point[ 0 ] ];
    p2 = Adj[ edge_point[ 1 ] ];

    Information_temp = head_Information;
    if( ( p1 -> color > p2 -> color )  )
    {
        color_temp = p2 -> color;
            
        while( Information_temp -> np_kind_next != NULL ) //先找到颜色标号大的集团位置
        {
            if( Information_temp -> np_color == p1 -> color )
                break;
            Information_before = Information_temp;
            Information_temp = Information_temp -> np_kind_next;
        }

        Information_temp1 = head_Information;//找到颜色标号小的位置
        while( Information_temp1 -> np_kind_next != NULL )
        {

            if( Information_temp1 -> np_color == p2 -> color )
                break;
            Information_temp1 = Information_temp1 -> np_kind_next;
        }
            
        node_set_temp1 = Information_temp -> node_set_point;
        node_set_temp4 = Information_temp1 -> node_set_point;
        while( node_set_temp4 -> node_set_next != NULL )
        {
            node_set_temp4 = node_set_temp4 -> node_set_next;
        }
            
            
        while( node_set_temp1 -> node_set_next != NULL)
        {
            node_set_temp3 = ( struct node_set* )malloc( LEN_node_set );
            node_set_temp3 -> node_ind = node_set_temp1 -> node_ind;
            node_set_temp3 -> node_set_next = NULL;
            node_set_temp4 -> node_set_next = node_set_temp3;
            node_set_temp4 = node_set_temp3;
            node_set_temp1 = node_set_temp1 -> node_set_next;
        }
            node_set_temp3 = ( struct node_set* )malloc( LEN_node_set );
            node_set_temp3 -> node_ind = node_set_temp1 -> node_ind;
            node_set_temp3 -> node_set_next = NULL;
            node_set_temp4 -> node_set_next = node_set_temp3;

        //删除颜色大集团的节点
                Information_before -> np_kind_next = Information_temp -> np_kind_next;
    }


    if( ( p1 -> color < p2 -> color )  )
    {
        color_temp = p1 -> color;
        while( Information_temp -> np_kind_next != NULL) //先找到颜色标号大的集团位置
        {
            if( Information_temp -> np_color == p2 -> color )
                break;
            Information_before = Information_temp;
            Information_temp = Information_temp -> np_kind_next;
        }

        Information_temp1 = head_Information;//找到颜色标号小的位置
        while( Information_temp1 -> np_kind_next != NULL )
        {

            if( Information_temp1 -> np_color == p1 -> color )
                break;
            Information_temp1 = Information_temp1 -> np_kind_next;
        }
            
        node_set_temp1 = Information_temp -> node_set_point;
        node_set_temp4 = Information_temp1 -> node_set_point;
        while( node_set_temp4 -> node_set_next != NULL )
        {
            node_set_temp4 = node_set_temp4 -> node_set_next;
        }
            
            
        while( node_set_temp1 -> node_set_next != NULL)
        {
            node_set_temp3 = ( struct node_set* )malloc( LEN_node_set );
            node_set_temp3 -> node_ind = node_set_temp1 -> node_ind;
            node_set_temp3 -> node_set_next = NULL;
            node_set_temp4 -> node_set_next = node_set_temp3;
            node_set_temp4 = node_set_temp3;
            node_set_temp1 = node_set_temp1 -> node_set_next;
        }
            node_set_temp3 = ( struct node_set* )malloc( LEN_node_set );
            node_set_temp3 -> node_ind = node_set_temp1 -> node_ind;
            node_set_temp3 -> node_set_next = NULL;
            node_set_temp4 -> node_set_next = node_set_temp3;

        //删除颜色大集团的节点
                Information_before -> np_kind_next = Information_temp -> np_kind_next;

    }

 return( head_Information );
}


//更新NP
struct np* Update_NP( struct node *Adj[ ], struct np *head_NP, long *edge_point ) //更新NP
{
    struct node *p1, *p2;
    struct np *temp_head_NP, *temp1_head_NP, *temp2_head_NP, *temp3_head_NP, *temp4_head_NP, *temp5_head_NP;
    int isexist;
    long temp_np_size;

    p1 = Adj[ edge_point[ 0 ] ];
    p2 = Adj[ edge_point[ 1 ] ]; 

    temp_np_size = p1 -> node_size + p2 -> node_size;
    //先操作新生成的集团 
    temp_head_NP = head_NP;

    isexist = 0;
    while( temp_head_NP -> np_next != NULL )
    {
        if( temp_head_NP -> np_size == temp_np_size )
        {
            isexist = 1;
            break;
        }

        temp_head_NP = temp_head_NP -> np_next;
    }

    if( temp_head_NP -> np_size == temp_np_size )
        {
            isexist = 1;
        }
    //printf( "isexist is :%d", isexist );
    //system("pause");

    if ( isexist == 0 ) //说明是新生成的集团类型
    {
        temp1_head_NP = ( struct np* )malloc( LEN_np );
        temp1_head_NP -> np_size = temp_np_size;
        temp1_head_NP -> ng_size = 1;
        temp1_head_NP -> np_next = NULL;
        temp_head_NP -> np_next = temp1_head_NP;
    /*    //测试NP
    test_head_NP = head_NP;
    printf("the NP----->>>>isexist == 0 is:\n" );
    printf("np_size \t  ng_size \t \n" );
    while( test_head_NP -> np_next != NULL )
    {
        printf(" %d \t %d \t \n" ,test_head_NP -> np_size, test_head_NP -> ng_size );
        test_head_NP =test_head_NP -> np_next;
    }
    printf(" %d \t %d \t \n" ,test_head_NP -> np_size, test_head_NP -> ng_size );
    
    system("pause");
        //测试NP
        */
    }


    if( isexist == 1 )//说明是已经存在的集团
    {
        temp_head_NP -> ng_size = temp_head_NP-> ng_size  + 1;
    }

    //开始选择被选择的集团
    //第一个集团
    temp2_head_NP = head_NP;
    if ( ( temp2_head_NP -> np_size == p1 -> node_size ) && (  ( temp2_head_NP -> ng_size - 1 ) == 0) )
        {
            head_NP = temp2_head_NP -> np_next;
        }

    else
    {
        while( temp2_head_NP -> np_size != p1 -> node_size )
            {
                temp3_head_NP = temp2_head_NP;
                temp2_head_NP = temp2_head_NP -> np_next;
            }


        temp2_head_NP -> ng_size = temp2_head_NP -> ng_size - 1;


        if(  temp2_head_NP -> ng_size == 0  )
            {
                temp3_head_NP -> np_next = temp2_head_NP -> np_next;
            }
    }

/*//测试NP
    test_head_NP = head_NP;
    printf("the NPppppppp is:\n" );
    printf("np_size \t  ng_size \t \n" );
    while( test_head_NP -> np_next != NULL )
    {
        printf(" %d \t %d \t \n" ,test_head_NP -> np_size, test_head_NP -> ng_size );
        test_head_NP =test_head_NP -> np_next;
    }
    printf(" %d \t %d \t \n" ,test_head_NP -> np_size, test_head_NP -> ng_size );
    
    system("pause");
        //测试NP
    */

    //第二个集团
    temp4_head_NP = head_NP;

    if ( ( temp4_head_NP -> np_size == p2 -> node_size ) && ( 0 == temp4_head_NP -> ng_size - 1))
        {
            head_NP = temp4_head_NP -> np_next;
        }
    else
        {
        while( temp4_head_NP -> np_size != p2 -> node_size )
            {
                temp5_head_NP = temp4_head_NP;
                temp4_head_NP = temp4_head_NP -> np_next;
            }
        temp4_head_NP -> ng_size = temp4_head_NP -> ng_size - 1;

        if( temp4_head_NP -> ng_size == 0 )
            {
                temp5_head_NP -> np_next  = temp4_head_NP -> np_next;
            }
        }

    /*test_head_NP = head_NP;
    printf("the NPnnnnnnnnnnnnn is:\n" );
    printf("np_size \t  ng_size \t \n" );
    while( test_head_NP -> np_next != NULL )
    {
        printf(" %d \t %d \t \n" ,test_head_NP -> np_size, test_head_NP -> ng_size );
        test_head_NP =test_head_NP -> np_next;
    }
    printf(" %d \t %d \t \n" ,test_head_NP -> np_size, test_head_NP -> ng_size );
    
    system("pause");
    */
    return( head_NP );
}

///更新Adj
long Update_Adj( struct node *Adj[ ], struct np_kind  *head_Information, long *edge_point )
{
    struct node *p1, *p2, *p3, *p4, *p5;
    struct np_kind  *temp_Information;
    struct node_set *temp_node_set;

    long node_size_temp, color_temp;
    p1 = Adj[ edge_point[ 0 ] ];
    p2 = Adj[ edge_point[ 1 ] ];

        //  更新Adj
        p3 = ( struct node* ) malloc( LEN_node );
        p3 -> node_index = p2-> node_index;
        p3 -> node_size = p2 -> node_size;
        p3 -> color = p2 -> color;
        p3 -> node_next = NULL;

        p4 = ( struct node* ) malloc( LEN_node );
        p4 -> node_index = p1 -> node_index;
        p4 -> node_size = p1 -> node_size;
        p4 -> color = p1 -> color;
        p4 -> node_next = NULL;
        
        while( p1 -> node_next != NULL )
        {
            p1 = p1 -> node_next;
        }
        p1 -> node_next = p3;

        
        while( p2 -> node_next != NULL )
        {
            p2 = p2 -> node_next;
        }
        p2 -> node_next = p4;


    p1 = Adj[ edge_point[ 0 ] ];
    p2 = Adj[ edge_point[ 1 ] ];
    node_size_temp = npmax;
    
    if( p1 -> color != p2 -> color ) //集团之间加边
    {
        node_size_temp = p1 -> node_size + p2 -> node_size;

        if( ( p1 -> color > p2 -> color ) || ( p1 ->color == p2 -> color ) )
        {
            color_temp = p2 -> color;
        }
        else
        {
            color_temp = p1 -> color;
        }

        temp_Information = head_Information;
        while( temp_Information -> np_color  != color_temp )//找出颜色标号小的集团在Informatiom中的位置
        {
            temp_Information = temp_Information -> np_kind_next;
        }
        temp_node_set = temp_Information -> node_set_point;

        while( temp_node_set -> node_set_next != NULL )//找出颜色标号小的集团中节点标号的最后位置
        {
            p5 = Adj[ temp_node_set -> node_ind - 1 ];
            while(  p5 -> node_next != NULL )
            {
                p5 -> color = color_temp;
                p5 -> node_size = node_size_temp;
                p5 = p5 -> node_next;
            }
            p5 -> color = color_temp;
            p5 -> node_size = node_size_temp;

            temp_node_set = temp_node_set -> node_set_next;

        }

        p5 = Adj[ temp_node_set -> node_ind - 1 ];
        while(  p5 -> node_next != NULL )
        {
            p5 -> color = color_temp;
            p5 -> node_size = node_size_temp;
            p5 = p5 -> node_next;
        }
        p5 -> color = color_temp;
        p5 -> node_size = node_size_temp;

    }


    return( node_size_temp );
    }
搜索更多相关主题的帖子: reading color 
2014-06-11 19:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-432800-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.575045 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved