Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Malloc returning segmentation fault

Reply
Thread Tools

Malloc returning segmentation fault

 
 
Dawn Minnis
Guest
Posts: n/a
 
      03-15-2005
Hey guys - this code when called with parameters:

driver.o n n 12 12 12 12 12 12 2.6 3.2

is kicking back a segmentation fault.

I've read the rest of the postings but am still confused. Can someone
take a look and tell me how to fix it - please dont be like the guy I
spoke to today and tell me that I am not allocating the memory
correctly and then walk off. Please, if possible provide me with
solutions or at least suggestions to try out.

Thanks

Dawn

/*driver: used to handle arguments*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>


/* define the external functions */
extern void callhelp();
//extern void getparams(int argc, char *argv[], double params[]);
extern void buildMatrix(double *arrayX, int dim1, int dim2);
extern void myMxM(char *transA, char *transB, int *m, int *n, int *k,
double *alpha, double *arrayA, int *lda, \
double *arrayB, int *ldb, double *beta, double *arrayC, int *ldc);

extern void dgemm_(char *transA, char *transB, int *m, int *n, int *k,
double *alpha, double *arrayA, int *lda, \
double *arrayB, int *ldb, double *beta, double *arrayC, int *ldc);

extern void lsame_(char*,char*);
extern void xerbla_(char*,int*);

int main(int argc, char *argv[])
{
int c, flag, m, n, k, lda, ldb, ldc, d, e, f, arrayDim=0,
tempArrayDim=0;
int *ptrM, *ptrN, *ptrK, *ptrLda, *ptrLdb, *ptrLdc;
char transA, transB;
double alpha, beta;
double *arrayA, *arrayB, *arrayC, *tempArrayC, *ptrAlpha, *ptrBeta;
char *endptr, *ptrTransA, *ptrTransB;

struct timeval start, stop;

while (--argc > 0 && (*++argv)[0] == '-')
{
while (c = *++argv[0])
{ flag = 0;
switch (c)
{
case 'h':
/*printf("\ncall help file");*/
callhelp();
argc = 0;
flag = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
flag = 1;
break;
}
}//end inner while
}//end outer while


//Check for correct number of parameters
if(argc != 10 && flag != 1)
{
printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
printf("Incorrect number of parameters. Run program again with option
-h for help.\n");
printf("*-*-*-*-*- ERROR -*-*-*-*-*\n\n");
}
else if(argc == 10 && flag != 1)
{

transA = *argv[0];
transB = *argv[1];
m = atoi(argv[2]);
n = atoi(argv[3]);
k = atoi(argv[4]);
lda = atoi(argv[5]);
ldb = atoi(argv[6]);
ldc = atoi(argv[7]);
alpha=strtod(argv[8], &endptr);
beta=strtod(argv[9], &endptr);

if((m == 0 || n == 0 || k == 0 || (alpha == 0 && beta == 0)) && flag
!= 1)
{ //values are equal to zero
printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
printf("Result will be zero with the parameters you provided\nRun
program again with option -h for help.\n");
printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
flag = 1;
}
else if(((lda < m || lda < 1) || (ldb < k || ldb < 1) || (ldc < m ||
ldc < 1)) && flag != 1)
{ //values for lda, ldb, ldc are less than 1 or m, n, k
printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
printf("One of LDA, LDB, LDC of incorrect length. Run program again
with option -h for help.\n");
printf("*-*-*-*-*- ERROR -*-*-*-*-*\n\n");
flag = 1;
}
else if((transA != 'n' && transA != 'N' && transA != 't' && transA
!= 'T' ) && flag != 1)
{
//transA not one of n or t
printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
printf("TRANS A not one of: n, N, t, T. Run program again with
option -h for help.\n");
printf("*-*-*-*-*- ERROR -*-*-*-*-*\n\n");
flag = 1;
}
else if((transB != 'n' && transB != 'N' && transB != 't' && transB
!= 'T') && flag != 1)
{
//transB nott one of n or t
printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
printf("TRANS B not one of: n, N, t, T. Run program again with
option -h for help.\n");
printf("*-*-*-*-*- ERROR -*-*-*-*-*\n\n");
flag = 1;
}
else if(flag != 1)//all is well, proceed.
{


//Check whether matrix A and B are transposed
//then build the array's accordingly
if(transA == 'n' || transA == 'N')
{
if(transB == 'n' || transB == 'N')
{
//both A and B are non-trans
//printf("Initial A\n");
arrayDim = lda*k;
fprintf(stderr, "allocating memory to ArrayA\n");
fprintf (stderr, "dimension: %d\n", arrayDim);
arrayA= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayA, lda, k);

//printf("Initial B\n");
arrayDim = ldb*n;
fprintf(stderr, "allocating memory to ArrayB\n");
fprintf (stderr, "dimension: %d\n", arrayDim);
arrayB= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayB, ldb, n);

//printf("Initial C\n");
arrayDim = ldc*n;
fprintf(stderr, "allocating memory to ArrayC\n");
fprintf (stderr, "dimension: %d\n", arrayDim);
arrayC= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayC, ldc, n);

/*printf("after build A contains = %f\n", arrayA[1]);
printf("after build B contains = %f\n", arrayB[1]);
printf("after build C contains = %f\n", arrayC[1]);*/
}
else if(transB == 't' || transB == 'T')
{
//A is non-trans, B is trans
//printf("Initial A\n");
arrayDim = lda*k;
arrayA= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayA, lda, k);

//printf("Initial B\n");
arrayDim = ldb*k;
arrayB= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayB, ldb, k);

//printf("Initial C\n");
arrayDim = ldc*n;
arrayC= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayC, ldc, n);
}
}
else if(transA == 't' || transA == 'T')
{
if(transB == 'n' || transB == 'N')
{
//A is trans, B is non-trans
//printf("Initial A\n");
arrayDim = lda*m;
arrayA= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayA, lda, m);
//printf("A0 now contains arrayA[%f]", arrayA[0]);

//printf("Initial B\n");
arrayDim = ldb*n;
arrayB= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayB, ldb, n);

//printf("Initial C\n");
arrayDim = ldc*n;
arrayC= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayC, ldc, n);
}
else if(transB == 't' || transB == 'T')
{
//both A and B are trans
//printf("Initial A\n");
arrayDim = lda*m;
arrayA= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayA, lda, m);

//printf("Initial B\n");
arrayDim = ldb*k;
arrayB= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayB, ldb, k);

//printf("Initial C\n");
arrayDim = ldc*n;
arrayC= (double *)malloc(sizeof(double)*arrayDim);
buildMatrix(arrayC, ldc, n);
}
}


tempArrayDim = ldc*n;
fprintf(stderr, "allocating memory to TempArrayC\n");
fprintf (stderr, "dimension: %d\n", tempArrayDim);
tempArrayC= (double *)malloc(sizeof(double)*tempArrayDim);

//buildMatrix(tempArrayC, ldc, n);
for(f=0; f<tempArrayDim; f++)
{
tempArrayC[f] = arrayC[f];
}

//generate pointers to the values
ptrTransA = &transA;
ptrTransB = &transB;

ptrM = &m;
ptrN = &n;
ptrK = &k;

ptrLda = &lda;
ptrLdb = &ldb;
ptrLdc = &ldc;

ptrAlpha = &alpha;
ptrBeta = &beta;

start.tv_usec=0; stop.tv_usec=0;
gettimeofday(&start,NULL);

myMxM(ptrTransA, ptrTransB, ptrM, ptrN, ptrK, ptrAlpha, arrayA,
ptrLda, arrayB, ptrLdb, ptrBeta, arrayC, ptrLdc);


gettimeofday(&stop,NULL);
printf("start =%i..stop =%i..result
=%i..(microsec)\n",start.tv_usec,
stop.tv_usec,stop.tv_usec-start.tv_usec);

e=ldc*n;
printf("myMxM resulting C matrix\n");
for(d=0; d<e; d++)
{
printf("contents = %.8f\n", arrayC[d]);
}

for(f=0; f<tempArrayDim; f++)
{
arrayC[f] = tempArrayC[f];
}
start.tv_usec=0; stop.tv_usec=0;
gettimeofday(&start,NULL);

dgemm_(ptrTransA, ptrTransB, ptrM, ptrN, ptrK, ptrAlpha, arrayA,
ptrLda, arrayB, ptrLdb, ptrBeta, arrayC, ptrLdc);

gettimeofday(&stop,NULL);
printf("start =%i..stop =%i..result
=%i..(microsec)\n",start.tv_usec,
stop.tv_usec,stop.tv_usec-start.tv_usec);

e=ldc*n;
printf("dgemm resulting C matrix\n");
for(d=0; d<e; d++)
{
printf("contents = %.8f\n", arrayC[d]);
}

}

}//close if(argc>1)


return 0;
}
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      03-15-2005
Dawn Minnis wrote:
> Hey guys - this code when called with parameters:
>
> driver.o n n 12 12 12 12 12 12 2.6 3.2
>
> is kicking back a segmentation fault.
>
> I've read the rest of the postings but am still confused. Can someone
> take a look and tell me how to fix it - please dont be like the guy I
> spoke to today and tell me that I am not allocating the memory
> correctly and then walk off. Please, if possible provide me with
> solutions or at least suggestions to try out.
>
> Thanks
>
> Dawn
>

[snip: lengthy code segment]

Sorry, your code does not compile for me, as I do not have
the prototyped functions and <sys/time.h> is no standard header.

Please provide a compiling minimal example. If it is still
too long to post here, consider offering the source for download
and posting a link to it.
This helps the people round here helping you.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
dawn.minnis@gmail.com
Guest
Posts: n/a
 
      03-15-2005

Michael Mair wrote:
> Dawn Minnis wrote:
> > Hey guys - this code when called with parameters:
> >
> > driver.o n n 12 12 12 12 12 12 2.6 3.2
> >
> > is kicking back a segmentation fault.
> >
> > I've read the rest of the postings but am still confused. Can

someone
> > take a look and tell me how to fix it - please dont be like the guy

I
> > spoke to today and tell me that I am not allocating the memory
> > correctly and then walk off. Please, if possible provide me with
> > solutions or at least suggestions to try out.
> >
> > Thanks
> >
> > Dawn
> >

> [snip: lengthy code segment]
>
> Sorry, your code does not compile for me, as I do not have
> the prototyped functions and <sys/time.h> is no standard header.
>
> Please provide a compiling minimal example. If it is still
> too long to post here, consider offering the source for download
> and posting a link to it.
> This helps the people round here helping you.
>
>
> Cheers
> Michael
> --
> E-Mail: Mine is an /at/ gmx /dot/ de address.


Hey Michael

Sorry for the confusion - but I'm a bonefide dimwit. In one of the
functions I was using malloc and multiplying by the dimension "0". Yes
zero, not a genuine dimension that was nicely passed in, no no, zero.

I am living proof you should never try and code or debug when tired.

Thanks again for your time and patience - and such a quick response.

Dawn

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault, malloc error mathieu.dutour@gmail.com C Programming 7 11-13-2007 09:37 AM
Why Segmentation fault in malloc( ) after calling several realloc( )? I_have_nothing C Programming 6 06-15-2005 07:17 AM
When I call malloc() to get some space, I get Segmentation fault Zheng Da C Programming 3 06-02-2005 08:54 PM
Segmentation fault in mallopt/malloc call Alexandre C Programming 7 03-06-2005 10:40 AM
malloc segmentation fault Bin Lu C Programming 9 10-23-2003 09:21 PM



Advertisments