Re: Another newbie EXC_BAD_ACCESS in C
Re: Another newbie EXC_BAD_ACCESS in C
- Subject: Re: Another newbie EXC_BAD_ACCESS in C
- From: Kevin Ballard <email@hidden>
- Date: Fri, 13 Jan 2006 22:38:02 -0800
Uhh yeah, you're writing 120000 characters into a 100000 character
array. That's your problem right there.
Also, clear() does not empty a string. According to my manpage it's a
method in curses. Perhaps you want dna[0] = 0? Oh, and the initial
strcpy(dna1, "") is useless since you already initialize it to 0.
Oh, and I would suggest that the way you're doing this is going to be
*extremely* inefficient. Instead of doing all the strcat stuff you
should instead do
char da = 'A';
char db = 'C';
char dc = 'G';
char dd = 'T';
and then in the loop simply do
dna1[i] = da; // or db or dc or dd.
And after the loop set
dna1[i] = 0;
which will null-terminate the string. Remove the clear() call as it's
useless, and make sure iterations is smaller than dna1. Also your
fprintf() has "%c" where it should have "%s" as was already
mentioned. Or you could use fputs() instead, since all you're doing
is outputting a string, as in fputs(dna1, fp);
And also as was suggested, in the future you can use gdb to determine
where the EXC_BAD_ACCESS occurred, although you'll want to compile
with debug symbols (and optimization) turned off.
On Jan 13, 2006, at 6:33 PM, W. Thomas Leroux wrote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int iArgc, char *pArgv[])
{
char *da = "A";
char *db = "C";
char *dc = "G";
char *dd = "T";
char dna1 [100000] = {0};
int random;
int iterations = 120000;
int iterations1 = 2;
//int iterations1 = 100000;
int MAXVALUE = 3; // limit selection from 0 to 3.
strcpy(dna1,"");
int j = 1;
for (j = 0; j < iterations1; j++) {
//loop one
int i = 1;
for (i = 0; i < iterations; i++) {
// fill 100000 character buffer loop
random = rand() % (MAXVALUE +1);
if(random == 1)
strcat(dna1,da);
else if (random == 2)
strcat(dna1,db);
else if (random == 3)
strcat(dna1,dc);
else
strcat(dna1,dd);
// printf ("%c%c%c%c", dna1, dna2, dna3, dna4);
}
//write the buffer to the disk, then clear it
FILE *fp;
fp=fopen("//Users//wtl//Documents//dna3.txt", "a");
fprintf(fp, "%c", dna1);
fclose(fp);
clear(dna1);
}
printf ("ALL DONE!");
return 0;
}
However, when I Build and Debug it, I get an EXC_BAD_ACCESS error -
put no specific line error. I'm a bit befuddled, because it looks
like the code should work, and I figured someone with a fresh eye
would spot the issue faster than I can.
--
Kevin Ballard
email@hidden
http://www.tildesoft.com
http://kevin.sb.org
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden