472,958 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Segmentation fault using realloc()

Dear all,
I'm trying to implement the Smith-Waterman algorithm for DNA sequence
alignment. I'm aligning a 2040 bp query sequence against a 5040 bp
sequence. I'll be trying to implement a parallel version of it later,
that's why I have the MPI timer function to measure the speedup.

seq[0] and foundseq0 are the query sequence. size is the length of the
sequences.

The problem seems to occur when I'm trying to increase the memory
allocated to the 2 1D arrays foundseq0 and foundseq1. When compiling,
I get the warnings

warning: assignment makes pointer from integer without a cast
warning: assignment makes pointer from integer without a cast

for the lines

if (temp = realloc(foundseq0, sizeof(char) * (count + foundsize[0]))
== NULL)
and
if (temp = realloc(foundseq1, sizeof(char) * (count + foundsize[k]))
== NULL)

although temp, foundseq0 and foundseq1 are char pointers.

I run the program, and I get the "Segmentation Fault" error and the
program stops right after the
printf("A\n");
statement.

However, the program works fine for very small sequences, 14bp against
13bp, and there is no need to reallocate memory.

Please advise.

Below is the code:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

#define MAX_SEQ 6000 // maximum length of sequence
#define N 2 // total number of sequences

/* Assume that the number of sequences in the database to be compared
with the
query sequence is known, and that the size of each sequence is also
known. */

struct table
{
int previ;
int prevj;
double value;
};

int main(int argc, char** argv)
{
FILE *fin, *fin1;
char input[FILENAME_MAX], *foundseq0, *foundseq1, *temp;
static char seq[N][MAX_SEQ+1];
double max = 0.0, maxscore = 0.0, test, start, end;
int size[N], i = 0, k, j = 0, stop = 0, count = 1, c;
int row = 1, maxrow = 0, column = 1, maxcolumn = 0;
int prevrow, prevcol, foundsize[N];

struct table **cell;

MPI_Init(&argc, &argv);

fin = fopen("files3.txt", "r");
if (!fin)
{
printf("Input file cannot be opened!\n");
exit(0);
}

while (fscanf(fin, "%s", input) != EOF)
{
if (i >= N)
{
printf("Number of sequences is more than that allowed!\n");
exit(0);
}
fin1 = fopen(input, "r");
if (!fin1)
{
printf("Input sequence file cannot be opened!\n");
exit(0);
}
printf("%s\n", input);
fscanf(fin1, "%d ", &size[i]);
printf("%d\n", size[i]);
while ((c = fgetc(fin1)) != EOF)
{
if (c != '\n')
{
if (j >= MAX_SEQ)
{
printf("Sequence is longer than that allowed!\n");
exit(0);
}
seq[i][j] = c;
j++;
}
}
printf("\n");
j = 0;
i++;
fclose(fin1);
}

for (i = 0 ; i < N ; i++)
foundsize[i] = size[i];

printf("Finished reading from files\n");

MPI_Barrier (MPI_COMM_WORLD);
start = MPI_Wtime();

/* Start DNA sequence comparison */
for (k = 1 ; k < N ; k++)
{
cell = (struct table**)calloc(size[0]+1, sizeof(struct table*));
for (i = 0 ; i < size[0]+1 ; i++)
cell[i] = (struct table*)calloc(size[k]+1, sizeof(struct table));

printf("Initialization\n");
for (i = 1 ; i <= size[0] ; i++)
{
for (j = 1 ; j <= size[k] ; j++)
{
if (seq[0][i-1] == seq[k][j-1])
cell[i][j].value = 1.0;
else
cell[i][j].value = -1.0/3.0;
}
}

/* Calculate scoring matrix */
while (!stop)
{
for (i = row-1 ; i >= 0 ; i--) // check subcolumn
{
test = cell[i][column].value - (1.0 + (1.0/3.0)*(double)
(row-i));
if (test max)
{
max = test;
cell[row][column].previ = i;
cell[row][column].prevj = column;
}
}
for (j = column-1 ; j >= 0 ; j--) // check subrow
{
test = cell[row][j].value - (1.0 + (1.0/3.0) * (double)
(column-j));
if (test max)
{
max = test;
cell[row][column].previ = row;
cell[row][column].prevj = j;
}
}
test = cell[row-1][column-1].value + cell[row][column].value;
if (test max)
{
max = test;
cell[row][column].previ = row-1;
cell[row][column].prevj = column-1;
}
cell[row][column].value = max;

/* find maximum score in matrix */
if (maxscore < max)
{
maxscore = max;
maxrow = row;
maxcolumn = column;
}
max = 0.0; // reset max score of cell

if (column >= size[k])
{
column = 1;
row++;
printf("Row = %d\n", row);
}
else
column++;
if (column == 1 && row size[0])
stop = 1;
}

/* DNA sequence alignment */
foundsize[0] = size[0];
foundseq0 = (char*)calloc(foundsize[0], sizeof(char));
foundseq1 = (char*)calloc(foundsize[k], sizeof(char));

row = maxrow;
column = maxcolumn;
stop = 0;
count = 0;

printf("Alignment\n");
while (!stop)
{
/* store the DNA alignments */
prevrow = cell[row][column].previ;
prevcol = cell[row][column].prevj;
if (cell[row][column].value == 0.0)
break;
if (row == prevrow)
foundseq0[count] = '-';
else
foundseq0[count] = seq[0][row-1];
if (column == prevcol)
foundseq1[count] = '-';
else
foundseq1[count] = seq[k][column-1];
row = prevrow;
column = prevcol;
count++;
printf("Count = %d\n", count);
if (count >= foundsize[0])
{
printf("A\n"); <--- Program stops here
if (temp = realloc(foundseq0, sizeof(char) * (count +
foundsize[0])) == NULL)
{
printf("ERROR: realloc failed");
exit(0);
}
foundsize[0] += count;
foundseq0 = temp;
}
if (count >= foundsize[k])
{
printf("B\n");
if (temp = realloc(foundseq1, sizeof(char) * (count +
foundsize[k])) == NULL)
{
printf("ERROR: realloc failed");
exit(0);
}
foundsize[k] += count;
foundseq1 = temp;
}
}

/* Print the alignment */
for (i = count-1 ; i >= 0 ; i--)
printf("%c", foundseq0[i]);
printf("\n");
for (i = count-1 ; i >= 0 ; i--)
{
if (foundseq1[i] == foundseq0[i])
printf("|");
else
printf(" ");
}
printf("\n");
for (i = count-1 ; i >= 0 ; i--)
printf("%c", foundseq1[i]);
printf("\n");

free(foundseq0);
free(foundseq1);
for (i = 0 ; i < size[0]+1 ; i++)
free(cell[i]);
free(cell);
}

end = MPI_Wtime();
printf("Elapsed time = %lfs\n", end-start);

fclose(fin);
MPI_Finalize();
return 0;
}

Thank you.

Regards,
Rayne

Aug 26 '07 #1
5 3816
On Sun, 26 Aug 2007 02:02:14 -0700, la********@yahoo.com wrote:

[snip]
The problem seems to occur when I'm trying to increase the memory
allocated to the 2 1D arrays foundseq0 and foundseq1. When compiling, I
get the warnings

warning: assignment makes pointer from integer without a cast warning:
assignment makes pointer from integer without a cast

for the lines

if (temp = realloc(foundseq0, sizeof(char) * (count + foundsize[0])) ==
NULL)
Comparison binds more than assignment, so you are assigning the
result of the comparison (0 if realloc fails, 1 otherwise) to
temp.
Also, sizeof(char) is *always* 1.
Try:
if ((temp = realloc(foundseq0, count + foundsize[0]) == NULL)
Note the parentheses around the assignment.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 26 '07 #2
la********@yahoo.com wrote:
>
Dear all,
I'm trying to implement the Smith-Waterman algorithm for DNA sequence
alignment. I'm aligning a 2040 bp query sequence against a 5040 bp
sequence. I'll be trying to implement a parallel version of it later,
that's why I have the MPI timer function to measure the speedup.

seq[0] and foundseq0 are the query sequence. size is the length of the
sequences.

The problem seems to occur when I'm trying to increase the memory
allocated to the 2 1D arrays foundseq0 and foundseq1. When compiling,
I get the warnings

warning: assignment makes pointer from integer without a cast
warning: assignment makes pointer from integer without a cast
Those kinds of warnings, usually indicate that this line of code:
#include <stdlib.h>
is missing.
#include <mpi.h>
mpi.h isn't a standard header
and as a result I can't compile your code.
cell = (struct table**)calloc(size[0]+1, sizeof(struct table*));
If you remove the casts from all of your calloc calls,
do you get more of the same warnings?

--
pete
Aug 26 '07 #3
Thank you, my program works now!

Aug 26 '07 #4
la********@yahoo.com wrote:
Thank you, my program works now!
"If it works, it's not sufficiently tested."
-- Author unknown

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 26 '07 #5
Eric Sosman wrote:
la********@yahoo.com wrote:
>Thank you, my program works now!

"If it works, it's not sufficiently tested."
-- Author unknown
Ah, so a fully tested software is one that doesn't work? :)

Aug 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John | last post by:
I'm getting a segmentation fault when I try to realloc memory. This only happens when I compile using gcc v. 3.2 on Red Hat Linux 8.0 3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
7
by: Alexandre | last post by:
Hello, Maybe it's a little OT, but the fact is that I don't necessarly want to know "how to correct?", but "why it happens?" I have a program who "segment fault" (ok, that's "normal"... ;-)...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.