473,695 Members | 2,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(foundse q0, sizeof(char) * (count + foundsize[0]))
== NULL)
and
if (temp = realloc(foundse q1, sizeof(char) * (count + foundsize[k]))
== NULL)

although temp, foundseq0 and foundseq1 are char pointers.

I run the program, and I get the "Segmentati on 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.t xt", "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("Sequenc e 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("Finishe d 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(s ize[k]+1, sizeof(struct table));

printf("Initial ization\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(f oundsize[0], sizeof(char));
foundseq1 = (char*)calloc(f oundsize[k], sizeof(char));

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

printf("Alignme nt\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(foundse q0, 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(foundse q1, 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 3867
On Sun, 26 Aug 2007 02:02:14 -0700, la********@yaho o.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(foundse q0, 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(foundse q0, 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********@yaho o.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********@yaho o.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********@yaho o.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
6551
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 error does not occur. Is there a way to solve this using compiler options, or any other method? In the end, I need this program to compile on the Linux box. Any help would be greatly appreciated!!!
16
8982
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 who don't speak french arbre means tree.
3
2869
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 call the function using this code fragement in my main function: --- char** arg_array; arg_count = create_arg_array(command, argument, arg_array); for(count = 0; count < arg_count; count++)
7
3353
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"... ;-) but this time, it's not my code who "segment fault" but it's in the call of malloc/mallopt I've got:
3
11432
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
6
4774
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 fault. If I just call realloc( ) once before calling malloc( ), it is OK. Why? I am trying to read some double-typed items from infile and save them
27
3351
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
3
5178
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 from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
14
3195
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 out, I've already tried all my ideas. Also, please do comment on my coding style or other aspects. Thank you. #include <stdio.h> #include <string.h>
0
8582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9001
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8860
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4348
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1980
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.