Thread: a segfaulting algorythm

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    Unhappy a segfaulting algorythm

    This is a "string2Hz" algorythm ; It is used to return the Hz of a
    string is entered. This is the syntax:
    [key][octave](optional [in/decrement (+/-)])
    So for example:
    C1
    a1
    a1+
    c1+

    These for example have to return error:
    a+1
    q1
    cc1+


    (error is 0 at this code)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define A1BASE 440
    char tmpchar[255];
    
    int octave_man(void) // gets complete Hz-number of string tmpchar ... .
    {
    	char *tmpchar2,*tmpchar3,*tmpchar4,*tmpchar5;
    	char *alphabad[] = { "a","b","c","d","e","f","g" };
    	int inti,inti2;
    	strncpy(tmpchar2,tmpchar,2); // kick possible garbage out, this mks it "user-friendly"
    	if((strcmp("a1",tmpchar)) == 0) //we use tmpchar to make it bug-free ... . 
    		return A1BASE;	// we don't need a huge calculation to define A1-value !!!
    	for(inti=0;inti<=7;inti++) // count up the scale
    	{
    		if((strncmp(tmpchar2,alphabad[inti],0)) == 0)
    		{
    			strncpy(tmpchar3,tmpchar2,0); // seperation
    			for(inti2 = 0;inti2<=6;inti2++) // count up the octave
    			{
    				sprintf(tmpchar4,"%s%d",tmpchar3,inti2); // tmp-usement
    				if((strncmp(tmpchar2,tmpchar4,2)) ==0 )
    				{
    					sprintf(tmpchar4,"%d",inti2); // real association!
    					break;
    				}
    			}
    			sprintf(tmpchar5,"%s%d",tmpchar3,tmpchar4);
    			if((strcmp(tmpchar5,tmpchar2)) == 0)
    				return ((A1BASE + (A1BASE/7*inti))*inti2);	// return tone+octave as absolute Hz
    			strcat(tmpchar5,"+");
    			if((strcmp(tmpchar5,tmpchar2)) == 0)
    				return ((A1BASE + (A1BASE/7*inti))+(A1BASE/14)*inti2);//return tone+octave+ as absolute Hz
    			sprintf(tmpchar5,"%s%d-",tmpchar3,tmpchar4);
    			if((strcmp(tmpchar5,tmpchar2)) == 0)
    				return ((A1BASE + (A1BASE/7+inti))-(A1BASE/14)*inti2);//return tone+octave- as absolute Hz
    		}		
    		// if we leave the for(); loop generally, somebody entered something very
    		// stupid! IT IS NOT VERY INTELIGENT TO POINT THIS OUT AFTER CALC SO MUCH :)
    	}
    //recapitulation: alphabad	: the scale
    //		  tmpchar2 	: whole string
    //		  tmpchar3 	: tone
    //		  tmpchar4	: octave
    //		  tmpchar5	: is just a tmp-char :)
    //		  init		: integer used for the scale
    //		  inti2		: integer used for the octave
    	return 0;
    }
    int main(int argc, char *argv[])
    {
    	int integer;
    	scanf("%s",tmpchar);
    	if((integer =octave_man()) == 0)
    	{
    		printf("error!\n");
    		return 1;
    	}
    	else
    	{
    		printf("%d\n",integer);
    		return 0;
    	}
    }

    A short appendix: The math behind it:
    a1 = 440 Hz, a2 = 880! So b1 is (a1+(a1/7*key))*octave ,
    so (A1 + (A1/7*1)) *1 = 502.857 (503 = B1 )!

    A general formula to calculate a full key is:
    (A1 + (A1/7*x)) *y
    (X = Number of the key: a is 1 , b is 2, c is 3 ,... | Y = Octave )


    This means:
    a1 b1 c2 d2 e2 f2 g2 a2
    440 503 566 629 691 754 817 880


    If I have to calculate with incrementation, I just do:
    (A1 + (A1/7*x) + (A1/14) *y
    ---------


    Please, could somebody reply and tell me where make
    octave_man() errors?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    34
    I see that it "takes some time", maybe the local time of the local heroes like salem is not very familiar with Europe , right ?

    Could you please send me a reply to
    [email protected]

    ???? This would be very nice

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Try changing

    Code:
    for(inti=0;inti<=7;inti++)
    to ..

    Code:
    for(inti=0;inti< 7;inti++)
    You were going for 8 times instead of 7.

    edit:

    There are some really questionable things about some of your code also. If someone doesn't beat me to it I'll post them later. I need some sleep now though.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Note that the variables tmpchar2, tmpchar3, tmpchar4 and tmpchar5 are pointers to char. If you want to use them as buffers to store strings, you will need to allocate memory for them.

    Also note that this will lead to a redesign of your function. In case you have allocated memory, you must also free the memory before you leave the function.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    34
    OK... .
    Ähm, the allocation is not neccessary , ... of course you are right, but the example you see is "over-tested" ! I used arrays first,... .

    Secondly, gdb tells me errors in strncmp, strcpy, strcmp and strcpy, no matter if I use pointers and allocate something first or just use arrays ... .

    @Mr Wizard: Thx ... I ´ll try this ... wait a few seconds ...

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    34
    ok, I have some errors in it, but it is NO segfaulting function anymore ... . Arrays:
    char array[N] => Range: 0 to N-1

    However, if I enter a value different then a1, the function returns zero (error)... .

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post your latest code.

    >>I see that it "takes some time"
    We're not here at your demand, people will answer when they're good'n'ready.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    Talking

    >Post your latest code.
    >
    Not neccessary anymore, I already got it last night!


    >>I see that it "takes some time"
    >We're not here at your demand, people will answer when they're good'n'ready.

    Of course you are right! I am sorry that my "argumentation" sounds like "I am an a..." !
    Thx for attention

    demonus

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Your basic algorithm is wrong. Have a look at http://www.math.niu.edu/~rusin/papers/uses-math/music/ it has loads of useful (and quite interesting) info on how music works mathematically as well as a list of frequencies for different notes on a correctly tuned piano which is probably worth a look to test your algorithm, as is the discussion of algorithms and why there are 12 notes in an octave. Enjoy!
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segfaulting!
    By CMakesMeSad :( in forum C Programming
    Replies: 23
    Last Post: 07-10-2009, 01:04 PM
  2. Segfaulting Distance Program
    By radiohead in forum C Programming
    Replies: 2
    Last Post: 01-09-2006, 08:48 PM
  3. distance algorythm help
    By MicroFiend in forum Game Programming
    Replies: 9
    Last Post: 06-12-2004, 09:28 PM
  4. Why is it segfaulting?
    By XSquared in forum C Programming
    Replies: 7
    Last Post: 03-30-2004, 06:52 AM
  5. parameters algorythm
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 12-02-2003, 07:18 PM