Segmentation fault 11 after compile

hello ... iv'e got a weird problem with this program on macOS Ventura 13.1 on a MacBook Pro M1 MAX and clang

prog:


#include <stdlib.h>

#include <stdint.h>

#include <stdbool.h>

#include <stdarg.h>

#include <errno.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/time.h>

#include <math.h>

#include <unistd.h>

#include <string.h>

#include <ctype.h>

#include <memory.h>

#include <iconv.h>



#define CA_PRIVATE_IMPLEMENTATION



static const char *coin(int fex) {

    bool isDuplicate = false;

    //---------------------------------------------------------------------

    const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    int max_index = (sizeof(charset) - 1);

    //---------------------------------------------------------------------

    const char *picked = NULL;

    const char *number = NULL;

    int j = 0,i = 0;

    

    for (i = 0; i < fex; i++)

    {

        

        do

        {

            int target = rand() % max_index; // Generate

            strncpy((char *)number,&charset[target],1); // Pick

            // Check for duplicates

            for (j = 0; j < fex; j++)

            {

                if (number == &picked[j])

                {

                    isDuplicate = true;

                    break; // Duplicate detected

                }

                else {

                    memmove((void *)&charset[target], &charset[target + 1], max_index - target);

                    max_index--;

                    isDuplicate = false;

                    break; // No Duplicate detected

                }

            } // end for

        } // end do

        while (isDuplicate); // equivalent to while(isDuplicate == true)

        

        if (!isDuplicate) { // equivalent to if(isDuplicate == false)

            strncpy((char *)&picked[j],number,1); // picked

        }

    } // end for

    return picked;

}



int main(void) {

    time_t start, stop;



    start = time(NULL);

    for (int i = 0; i < 6; i++){

        printf("%s\n", coin(26));

    }

    printf("%s\n",coin(5));

    stop = time(NULL);

    printf("Time elapsed : %ld seconds\n",(stop - start));

    

    return 0;

}

no warnings and no error on compile and sign

but on run it crashes

don't forget

First things first, you’ve failed to include <stdio.h>. I added that to the top of the file. I’ve included the resulting program below so I can reference the line numbers.

no warnings and no error on compile

If that’s the case then you need to enable more warnings (-: Or program in a language that’s more forgiving, like Swift (-:

IMPORTANT: Come to think of it, this isn’t a joke. C-based languages are full of pitfalls. If you’re going to write code in C, you really do need to learn how to enable all the compiler warnings, and learn how to use the static analyser, and enable the runtime analysers like ASan and UBSan. That’s the only way to make C programs that are even remotely reliable. However, there is a better alternative, namely, to use a language that doesn’t have all these pitfalls. On Apple platforms the obvious choice is Swift but there are lots of other good choices out there, like Kotlin or Rust. Anything but C, basically (-:

I pasted your code into a test project here in my office where I have all the warnings turned on. It produced two important results:

  • The compiler issues a warning on line 37 that Loop will run at most once (loop increment never executed). That’s because the if statement starting on line 39 has a break on both branch of the if. This breaks out of the for loop starting on line 37. I think you’re expecting it to break out of the do loop starting on line 32.

  • I then ran the static analyser. This flags an issue on line 35, namely Null pointer passed as 1st argument to string copy function. That’s because the declaration of number on line 26 sets it to NULL and then nothing changes it. It’s this problem that’s almost certainly the cause of your SIGSEGV crash.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

 1 #include <stdlib.h>
 2 #include <stdint.h>
 3 #include <stdbool.h>
 4 #include <stdarg.h>
 5 #include <errno.h>
 6 #include <sys/types.h>
 7 #include <sys/stat.h>
 8 #include <sys/time.h>
 9 #include <math.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <memory.h>
14 #include <iconv.h>
15 #include <stdio.h>
16 
17 #define CA_PRIVATE_IMPLEMENTATION
18 
19 static const char *coin(int fex) {
20     bool isDuplicate = false;
21     //---------------------------------------------------------------------
22     const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
23     int max_index = (sizeof(charset) - 1);
24     //---------------------------------------------------------------------
25     const char *picked = NULL;
26     const char *number = NULL;
27     int j = 0,i = 0;
28     
29     for (i = 0; i < fex; i++)
30     {
31         
32         do
33         {
34             int target = rand() % max_index; // Generate
35             strncpy((char *)number,&charset[target],1); // Pick
36             // Check for duplicates
37             for (j = 0; j < fex; j++)
38             {
39                 if (number == &picked[j])
40                 {
41                     isDuplicate = true;
42                     break; // Duplicate detected
43                 }
44                 else {
45                     memmove((void *)&charset[target], &charset[target + 1], max_index - target);
46                     max_index--;
47                     isDuplicate = false;
48                     break; // No Duplicate detected
49                 }
50             } // end for
51         } // end do
52         while (isDuplicate); // equivalent to while(isDuplicate == true)
53         
54         if (!isDuplicate) { // equivalent to if(isDuplicate == false)
55             strncpy((char *)&picked[j],number,1); // picked
56         }
57     } // end for
58     return picked;
59 }
60 
61 int main(void) {
62     time_t start, stop;
63 
64     start = time(NULL);
65     for (int i = 0; i < 6; i++){
66         printf("%s\n", coin(26));
67     }
68     printf("%s\n",coin(5));
69     stop = time(NULL);
70     printf("Time elapsed : %ld seconds\n",(stop - start));
71     
72     return 0;
73 }

here is an online version with compiler and editor with modified source

http://tpcg.io/_BN0N31

here is the plain version:


1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <stdbool.h>
5 #include <time.h>
6 #include <math.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <memory.h>
11 #include <iconv.h>
12
13static const char *coin(int fex)
14{
15
16   bool isDuplicate = false;
17    //---------------------------------------------------------------------
18    const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
19    int max_index = (sizeof(charset) - 1);
20    //---------------------------------------------------------------------
21    const char *picked = "AAAAAAAAAAAAAAAAAAAAAAAAAA";
22    const char *number = "A";
23    int j = 0,i = 0;
24
25    for (i = 0; i < fex; i++) {
26        do {
27            int target = rand() % max_index; // Generate
28            strncpy((char *)number,&charset[target],strlen(&charset[target])); // Pick
29            // Check for duplicates
30
31            for (j = 0; j < fex; j++) {
32
33                if (number == &picked[j]) {
34
35                    isDuplicate = true;
36                    // break; // Duplicate detected
37                } // end if
38                else {
39                    memmove((void *)&charset[max_index], &charset[max_index - atoi(number)], atoi(&charset[max_index]) - atoi(number));
40                    max_index--;
41                    isDuplicate = false;
42                    // break; // No Duplicate detected
43                } // end else
44            } // end for
45        } // end do
46        while (isDuplicate); // equivalent to while(isDuplicate == true)
47
48        if (!isDuplicate) { // equivalent to if(isDuplicate == false)
49            strncpy((char *)&picked[j],number,strlen(number)); // picked
50        } // end if
51    } // end for
52    return picked;
53 }
54
55 int main(void)
56 {
57    time_t start, stop;
58
59    start = time(NULL);
60
61    for (int i = 0; i < 6; i++) {
62        printf("%s\n", coin(26));
63    } // end for
64    printf("%s\n",coin(5));
65    stop = time(NULL);
66    printf("Time elapsed : %ld seconds\n",(stop - start));
67
68    return 0;
69
70 }

Do you have a follow-up questions here? Or were you just posting the fixed source for just for the record?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

fixed source

good day @eskimo

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdint.h>
 4 #include <stdbool.h>
 5 #include <string.h>
 6 #include <math.h>
 7 #include <ctype.h>
 8 #include <memory.h>
 9 #include <time.h>
10 
11 #define fox 25
12 
13 int main()
14 {
15    time_t start, stop;
16    start = time(NULL);
17    
18    bool isDuplicate = false;
19 
20    //---------------------------------------------------------------------
21    char charset[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
22    int max_index = (strlen(charset) - 1);
23    //---------------------------------------------------------------------
24
25    char *picked[fox];
26    char *newabc[1];
27
28    int j = 0,i = 0;
29
30    for (i = 0; i <= fox; i++) {
31    newabc[0] = malloc(1*sizeof(char *));
32        picked[i] = malloc(1*sizeof(char *));
33
34        do {
35            newabc[0] = &charset[random() % max_index];
36            // Check for duplicates
37            for (j = 0; j <= fox; j++) {
38                if (newabc[0] == picked[j]) {
39                    isDuplicate = true;
40                    break; // Duplicate detected
41                } // end if
42            } // for
43        } while (isDuplicate); // equivalent to while(isDuplicate == true) // end do while
44
45        strncpy((char *)picked[j],newabc[0],strlen(picked[j])); // picked
46
47        printf("%s",picked[j]);
48
49        free(newabc[0]);
50        free(picked[j]);
51  } // end for
52    printf("\n");
53  return 0;
54 }
Segmentation fault 11 after compile
 
 
Q