Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Sorry for the long codes. I'm developing a command program that tokenizing the user commands. If I enter "Hello world "hi guys" ",
temp[0] = "Hello"
temp[1] = "world"
temp[2] = "hi guys"
Now I'm struggling with the store_token function. I have to print tokens like this.
tokens[0] = "Hello"
tokens[1] = "world"
tokens[2] = "hi guys"
temp is a temporary bridge that stores cut tokens. So I write like this
(*tokens)[i] = (*temp)[i]
because 'tokens' is char *[], and I want to access the element of 'tokens'
to store the char * of the temp.
Here is some problem. I can't figure out the (*tokens)[i] and I don't know
what it directs.
This is a problem in code readability. You have some code that almost works; but, because you have not invested in readaility, you can't find your error.
I would start by clarifying the "reset / complete the token operation" and "add to token" operations. If any code flow doesn't do one or the other, there's a bug.
for (int i = 0; i < clen; i++) {
if (*command == *"\"" ) {
isnested = !isnested;
continue;
} else if (*command == *"\n") {
toklen = 0;
ntoken++;
} else if (isspace(*command) && !(isnested)) {
if (doublespace) {
*command++;
continue;
} else {
doublespace = true;
ntoken++;
toklen = 0;
}
} else {
doublespace = false;
temp[ntoken][toklen] = *command;
toklen++;
}
*command++;
}
*nr_tokens = ntoken;
Assuming that adding to toklen indicates "add to the token" and toklen = 0 signals a "complete the token" I would rewrite the code
for (int i = 0; i < clen; i++) {
if (*command == *"\"" ) {
isnested = !isnested;
continue;
} else if (*command == *"\n") {
completeToken();
} else if (isspace(*command) && !(isnested)) {
if (doublespace) {
*command++;
continue;
} else {
doublespace = true;
completeToken();
}
} else {
doublespace = false;
addToken(*command);
}
*command++;
}
*nr_tokens = ntoken;
And now a few things are clear.
There are code flows through your algorithm that don't add to the token and don't complete the token. These code flows need simplified and put up front (so everyone can see what isn't important).
You don't add the quotes to a isnested token. Maybe your lexer strips the quotes; but, if it does, there's no token completion when we are exit the quotes.
Your lexer handles new lines without regard to the isnested variable. Odds are it should be adding the newline to the string if inside a set of quotes.
Your lexer is very concerned with double spaces. Normally, if spaces are being stripped, they are stripped without regard to how many exist.
Even in small blocks of code, readability matters. C's history ignores modern readability rules, as the rules came out after the language; but, you would do well to apply them anyway, so you can get the modern readability benefits within the C operating enviornment.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to develop a Operative System.
It works, but the function "print" that I've written has an error:
If I print two Strings only one will be displayed, and the command \n does not work.
My questions are:
How to use the command \n?
and
How to create the function clear without using libraries?
This is the function print:
void print(char* message, int color)
{
char* mem = (char*)(0xb8000);
while(*message != 0)
{
*mem = *message;
mem++;
message++;
*mem = (char*)color;
mem++;
}
}
PS It Works, but the problem is that if I use the function print two times (with or whitout \n it prints me only the second part.
To simulate a line break you probably need to write as many spaces to the video memory as there are characters left in the current line. Hence, you need to remember how many characters you wrote to the current line.
The same applies for scrolling. You need to copy the memory in a way that the last line on the screen becomes blank again.
You should have some context of the printing function.
eg: you should have a global(or static) variable to remember your last printed position of the video output memory.
something like:
```
static int last_pos = 0;
void print(char* message, int color)
{
char* mem = (char*)(0xb8000);
mem += last_pos * 2;
while(*message != 0)
{
if(*message == '\n') {
set the mem to next line and calculate the new last_pos
continue;
}
*mem = *message;
mem++;
message++;
*mem = (char*)color;
mem++;
last_pos ++;
if (last_pos >= max_video_buffer) {
move content the buffer to scroll the screen
or
simply reset the last_pos and mem to restart from top-left.
}
}
}
```
when do clearing: just fill the whole video output memory to empty and reset the last_pos
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So, I have some class work that I cannot solve. I could get the code to work partially but now it is just useless. It either freezes (doesn't complete running) or it will give me several inf in the first row.
#include <stdio.h>
#include <math.h>
int main()
{
double a[10][11]={
{3.55618, 5.87317, 7.84934, 5.6951, 3.84642, 9.15038, -1.68539, 5.03067, 7.63384, -1.75626, -1.92193},
{-4.82893, 8.38177, -0.301221, 5.10182, -4.1169,-6.09145, -3.95675, -2.33365, 1.3969, 6.54555, -2.35262},
{-7.64196, 5.66605, 3.20481, 1.55619, -1.19814, 9.79288, 5.35547, 5.86109, 4.95544, -9.35749, 2.27709},
{-2.95914, -9.16958, 7.3216, 2.39876, -8.1302, -7.55135, -2.37718, 7.29694, 5.9867, 8.5401, -2.67493},
{-8.42043, -0.369407, -5.4102, -8.00545, 9.22153, 3.96454, 5.38499, 0.438365, 0.419677, 4.17166, 1.84756},
{6.02952, 4.57728, 5.46424, 3.52915, -1.01135, -3.74686, 8.14264, -8.86961, -2.88114, 1.29821, 4.154126},
{0.519819, -6.16655, 1.13216, 2.75811, -1.05975, 4.20286, -3.45764, 0.763558, -0.281287, -9.76168, -.93387},
{5.15737, -9.67481, 9.29904, -3.93334, 9.12785, -4.25208, -6.1652, 2.5375, 0.139195, 2.00106, -1.28356},
{-4.30784, 1.40711, -6.97966, -9.29715, 5.17234, 2.42634, 1.88818, -2.05526, -3.7679, 3.3708, -3.46841},
{-4.65418, 7.18118, 6.51338, 3.13249, 0.188456, -16.85599, 7.21435, -2.93417, 1.06061, 1.10807, -2.61529}};
int i, j, k, l;
double b[10][11];
i=0;
while(i<10)
{
j=0;
l=i;
while(l<10)
{
j=0;
l++;
while(j<11)
{
This code below works fine. When used on its own, it will turn all the diagonal values into 1.
b[i][j]=a[i][j]/a[i][i];
This code used below to work partially, setting the first column equal to 0, but now it is useless. I tried to manipulate it into getting rid of all values by having that 0 be a k, with k++ within the while code, but it would return either a segmentation code if I placed it by the i++; or a bus error is placed in front of the j=0;, and it would just freeze the program if I placed the i in the 0. Now it is useless that I restored it to default
b[l][j]=a[l][j]-b[i][j]*a[l][0];
j++;
}
}
i++;
}
j=0;
i=0;
while(i<10)
{
while(j<11)
{
printf("%lf\t", b[i][j]);
j++;
}
printf("\n");
i++;
j=0;
}
return 0;
}
How do I go about solving all these issues within my C program?
Not 100% sure what your code is up to, but I see at least 1 place where I believe that you are reading off the end of your arrays:
while(l<10)
{
j=0;
l++;
...
b[l][j]=a[l][j]-b[i][j]*a[l][0];
Because you are incrementing after you check for max size, you have 1 iteration where you are probably reading passed the max size of the array.
To check, put some prints on your indexes in your code where you do your assignments or assert them or something and I think you will find the source of your woes.
Also, PLEASE use constants for your sizes instead of the magic numbers all over the place. You will thank me later when trying to do a calculation of a matrix of a different size.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am facing a very strange problem.
- I have an if condition:
if((IN_SYNC == sync_flag) || (cycle_number == spi_slot_number))
Before the condition, the variable "spi_slot_number" is '7' and after the if condition it was turned to '0' (which is the value of "cycle_number") !!!
Does any one knows how can such thing happen ?!
Important notes:
1- My code is in C language.
2- I checked the stack before and after the condition to make sure no stack corruption happening.
3- My program is one thread program, so no interrupts or other threads can interrupt.
4- If I commented the if condition , every thing goes fine.
5- I don't know how to generate the assembly code ...
As said by others, you haven't shared enough code to actually identify where your problem is. One thought that occurred to me however is the IN_SYNC identifier. It's a standard coding convention to put macros in all caps, and if it is a macro, it's possible that it's doing the dirty deed. Check for definition of IN_SYNC.
One other thing -- your if test has two tests, separated by an || operator. Try breaking the two tests apart to see which one is causing your side effect. Something like this:
printf("%d \n", spi_slot_number);
if (IN_SYNC == sync_flag) {
/* do nothing */
}
printf("after IN_SYNC test %d \n", spi_slot_number);
if (cycle_number == spi_slot_number) {
/* do nothing */
}
printf("after cycle_number test %d \n", spi_slot_number);
What happens if you do comparision on temporal copy of spi_slot_number? Does it work as expected?
void GetData(slot_id_T spi_slot_number, uint8_t* data_received,
uint16_t data_length, uint8_t data_is_valid_flag)
{
uint8_t cycle_number;
slot_id_T copy = spi_slot_number; // <- ADDED
cycle_number = GetCycleNumber() + 1;
if(cycle_number > LAST_CYCLE)
{
cycle_number = 0;
}
printf("%d \n", spi_slot_number);
if((IN_SYNC == sync_flag) || (cycle_number == copy)) // <- CHANGE
{
printf("%d \n", spi_slot_number);
switch(data_is_valid_flag)
{
case DATA_IS_VALID:
SendData(spi_slot_number, p_buffer, data_length);
break;
case DATA_IS_NOT_VALID:
IndicateDataNotValid(spi_slot_number, p_buffer, data_length);
break;
default:
/* Do Nothing */
break;
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why I am getting runtime error (SIGSEGV) on the following code:
#include<stdio.h>
#include<string.h>
int main()
{
int t_line,count[10000],i;
scanf("%d",&t_line);
for(i=1;i<=t_line;i++)
{
fflush(stdin);
gets(t);
count[i]=(int)t[0]+(int)t[1]+(int)t[2];
}
for(i=1;i<=t_line;i++)
printf("%d\n",count[i]);
return 0;
}
I have also tried to solve this problem by initialized all the elements of array.
I am wondering how the code compiled, without declaration of the variable t. But, still the only missing elemnt was: char t[your choice of size];. Apart from that
#include<stdio.h>
//#include<string.h> No need of this header,a s you are not using any string functions
int main()
{
int t_line,count[10000],i;
char t[64];//you need to declare the variable before using it
scanf("%d",&t_line);
//Its safer if you check this
if(t_line >= 10000)//if you use 0 and < t_line in for loop below then change the condition to: if(t_line > 10000)
{
printf("ERROR: Limit exceeded. Not enough memory.\n");
return 1;//or you could use exit(1); and #include <stdlib.h>
}
for(i=1;i<=t_line;i++)//suggested: for(i=0;i<t_line;i++)
{
//fflush(stdin);
//gets(t);
char *rc = fgets(t, sizeof(t), stdin);
if(rc != NULL)
{ t[strlen(t) - 1] = '\0';\\because fgets gets the \n into the string too. This line makes fgets similar to gets, improving safety from overflow.
}
else
{
// handle fgets failed error
}
count[i]=(int)t[0]+(int)t[1]+(int)t[2];
}
for(i=1;i<=t_line;i++)//suggested: for(i=0;i<t_line;i++)
printf("%d\n",count[i]);
return 0;
}
Find the solution and suggested changes inline as code comments.
In C, its better to use indexes starting from 0, unless there is a specific requirement to use other values.