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 4 years ago.
Improve this question
The if(ptr->is_word) on the sizeH(node *ptr) function is causing a segmentation fault.
dictionary.c:120:13: runtime error: member access within null pointer of type 'node' (aka 'struct node')
dictionary.c:120:13: runtime error: load of null pointer of type '_Bool'
Segmentation fault
If I add an if condition to return a specific value before this if-condition, the code executes and I make sure the root isn't null
so I guess the problem is the ptr isn't passed correctly to the sizeH function.
unsigned int size(void)
{
node *ptr = root;
printf(ptr->is_word ? "true" : "false");
unsigned int count = sizeH(ptr);
printf("COUNTER IN SIZE %d\n" , count);
return 0;
}
unsigned int sizeH(node *ptr){
if(ptr->is_word)
{
return 1;
}
else
{
for(int x = 0; x < N; x++){
return 0 + sizeH(ptr->children[x]);
}
}
return 0;
}
There's a couple of fundamental problems with your sizeH function. Firstly it doesn't check that the ptr that is passed in is not NULL. This is very likely to happen as your function is recursive and you have this loop that is called for each child node.
for(int x = 0; x < N; x++){
return 0 + sizeH(ptr->children[x]);
}
Except that the code in the loop is wrong and it'll only ever be called for the first child, which is the second issue. Because you use return inside the loop, it'll only ever run once. Instead you should tally up the values returned for each child node and then return that.
Making the two changes above would leave your function looking like this.
unsigned int sizeH(node *ptr) {
if(ptr==NULL) {
return 0;
} else if(ptr->is_word) {
return 1;
} else {
unsigned int ret = 0;
for(int x = 0; x < N; x++) {
ret += sizeH(ptr->children[x]);
}
return ret;
}
// return 0; This isn't needed - the code will never reach here.
}
It's also worth picking one coding formatting style and sticking to it rather than using a mixture as it makes for neater code.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have two code snippets. Both give me a warning about using uninitialized variables, however, the former throws a segmentation fault while the latter DOESN'T. Please point out what has caused the difference here.
EDIT: They said it is undefined behaviour. So to be clear, i make a char **eligible_file which is undefined, then how can i get around with this without setting a fixed size for the variable?
the first:
glob_t glob1;
glob("*.log", GLOB_ERR, NULL, &glob1);
char** file_name = glob1.gl_pathv;
int file_num = glob1.gl_pathc;
char** eligible_file;
int j = 0;
if (compare_string(argv[1], "-o")) {
for (int i = 0; i < file_num; i++) {
int rc = file_or(file_name[i], argv, 2, argc);
if (rc == 0) {
eligible_file[j] = file_name[i]; // the fault occurs here
j += 1;
}
}
} else {
for (int i = 0; i < file_num; i++) {
int rc = in_file(file_name[i], argv, 1, argc);
if (rc == 0) {
eligible_file[j] = "xasdax"; // the fault occurs here
j += 1;
}
}
}
the latter:
char** fake;
char* me[] = {"qwedsa", "wqdxs", "qwdsx"};
if (1) {
for (int i = 0; i < 3; i++) {
fake[i] = me[i];
}
} else {
for (int i = 0; i < 3; i++) {
fake[i] = "wqxsaa";
}
}
In both code snippets you write to a pointer without allocating memory for it.
1st snippet:
char** eligible_file;
...
eligible_file[j] = file_name[i]; // the fault occurs here
j += 1;
and
char** eligible_file;
...
eligible_file[j] = "xasdax"; // the fault occurs here
j += 1;
2nd snippet:
char** fake;
fake[i] = me[i];
...
fake[i] = "wqxsaa";
I am almost sure that in all cases, the error appears when the array index becomes greater than 0 - that is when writing in un-allocated memory occurs.
You should allocate memory in either of two ways:
Use proper arrays instead of pointers;
use malloc() or other similar function to allocate (enough) memory.
"undefined behavior" = whatever happens, good or bad, do not ask "why?"
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 4 years ago.
Improve this question
I have the following code* inside an if else {} code block but the GCC compiler throws me this error:
hw2b.c: In function `printSymbolK':
hw2b.c:26: parse error before `int'
hw2b.c:27: `w' undeclared (first use in this function)
hw2b.c:27: (Each undeclared identifier is reported only once
hw2b.c:27: for each function it appears in.)
*
int w = 0;
for(w = 4; w < i; w++) {
printf(" ");
}
Can someone tell me what i'm missing here? The w variable is declared and initialized inside the if else block but it says it isn't. Any ideas?
EDIT: You were right, i only copied a small piece of my program. Here is the full code:
#include <stdio.h>
/*
* The printSymbolK function accepts a
* natural number (N > 3) and prints the
* symbol K to the console according to certain
* specifications.
*/
void printSymbolK(int N) {
int i = 0,k=0;
int katoAkeraioMeros = N/2; // to apotelesma tou pilikou tha strogkilopoihtei pros ta kato dinontas mas to kato akeraio meros tou N/2
char star = '*';
for(i = 0;i < N; i++) {
if( i < katoAkeraioMeros) { // katoAkeraioMeros - i+1 = posa tha einai apo pano
int res = katoAkeraioMeros - 1; // poses fores tha trexei i epanalipsi
int l = 0;
printf("%c",star);
for(l = i;l < res;l++) {
printf(" ");
}
printf("%c\n",star);
} else if(i+1 == katoAkeraioMeros) {
printf("%c%c\n",star,star);
} else if(i > katoAkeraioMeros) { // auxise to space kata 1 gia kathe i apo edo kai pera
printf("%c",star);
int w = 0;
for(w = 4; w < i; w++) {
printf(" ");
}
printf("%c\n",star);
} else {
printf("%c %c\n",star,star);
}
}
}
int main() {
// Stelios Papamichail 4020
int n = 0;
do {
scanf("%d",&n);
printf("\n\n");
} while(n <= 3);
printSymbolK(n);
return 0;
}
the first error says it all:
hw2b.c:26: parse error before `int'
So something triggered a syntax error before the int token.
you shouldn't worry about further errors. at this point the parser is confused and thinks you're using w whereas you're trying to declare it (compilers don't stop at the first error so you can fix several errors at once, but sometimes that backfires, and sometimes they stop at some error, and when you fix it, you get more errors... compiler does its best with invalid code)
Just fix the first error, and retry.
Without seeing the full code, I would guess your using an older C compiler (C89) and you have code that is neither variable definition nor declarations above 'int w = 0;'.
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 4 years ago.
Improve this question
#include<stdio.h>
int encryption_check(char* s , char* t)
{
printf("%s",s);
int i=0 , diff = t[0] - s[0];
while(s[i]!='\0' && t[i]!='\0')
{
if(t[i]-s[i]<=0)
{
return -1;
}
else if(t[i]-s[i]!=diff)
{
return -1;
}
}
return diff;
}
int main()
{
int d = 0;
char s[]= "abc";
char t[]= "def"; // s=plain text and t=cipher text
d = encryption_check(s,t);
printf("%d",d);
return 0;
}
encryption_check() returns the difference between ciphered and plain text.
This is giving segmentation fault for some reason.
Also I would like to know how to pass strings of random (not fixed) length to encryption_check().
If not getting into details of what your encryption_check should actually do and keep it short, your while loop can't terminate as your i never changes after being set to 0.
On the first run, s[0] = 'a' and t[0] = 'd' so t[0] - s[0] = 3 and so the conditions in while will be true. On the consequent runs, as you don't iterate i, the loop won't stop.
To fix it, just add i++ as below:
while(s[i]!='\0' && t[i]!='\0') {
if(t[i]-s[i]<=0) {
return -1;
} else if(t[i]-s[i]!=diff) {
return -1;
}
i++;
}
This question already has answers here:
Best practice for compute the function return value
(5 answers)
Closed 5 years ago.
I am trying to get rid of rule 15.5 from my code. This is basically because of multiple returns in the function.
Code looks like:
int32_t
do_test(int32_t array[])
{
for(int32_t i=0; i < VAL; i++)
{
if(array[i] == 2) {
return 1;
}
}
return 0;
}
I have tried with a temp variable which store the return value and returning this variable at the end. But that didnt work.
Any suggestions ?
You need to store a temp variable and to break the loop
int32_t
do_test(int32_t array[])
{
int32_t result = 0;
for(int32_t i=0; i < VAL; i++)
{
if(array[i] == 2) {
result = 1;
break; // !!
}
}
return result;
}
int procurarMatriculaLista(tipoEspera listaDeEspera[], int ClistaDeEspera, char matricula[])
{
int i, pos= -1;
for(i = 0; i<ClistaDeEspera; i++)
{
printf("coock");
if(strcmp(listaDeEspera[i].matricula, matricula) == 0)
{
pos=i;
i=ClistaDeEspera;
}
}
return pos;
}
It has no errors but my program crashes whenever I call this function. Why?
If it's crashing in that function, the most likely cause is that the arguments being given to strcmp are causing it to fail. Such as if they're not actually C-style strings, or if the length value (ClistaDeEspera) you're passing in is too big for the actual array.
Hence you need to check listaDeEspera[i].matricula where i ranges from 0 to ClistaDeEspera - 1 inclusive, and matricula.
Most likely one of those values is not what you think it is.
As an aside, your code could be made "cleaner" by getting rid of the store-position-and-force-loop-end aspect. With a small piece of code like this, multiple return points have no real detrimental affect on readability:
int procurarMatriculaLista(tipoEspera listaDeEspera[], int ClistaDeEspera, char matricula[]) {
for (int i = 0; i < ClistaDeEspera; i++)
if (strcmp (listaDeEspera[i].matricula, matricula) == 0)
return i;
return -1;
}