unexpected freeze while handling input - c

I've been trying to handle multiple functions which the user can select from, but for some reason, unexpected input isn't causing the developed reaction, freezing instead.
int main(){
int done = 0, isModeValid = 1;
char nextMode[15], *options[] = {"quit", "test", "getASCII"};
while(done == 0){
cls();
isModeValid = 0;
text(1);
currentOptions(options);
gets(nextMode);
int i = 0;
for(i = 0; i < (sizeof(options)); i++){
if(strcmp(nextMode, options[i]) == 0){
done = runMode(i);
break;
}
//Error seems to happen after this point
if(strcmp(nextMode, options[i]) != 0 && i == sizeof(options)){
cls();
text(3);
Sleep(750);
}
}
}
return 0;
}
void cls(){
system("cls");
}

You are invoking undefined behaviour. sizeof yields the size of the argument in bytes/chars, not the length of the array. So you are iterating over more elements than the array actually contains and try to access elements past its bounds.
Use sizeof(options) / sizeof(options[0]) to get the length independent from the type of each entry.
Note: Declaring the array static would make its allocation and initialization before main is called. Your current version will do that each time the function is called. While uncritical for main, it will be significant for other functions which are called more than once.

To get the number strings in options, you need (sizeof(options)/sizeof(options[0])), not just sizeof(options)... so your for loop is looping too many times, and you're accessing out of bounds.
Also, your second if never executes because i will never get to sizeof(options).

Related

How to use more than one dynamic allocation on C programming?

I'm making a program that reads two sets of data (float) from two different .txt files, and then it transfers these data to two different arrays, which will be used in further calculations. However, when I try to use dynamic allocation more than once, something goes wrong and the data seem not to be stored in the array.
The following simplified program seems to be working fine:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float *VarA;
int n = 0;
int *counter;
int i;
FILE *input1;
input1 = fopen("C:\\Users\\...test.txt","r");
VarA = (float*)calloc(20001, sizeof(float));
for(i = 0; i < 20001; i++)
{
fscanf(input1,"%f",&VarA[i]);
printf("%f\n",VarA[i]);
}
free(VarA);
fclose(input1);
return 0;
}
it successfully shows the data stored in the array VarA. However, if I introduce a new array to count the number of lines in the file (which is necessary for my further calculations), I just get the value 0.000000 from every array element:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float *VarA;
int n = 0;
int *counter;
int i;
FILE *input1;
input1 = fopen("C:\\Users\\...test.txt","r");
counter = (int*)calloc(100000, sizeof(int));
while(fscanf(input1,"%f",&counter[n]) != EOF)
{
n++;
}
free(counter);
printf("n = %i\n", n);
VarA = (float*)calloc(n, sizeof(float));
for(i = 0; i < n; i++)
{
fscanf(input1,"%f",&VarA[i]);
printf("%f\n",VarA[i]);
}
free(VarA);
fclose(input1);
return 0;
}
I know that I can avoid using another array to count the number of lines. The point is that every time I use another array, for any purpose, I get the same result. For instance, if I don't use an array to count the number of lines, but I make another one to store my other set of data, one of these arrays just won't present the data after the reading. I tried to modify my program several times in order to find the source of such behavior, but without success.
(At least) two major problems: first,
counter = (int*)calloc(100000, sizeof(int));
while(fscanf(input1,"%f",&counter[n]) != EOF) {
n++;
}
free(counter);
is basically saying "Grab me a chunk of memory, fill it with data as I read the file, then throw it away without ever using it." Probably not what you intended. Then,
VarA = (float*)calloc(n, sizeof(float));
for (i = 0; i < n; i++) {
fscanf(input1,"%f",&VarA[n]);
printf("%f\n",VarA[n]);
}
free(VarA);
which says, "Grab a big chunk of memory, then read data from after the end of the file I just read everything from, put it there, then throw it away."
If you want to read the data from the same file again, you'll have to close it an reopen it (or "seek" to the start). And if you want to do anything with it, you'll have to do it before free()ing the memory you loaded it into.
counter = (int*)calloc(100000, sizeof(int));
// ^--- `int*` ^--- `int`
// v--- `int` pointer
while(fscanf(input1,"%f",&counter[n]) != EOF)
// ^--- `float` designator
Do you see any discrepancies here? Your code allocates ints, then passes a pointer to those ints to fscanf telling it they're floats (using the %f designator). According to the C standard draft n1570, section 7.21.6.2p10 this constitutes undefined behaviour:
If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
My suggestion would be to use the * assignment suppression modifier here, for example:
while (fscanf(input1, "%*f") != EOF) n++;
or, alternatively
while (fscanf(input1, "%f", &(float){0}) != 1) n++;
Note also how I've changed the check from EOF to 1. You can find more information about the return values of fscanf here (which you really should read before using any scanf-related function... and stop guessing, because guessing in C can be harmful).
Additionally, you need to rewind your file once it reaches EOF, otherwise every call to fscanf following this loop will return EOF:
rewind(input1);
P.S. Don't cast malloc in C. This goes for calloc and realloc, too. There's a lot of this quoted stuff that has opengroup manuals of its own; I'll leave it as an exercise to you to find (and read) the opengroup manuals.

What's the point of using linear search with sentinel?

My goal is to understand why adopting linear search with sentinel is preferred than using a standard linear search.
#include <stdio.h>
int linearSearch(int array[], int length) {
int elementToSearch;
printf("Insert the element to be searched: ");
scanf("%d", &elementToSearch);
for (int i = 0; i < length; i++) {
if (array[i] == elementToSearch) {
return i; // I found the position of the element requested
}
}
return -1; // The element to be searched is not in the array
}
int main() {
int myArray[] = {2, 4, 9, 2, 9, 10};
int myArrayLength = 6;
linearSearch(myArray, myArrayLength);
return 0;
}
Wikipedia mentions:
Another way to reduce the overhead is to eliminate all checking of the loop index. This can be done by inserting the desired item itself as a sentinel value at the far end of the list.
If I implement linear search with sentinel, I have to
array[length + 1] = elementToSearch;
Though, the loop stops checking the elements of the array once the element to be searched is found. What's the point of using linear search with sentinel?
A standard linear search would go through all the elements checking the array index every time to check when it has reached the last element. Like the way your code does.
for (int i = 0; i < length; i++) {
if (array[i] == elementToSearch) {
return i; // I found the position of the element requested
}
}
But, the idea is sentinel search is to keep the element to be searched in the end, and to skip the array index searching, this will reduce one comparison in each iteration.
while(a[i] != element)
i++;
First, lets turn your example into a solution that uses sentinels.
#include <stdio.h>
int linearSearch(int array[], int length, int elementToSearch) {
int i = 0;
array[length] = elementToSearch;
while (array[i] != elementToSearch) {
i++;
}
return i;
}
int main() {
int myArray[] = {2, 4, 9, 2, 9, 10, -1};
int myArrayLength = 6;
int mySearch = 9;
printf("result is %d\n", linearSearch(myArray, myArrayLength, mySearch));
return 0;
}
Notice that the array now has an extra slot at the end to hold the sentinel value. (If we don't do that, the behavior of writing to array[length] is undefined.)
The purpose of the sentinel approach is to reduce the number of tests performed for each loop iteration. Compare:
// Original
for (int i = 0; i < length; i++) {
if (array[i] == elementToSearch) {
return i;
}
}
return -1;
// New
while (array[i] != elementToSearch) {
i++;
}
return i;
In the first version, the code is testing both i and array[i] for each loop iteration. In the second version, i is not tested.
For a large array, the performance difference could be significant.
But what are the downsides?
The result when the value is not found is different; -1 versus length.
We have to make the array bigger to hold the sentinel value. (And if we don't get it right we risk clobbering something on the stack or heap. Ouch!)
The array cannot be read-only. We have to be able to update it.
This won't work if multiple threads are searching the same array for different elements.
Using the sentinel value allows to remove variable i and correspondingly its checking and increasing.
In your linear search the loop looks the following way
for (int i = 0; i < length; i++) {
if (array[i] == elementToSearch) {
return i; // I found the position of the element requested
}
}
So variable i is introduced, initialized, compared in each iteration of the loop, increased and used to calculate the next element in the array.
Also the function has in fact three parameters if to pass to the function the searched value
int linearSearch(int array[], int length, int value) {
//...
Using the sentinel value the function can be rewritten the following way
int * linearSearch( int array[], int value )
{
while ( *array != value ) ++array;
return array;
}
And inside the caller you can check whether the array has the value the following way
int *target = linearSearch( array, value );
int index = target == array + size - 1 ? -1 : target - array;
If you add the value to search for, you can reduce one comparison in every loop, so that the running time is reduced.
It may look like for(i = 0;;i++) if(array[i] == elementToSearch) return i;.
If you append the value to search for at the end of the array, when instead of using a for loop with initialization, condition and increment you can a simpler loop like
while (array[i++] != elementToSearch)
;
Then the loop condition is the check for the value you search for, which means less code to execute inside the loop.
The point is that you can convert the for loop into a while/repeat loop. Notice how you are checking i < length each time. If you covert it,
do {
} while (array[i++] != elementToSearch);
Then you don't have to do that extra checking. (in this case, array.length is now one bigger)
Although the sentinel approach seems to shave off a few cycles per iteration in the loop, this approach is not a good idea:
the array must be defined with an extra slot and passing its length as 1 less than the defined length is confusing and error prone;
the array must be modifiable;
if the search function modifies the array to set the sentinel value, this constitutes a side effect that can be confusing and unexpected;
the search function with a sentinel cannot be used for a portion of the array;
the sentinel approach is inherently not thread safe: seaching the same array for 2 different values in 2 different threads would not work whereas searching a constant read only array from multiple threads would be fine;
the benefits are small and only for large arrays. If this search becomes a performance bottleneck, you should probably not use linear scanning. You could sort the array and use a binary search or you could use a hash table.
optimizing compilers for modern CPUs can generate code where both comparisons will be performed in parallel, hence incur no overhead;
As a rule of thumb, a search function should not have side effects. A good example of the Principe of least surprise.

Why do I get a segmentation fault when trying to copy an array from stdin to a 2d array?

This is in relation to the HackerRank Restaurant problem, which I have solved in other languages but am trying, now, to solve in C.https://www.hackerrank.com/challenges/restaurant
I first tried to store the result if read_slice_dimension() directly into the multidimensional array, but because I cannot pass an array back from a function without making it static, this does not work because then every nested array points to the same static 2 integer array in memory, which, as a result of it being overwritten every time read_slice_dimension() is called, means I would have an array containing num_slices pointers to the last array read in from stdin.
Thus my decision to try memcpy, so that I could copy the arry from read_slice_dimension() to a new block of memory so that it persists and is not lost when I read in the next slice. It appears, however, that memcpy is not the way to do this. What is?
// Gets the number of slices for this test according to the first input value from stdin.
int read_num_slices() {
int num_slices = 0;
scanf("%i", &num_slices);
if (num_slices == 0) {
goto error;
}
return num_slices;
error:
flag_error("ERROR: Could not parse the number of entries from first input line.");
}
// Gets a single line from stdin and attempts to parse it into a 2D int array representing the dimensions of a slice.
int* read_slice_dimension() {
static int slice_dimension[2] = {0};
scanf("%i %i", &slice_dimension[0], &slice_dimension[1]);
if (slice_dimension[0] + slice_dimension[1] == 0) {
goto error;
}
return slice_dimension;
error:
flag_error("ERROR: Could not parse line entered into a 2 integer array representing the slice's dimensions.");
}
// Gets all of the bread slices to be processed.
//
// This function reads from stdin. The first line should be a single integer that specifies the number of slices to be
// processed by this current test. The subsequent lines should be two integers separated by a space which represent
// the 2D dimensions of each slice.
int** get_slices() {
int num_slices = read_num_slices();
static int** slices;
slices = (int**)malloc(num_slices * sizeof(int*));
int i = 0;
for (i; i < num_slices; i++) {
int* slice = slices[i];
slice = (int*)malloc(2 * sizeof(int));
memcpy(slice, read_slice_dimension(), 2 * sizeof(int));
printf("%i %i\n", slices[i][0], slices[i][1]); // CAUSES SEGMENTATION FAULT
}
return slices;
}
You're setting slice = slices[i], then calling malloc. That's backwards. Call malloc, then set slices[i].
– user3386109

Structure variable in an if statement

I need to make an C application for school and im stuck at one bit. I filled my struct with word from a file wich works fine, except for the time it just prints a random integer i guess?
My code:
char buffer[20];
int i;
for(i = 0; i < 10; i++) {
fgets(buffer,20,fp);
apcWordList[i].pcWord = strdup(buffer);
apcWordList[i].cMaxScore = 0;
apcWordList[i].tTime = time(NULL);
}
fclose(fp);
Now what i wanted to do was this(sSecretWord is the word the person guessed and score the points he got for guessing the word):
for(i = 0; i < 10; i++) {
fgets(buffer,20,fp);
if(apcWordList[i].pcWord == sSecretWord && score > apcWordList[i].cMaxScore) {
apcWordList[i].cMaxScore = score;
apcWordList[i].tTime = time(NULL);
}
}
but it crashes and I am really confused how to compare the variables and change them when needed. I hope I have explained it well enough and my English could be read well.
Assuming pcWord is a char *, you cannot compare that pointer using ==, most of the time.
There is no string data type in C, a string is never a single value.
You must use strcmp():
if(strcmp(apcWordList[i].pcWord, sSecretWorD) == 0 &&
score > apcWordList[i].cMaxScore)
The rest of your code makes little sense, I'm afraid. The loop has a typo, and there's no connection between the fgets() and the if. I don't think the fgets() should be in the second part of the code, at all.
The following expression in your if condition
apcWordList[i].pcWord == sSecretWord
compares the memory addresses they evaluate to and not the values stored at those memory locations which is probably what you want. You should use strcmp instead to compare the strings pointed to by above two variables.
strcmp(apcWordList[i].pcWord, sSecretWorD) == 0

Why does my palindrome function always return 1?

I'm writing a function for my homework which is supposed to tell if a given string is a palindrome or not.
Although I even tried it on paper with the word "otto", my program always returns 1.
Although this is a quite common question, I'd really like to know what I'm doing wrong instead of just copying a solution from here.
int is_palindrom(const char* palin)
{
int size = strlen(palin), i=0;
for (i=0;i<=(size/2); ++i)
{
if(palin[i] != palin[(size - i -1)])
{
return 1;
}
}
return 0;
}
Your code is correct, however please note that you may have an inverted logical expression. You are returning 1 in case of not equal, and 0 when it is. This means your function is working the opposite of "standard" C functions, where 1 evaluates to true.
Obviously, you are free to use whichever value you like to represent whatever you want. However, this can easily lead to confusion if someone else is reading your code. If bool is available, you should be using that; otherwise, you should always assume 1 is true and 0 is false.
Also, make sure to note is_palindrome takes a string and not an integer.
i.e. you must call it as is_palindrome("767") and not is_palindrome(767)
Your code does return 0 when it should. I am guessing when you read the string you pass as argument to your function, there are extra characters appended to the string, most probably a new line character. Try debugging the application or adding debug output in the function. For instance print the length of the string and the ascii codes of the characters in it.
Here is the code I used to verify it:
#include <stdio.h>
#include <string.h>
int is_palindrom(const char* palin)
{
int size = strlen(palin), i=0;
for (i=0;i<=(size/2); ++i)
{
if(palin[i] != palin[(size - i -1)])
{
return 1;
}
}
return 0;
}
int main(void) {
printf("%d", is_palindrom("otto"));
return 0;
}
Make sure your (const char *) has a "\0" at the end when you call this function.
#include<stdio.h>
#include<conio.h>
int is_palindrom(const char* jj);
int main(char *args){
int rr = is_palindrom("otto");
printf("rsult is %d", rr);
getch();
}
int is_palindrom(const char* palin)
{
int size = strlen(palin), i=0;
for (i=0;i<=(size/2); ++i)
{
if(palin[i] != palin[(size - i -1)])
{
return 1;
}
}
return 0;
}
I ran you code using above code snippet and it work fine for me.it returns 0 if palindrome is entered and 1 if entered value is not palindrome. the main part of the function is the loop
for (i=0;i<=(size/2); ++i) and the comparison if(palin[i] != palin[(size - i -1)]) the loop starts from 0 and then in condition palin[0] element and palin[4-0-1] i.e palin[3] element first o and last o in this case are mapped then the increement ++i takes place and then nest mapping of palin[second] and palin[second-last] elements happen so you can you either `++i' or 'i++'

Resources