Post

Replies

Boosts

Views

Activity

Reply to Segmentation fault 11 after compile
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 }
Jan ’23
Reply to Segmentation fault 11 after compile
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 }
Jan ’23