Thread: 0xC0000005 When calling a function.

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    0xC0000005 When calling a function.

    Hello everyone,

    I'm new to C/C++. I'm trying to make a program that's going to use the CBLAS libraries that I downloaded on BLAS. After fighting tooth and nail with VC 2005 (I downgraded on purpose because at one point I was desperate.) with regards to solving compilation errors and such and eventually it all compiled just fine.
    The problem now is, I get the above mentioned error. It says: "Unhandled exception at 0x0040271c in Try.exe: 0xC0000005: Access violation reading location 0x4e18feb8."

    Now there are a few .cpp files (I'm compiling as C code.) which contain the functions and there is one other one which contains my main method. Using the debugger, it goes through 3 files all in all.

    The main file:
    Code:
    /* cblas_example2.c */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "cblas.h"
    #include "cblas_f77.h"
    
    
    #define INVALID -1
    
    
    int main (int argc, char **argv )
    {
       int rout=-1,info=0,m,n,k,lda,ldb,ldc;
       double A[2] = {0.0,0.0}, 
              B[2] = {0.0,0.0}, 
              C[2] = {0.0,0.0}, 
              ALPHA=0.0, BETA=0.0;
              
    
    
             printf("Checking if cblas_dgemm fails on parameter 11\n");
             cblas_dgemm( CblasRowMajor,  CblasNoTrans, CblasNoTrans, 0, 2, 2,
                       ALPHA, A, 1, B, 1, BETA, C, 1 );
              
       return 1;
    }
    Then, at cblas_dgemm we proceed to:

    Code:
    /* *
     * cblas_dgemm.c
     * This program is a C interface to dgemm.
     * Written by Keita Teranishi
     * 4/8/1998
     *
     */
    
    
    #include "cblas.h"
    #include "cblas_f77.h"
    void cblas_dgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA,
                     const enum CBLAS_TRANSPOSE TransB, const int M, const int N,
                     const int K, const double alpha, const double  *A,
                     const int lda, const double  *B, const int ldb,
                     const double beta, double  *C, const int ldc)
    {
       char TA, TB;   
    #ifdef F77_CHAR
       F77_CHAR F77_TA, F77_TB;
    #else
       #define F77_TA &TA  
       #define F77_TB &TB  
    #endif
    
    
    #ifdef F77_INT
       F77_INT F77_M=M, F77_N=N, F77_K=K, F77_lda=lda, F77_ldb=ldb;
       F77_INT F77_ldc=ldc;
    #else
       #define F77_M M
       #define F77_N N
       #define F77_K K
       #define F77_lda lda
       #define F77_ldb ldb
       #define F77_ldc ldc
    #endif
    
    
       int CBLAS_CallFromC;
       int RowMajorStrg;
       RowMajorStrg = 0;
       CBLAS_CallFromC = 1;
    
    
       if( Order == CblasColMajor )
       {
          if(TransA == CblasTrans) TA='T';
          else if ( TransA == CblasConjTrans ) TA='C';
          else if ( TransA == CblasNoTrans )   TA='N';
          else 
          {
             cblas_xerbla(2, "cblas_dgemm","Illegal TransA setting, %d\n", TransA);
             CBLAS_CallFromC = 0;
             RowMajorStrg = 0;
             return;
          }
    
    
          if(TransB == CblasTrans) TB='T';
          else if ( TransB == CblasConjTrans ) TB='C';
          else if ( TransB == CblasNoTrans )   TB='N';
          else 
          {
             cblas_xerbla(3, "cblas_dgemm","Illegal TransB setting, %d\n", TransB);
             CBLAS_CallFromC = 0;
             RowMajorStrg = 0;
             return;
          }
    
    
          #ifdef F77_CHAR
             F77_TA = C2F_CHAR(&TA);
             F77_TB = C2F_CHAR(&TB);
          #endif
    
    
          F77_dgemm(F77_TA, F77_TB, &F77_M, &F77_N, &F77_K, &alpha, A,
           &F77_lda, B, &F77_ldb, &beta, C, &F77_ldc);
       } else if (Order == CblasRowMajor)
       {
          RowMajorStrg = 1;
          if(TransA == CblasTrans) TB='T';
          else if ( TransA == CblasConjTrans ) TB='C';
          else if ( TransA == CblasNoTrans )   TB='N';
          else 
          {
             cblas_xerbla(2, "cblas_dgemm","Illegal TransA setting, %d\n", TransA);
             CBLAS_CallFromC = 0;
             RowMajorStrg = 0;
             return;
          }
          if(TransB == CblasTrans) TA='T';
          else if ( TransB == CblasConjTrans ) TA='C';
          else if ( TransB == CblasNoTrans )   TA='N';
          else 
          {
             cblas_xerbla(2, "cblas_dgemm","Illegal TransB setting, %d\n", TransB);
             CBLAS_CallFromC = 0;
             RowMajorStrg = 0;
             return;
          }
          #ifdef F77_CHAR
             F77_TA = C2F_CHAR(&TA);
             F77_TB = C2F_CHAR(&TB);
          #endif
    
    
          F77_dgemm(F77_TA, F77_TB, &F77_N, &F77_M, &F77_K, &alpha, B,
                      &F77_ldb, A, &F77_lda, &beta, C, &F77_ldc);
       } 
       else  cblas_xerbla(1, "cblas_dgemm", "Illegal Order setting, %d\n", Order);
       CBLAS_CallFromC = 0;
       RowMajorStrg = 0;
       return;
    }
    And finally to F77_dgemm which leads us to:

    Code:
    /* *     Written by D.P. Manley, Digital Equipment Corporation.
     *     Prefixed "C_" to BLAS routines and their declarations.
     *
     *     Modified by T. H. Do, 2/19/98, SGI/CRAY Research.
     */
    #include <stdlib.h>
    #include "cblas.h"
    #include "cblas_test.h"
    #define  TEST_COL_MJR    0
    #define  TEST_ROW_MJR    1
    #define  UNDEFINED     -1
    
    
    void F77_dgemm(int *order, char *transpa, char *transpb, int *m, int *n, 
                  int *k, double *alpha, double *a, int *lda, double *b, int *ldb,
                  double *beta, double *c, int *ldc ) {
    
    
      double *A, *B, *C;
      int i,j,LDA, LDB, LDC;
      enum CBLAS_TRANSPOSE transa, transb;
    
    
      get_transpose_type(transpa, &transa);
      get_transpose_type(transpb, &transb);
    
    
      if (*order == TEST_ROW_MJR) {
         if (transa == CblasNoTrans) {
            LDA = *k+1;
            A = (double *)malloc( (*m)*LDA*sizeof( double ) );
            for( i=0; i<*m; i++ )
               for( j=0; j<*k; j++ )
                  A[i*LDA+j]=a[j*(*lda)+i];
         }
         else {
            LDA = *m+1;
            A   = ( double* )malloc( LDA*(*k)*sizeof( double ) );
            for( i=0; i<*k; i++ )
               for( j=0; j<*m; j++ )
                  A[i*LDA+j]=a[j*(*lda)+i];
         }
         if (transb == CblasNoTrans) {
            LDB = *n+1;
            B   = ( double* )malloc( (*k)*LDB*sizeof( double ) );
            for( i=0; i<*k; i++ )
               for( j=0; j<*n; j++ )
                  B[i*LDB+j]=b[j*(*ldb)+i];
         }
         else {
            LDB = *k+1;
            B   = ( double* )malloc( LDB*(*n)*sizeof( double ) );
            for( i=0; i<*n; i++ )
               for( j=0; j<*k; j++ )
                  B[i*LDB+j]=b[j*(*ldb)+i];
         }
         LDC = *n+1;
         C   = ( double* )malloc( (*m)*LDC*sizeof( double ) );
         for( j=0; j<*n; j++ )
            for( i=0; i<*m; i++ )
               C[i*LDC+j]=c[j*(*ldc)+i];
    
    
         cblas_dgemm( CblasRowMajor, transa, transb, *m, *n, *k, *alpha, A, LDA,
                      B, LDB, *beta, C, LDC );
         for( j=0; j<*n; j++ )
            for( i=0; i<*m; i++ )
               c[j*(*ldc)+i]=C[i*LDC+j];
         free(A);
         free(B);
         free(C);
      }
      else if (*order == TEST_COL_MJR)
         cblas_dgemm( CblasColMajor, transa, transb, *m, *n, *k, *alpha, a, *lda,
                      b, *ldb, *beta, c, *ldc );
      else
         cblas_dgemm( UNDEFINED, transa, transb, *m, *n, *k, *alpha, a, *lda,
                      b, *ldb, *beta, c, *ldc );
        ;
    }
    I get the above-mentioned exception in the last line, or:

    Code:
     cblas_dgemm( UNDEFINED, transa, transb, *m, *n, *k, *alpha, a, *lda,
                      b, *ldb, *beta, c, *ldc );
    The debugger tells me what the address in the exception is the address if *ldc.

    What am I doing wrong?
    Any input will be greatly appreciated.
    I apologise for the large amount of code, but I wanted to provide sufficient information and hopefully I've done that.

    Thanks in advance.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are passing a constant into the function (type const int). Then you take the address of that (and pass to int*). This would by itself cause undefined behaviour because you get rid of const.
    You should make up your mind if you want to use C or C++. This is all C, but you post this in the C++ section.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Quote Originally Posted by Elysia View Post
    You are passing a constant into the function (type const int). Then you take the address of that (and pass to int*). This would by itself cause undefined behaviour because you get rid of const.

    First of all, why would this cause undefined behavior? I'm passing a constant when I need a constant and I'm passing an address when it's required. It all fits the definition of the functions.
    Second, what exactly do you suggest me to do? Get rid of all the const's or get rid of all the addresses?

    Thank you for your reply!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by LTKD View Post
    First of all, why would this cause undefined behavior? I'm passing a constant when I need a constant and I'm passing an address when it's required. It all fits the definition of the functions.
    You are removing constness. That is undefined behaviour. I am quite surprised this actually compiles. Are you compiling as C?

    Second, what exactly do you suggest me to do? Get rid of all the const's or get rid of all the addresses?
    That depends on whether you want this to be C or C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Yes, I'm compiling it as C. And I don't mind whether it's C or C++ I just want to get it to work. How do I go about it? Thanks a zillion.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by LTKD View Post
    Yes, I'm compiling it as C.
    With warnings, I bet. You should fix them.

    And I don't mind whether it's C or C++ I just want to get it to work.
    You should care. Either you write C, or you write C++. It's not an either or solution here. Mixing languages is bad. You need to make up your mind.

    How do I go about it? Thanks a zillion.
    To fix that problem, keep the constness chain. Change the pointer to const int*, or just keep it as const int if you don't need a pointer (I don't see why you do).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Fixed. Thank you. All I did was kept the constness chain.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-02-2011, 05:20 AM
  2. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  3. 0xc0000005 error
    By Hankyaku in forum C++ Programming
    Replies: 32
    Last Post: 03-14-2008, 04:20 PM
  4. Replies: 5
    Last Post: 07-13-2003, 01:01 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM

Tags for this Thread