LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2012, 03:44 PM   #1
Steve Tentmacher
LQ Newbie
 
Registered: Sep 2012
Posts: 3
Blog Entries: 1

Rep: Reputation: Disabled
I am encountering a segfault after 69 iterations of a while loop


I am encountering a segfault when utilizing string processing in C. I have malloced char * for the desired string length. After 69 iterations it segfaults. Do you know if this could be stack related?
 
Old 09-25-2012, 03:51 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by Steve Tentmacher View Post
I have malloced char * for the desired string length.
Did you allow for the terminating null? When mallocing space for strings in C, you always need one more than the string length, because of that null.

Quote:
Originally Posted by Steve Tentmacher View Post
Do you know if this could be stack related?
Nothing you said makes stack involvement likely. But you gave so little info, it could be anything.
If my first guess (above) was wrong, post the code. Don't ask us to guess your bug from symptoms with no code.

Last edited by johnsfine; 09-25-2012 at 03:53 PM.
 
Old 09-25-2012, 06:21 PM   #3
Steve Tentmacher
LQ Newbie
 
Registered: Sep 2012
Posts: 3
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
I have not done a string cat with the return string, so I may need to do so.
I will take a look at the string function. It does a sum as follows:


char * sum_CRT_terms(int prime_array[], int r, int gamma[], char *modulus_M){
int i,m,z_array[512],
j,k,l,
wkg_prime_precision = r;

int counter = 0, sum = 0, sum_check = 0,
carry = 0, output_wkg_val, current_wkg_val,
length_of_output = wkg_prime_precision*5 + 2;

//This places the modulus into a working array, as digits.
for (i=0; i<strlen(modulus_M);i++)
z_array[i] = modulus_M[i] - '0';//Subtract character '0' to
//give int result from the char.

char *output_number = (char *) malloc(length_of_output);
char *output_Mi = (char *) malloc(length_of_output);
char *output_CRT = (char *) malloc(length_of_output);
char test_break[80];
int array_Mi[wkg_prime_precision-1], array_gamma[r];
int borrow = 0;
char char_digit;
for ( m = (length_of_output - 1) ; m >= 0 ; m-- )
output_number[m] = '0';
for (i = 0; i<wkg_prime_precision ; i++){
//printf("\n\n\tM%i for CRT%d: \t ",i,i );
k = 0, l=0;
for ( j = 0; j<wkg_prime_precision ; j++){
/* Note that the condition below excludes prime_array[i]
This allows us to find Mi (which is the product of all
m(j) except modulus m(i). The other value of array_gamma,
contains a(i)*y(i). Then when the multiplication is done,
by find_CRT, we end up with the "i"th term of the CRT.
Further, this term is added to the current sum, which
is done below with string processing. In the processing
which follows, a string compare is done, to test if the
the resultant string is lexicographically less than the
modulus. If it is, the next CRT term (if any are left)
is done within this loop. If the resultant string is larger,
it is subtracted, using a string subtract. */
if (j ==i){
array_gamma[l++] = gamma[j];
}
else
{
array_gamma[l++] = prime_array[j];
array_Mi[k++] = prime_array[j];
counter += j;
}}
output_Mi =
find_CRT(array_gamma, wkg_prime_precision);
for ( m = (length_of_output - 1) ; m >= 0 ; m-- ){
output_wkg_val = ( output_number[m] - '0');
current_wkg_val= ( output_Mi[m] - '0' );
output_wkg_val += current_wkg_val + carry;
carry = output_wkg_val/10;
output_wkg_val = output_wkg_val%10;
char_digit = int_to_char(output_wkg_val);
output_number[m] = char_digit;
}
/* String compare tells if the string is lexicographically
less than modulus.*/
if ( strcmp( output_number, modulus_M) < 0 )//{
//printf("\n\n\tM%i for CRT%d: \t ",i,i );
;
//printf("\tThe working output is less than M.");
//}
else for ( m = (length_of_output - 1); m >= 0; m--){
// printf("\n\n\tM%i for CRT%d: \t ",i,i );
output_wkg_val = ( output_number[m] - '0') - borrow;
borrow = 0;
output_wkg_val -= z_array[m];
if (output_wkg_val < 0){
output_wkg_val += 10;
borrow = 1;
}
char_digit = int_to_char(output_wkg_val);
output_number[m] = char_digit;
}
output_CRT=output_number;
borrow = 0;
carry = 0;
}
return output_CRT;
}//end of sum_CRT_terms()

Last edited by Steve Tentmacher; 09-25-2012 at 06:22 PM. Reason: Enhance answer. This is the called function by the caller's while.
 
Old 09-26-2012, 03:58 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Please, please, please use code tags; your code is difficult to read this way.

Edit your previous post and just before your code starts, type [code]. At the end of your code, type [/code]. This will maintain indentations and makes it easier to read.

One thing that I notice is that you never check if malloc returns null.
 
1 members found this post helpful.
Old 09-26-2012, 07:56 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
I see several things that appear to be bugs. But without seeing the whole program it is hard to guess at the bug causing the seg fault.

You malloc a lot of things without ever freeing them, so eventually the program would exhaust memory and fail. I doubt that is the serious problem, but it might be.

You do appear to be allowing space for the terminating null, but you don't appear to be inserting any terminating null. You use strcmp on two strings one of which lacks a terminating null. If the two strings happen to be equal, there is a tiny chance (each time) that the comparison would seg fault due to the lack of a terminating null. I doubt that is the bug causing the symptom, but it might be.

Information (such as array_Mi and counter) is computed and never used. That isn't directly a bug, but is usually an indication that you haven't really coded the algorithm you think you have coded, implying a more serious bug elsewhere.

Last edited by johnsfine; 09-26-2012 at 08:06 AM.
 
1 members found this post helpful.
Old 09-26-2012, 01:45 PM   #6
Steve Tentmacher
LQ Newbie
 
Registered: Sep 2012
Posts: 3
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Hello, and Thanks.

Thanks for the replies.

I have spent two years writing a single and now multi-precision implementation. Since I have worked alone, I am sure there are gaps in my understanding, short-cuts taken, etc. I cannot reply to your replies for at least a day, or two. (I am working on a short fuse project.)

Nonetheless, I greatly appreciate your deep experience, and will integrate your recommendations as applicable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Next Iterations of the KDE Workspaces LXer Syndicated Linux News 0 06-19-2012 05:20 AM
[SOLVED] Breaking iteration after a given number of iterations (bash). stf92 Linux - Newbie 8 06-14-2011 10:16 AM
problem with segfault signal handling in loop linuxsavar Programming 1 06-08-2011 09:07 AM
I'm getting closer to the answer: Python iterations over nested tuples Mohtek Programming 3 05-24-2010 01:16 PM
eth0: too many iterations (6) in nv_nic_irq bee2643 Linux - Networking 1 01-11-2009 12:47 AM


All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration