When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
486 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Could you provide more information on where the segmentation fault happens? Maybe include the error.
it just says segmentation fault core dumped
There's no additional information at all?
Sometimes you get some debugging information.
when i try to debub
debug
it makes no sense because even when i put a breakpoint at the start where i define the struct it gives me seg
Okay so one thing to test is
Check if the [i] index is actually being checked to exist.
You increment i in the for loops
but you're not checking if the index actually exists, right?
true
What if curso[i] does not exist when you go to ```c
if(cursos[i].curso_codigo == NULL){
puts("memória excedida");
exit(-1);
}
It'll throw an error since it can not evaluate cursos[i], and therefor can't check cursos[i].curso_codigo
because its sequencial?
but it should exist no? i am pulling it from main?
if(cursos[i] && cursos[i].curso_codigo == NULL)
it gives me an error
and when i declare the i inside the function it gives me a warning saying its not being used
I'm not too sure then, can't quickly spot
i think that maybe the error might be in pulling the n_cursos from the main
How did you even get that far without spotting a segfault
Perhaps!
could you check this ?, i am not experienced enough to know even if i say this
what you can do is try to printf("%d, i)
debug it by adding a printf statement inside the loop
see where it beaks
breaks
print every iteration value
it doesnt reach the line after for
1, 2, 3 etc and see where it breaks
even
What is the n_cursos value?
it didnt reach the print
try and print the n_cursos value before the for loop
the n_cursos values is a value i get from reading an excel file and icrementing each time it reads a line
and i am pulling it from main
But
What is it's value before going into the loop
you just said the loop isn't doing anything
as it's not printing
I'm confused now.
It's not doing anything inside the for loop?
Cause it's not printing the i
this is the snippet of the code that counts n_candidatos and n_cursos
it is suposed to be alocating memory ig
but it doesnt reach the print
bc of the segmentation default
so now we know which function is not getting reached
That's progress
Honestly, just keep doing the printf's
And figure out where it's actually breaking
like the exact line
It'll make it much easier for you to figure out what's going wrong
And it's a great example to learn from
but if always stops before the prints i will never understand what its causing it
it always stops bc of the segmentation default
Exactly
So find a place that does not stop before the printf
you need to figure out where exactly your code is bugging out
So follow your main's function calls
See which gets called first, check a printf there
gets printed? Move to next function being called
untill it doesn't print
and you have your area to look at
Might sound demotivating, but I do it on the regular.
but i think it doesnt reach the main right? because the 1st function is not printing
Then move the printf up higher
inside the main
untill it does print
and figure out which function is failing
or not even function, but maybe it's an assignment to a variable
so just keep moving the printf untill it prints
oh the main should be the 1st thing and the functions should be after the main?
What I mean is.
printf("This code has been reached without any errors.");
place that inside your main
Preferably after declaring your variables.
And see if it prints, if it does, move it down another line.
Untill it does not print
When it stops printing, you know which line/function is causing your error
What
ye
yes i did
the file? the input file?
No ; here.
Well, I mean both.
You need to save your code changes before executing it
they both are
i couldnt tell you realy
i dont know
what editor are you using
Cause those green lines seem to be indicating unsaved changes.
codeblocks from virtual machine
Nevermind, green lines are modified + saved
Honestly, it seems like you're program is not even running
How can it be working well if your main function is not executing?
I mean, there's obviously something sketchy going on. Almost like it's a different file.
the reason i dont think its the program fault its because the university i attend tells us too use it
I meant his code
But he seems to mean IDE
😦
The IDE seems okay, I see Valgrind in there and error logs.
warning flags
But how can his main function not execute the printf method
even if it's the first line.
Oof
try running the program straight from a terminal
Not sure if he knows how
gcc
same here :p
but the command to run is which ?
gcc filename
The IDE is already compiling it probably, yoiu just need to find the binary and run it
yes, idk how that IDE does it specifically tho
lel
i can run with valgrind
try that
its like --valgrind-leakcheck=full ?
i have to ask it to a colleague lemme see
you are writing to NULL somewhere
i am checking if its null
ah
could it be this
the 2 ==
nah its not i am tweaking already
everytime i used null was just to check if something was null i dindnt atribute the null value to anything
when you do token = strtok(...)
add an if statement checking whether NULL == token
oh ok
in all of your strtok instances
that makes sense
maybe you should try writing this as a smaller program, with less 'moving pieces' so that you can get one thing right at a time
idk where it was from
but when i ran it
i got an error in print
i lost it
i am trying to find it
iu think the error is in opening the file
tbh
it should be about these variables
because it printed Error segmentation fault
ok know it showed this
.main.c:269:10: error: unused variable 'nota_curso' [-Werror,-Wunused-variable] char nota_curso [i];
The Error it's throwing is because it probably can not find the .csv files.
And besides that you have leaks.
but they are with the right name and in the right directory
Show me your directory
Yes, you're allocating memory
well i didnt write anything to free them yet
Okay let's check if those .csv's are readable.
In the terminal inside your directory
can you do ls -la
And show me output ?
ok i ll do it
i think its sucessful bc it doesnt print the error
The "Error" you see here should be coming from the fopen
the error is not showing up anymore
if you have an error with fopen you should probably exit(EXIT_FAILURE) right there
instead of pritning an error
Okay, your .csv files have read and write access.
(i nthis specific case)
no issue there
ok ill change it
while (fgets(line, 300,fp_candidatos)){
if(joao == 0){
joao = 1 ;
continue;
}
n_candidatos++ ;
}
joao = 0 ;
while (fgets(line, 300,fp_curso)) {
if(joao == 0 ){
joao = 1;
continue;
}
n_cursos++;
}
``` whats the point here?
of joao
the point is to read trought the lines in a loop to find out how many n_cursos is and n_candidatos
without the need of an input
but why do you skip one
bc the file in excel
has the 1st line that is
like the names of the collumns
the problem now is on the function memoria_candidatos
ah ok. you could have just decremented the variable you know? lol
but ok doesnt matter
so the program enters that function but crashes isnide that?
idk what happened but its not crashing
but its not running well
i am going trough each line
with watches
but something doesnt match here
how do you allocate the array cursos
the token changes from 2 to 19 instead of 18
the same as candidatos
how do you allocate candidatos
no, that one fills in the pointers inside each 'Curso' struct
but you are passing to that function an array
looks like you are using VLA
VLA?
variable length array
could you post again what the current state of your file is
the current state of my file?
i changed the name of the files
in my directory
and theres an error that i cannot explain , its so stupid
what is it?
lol
thats spooky
token = strtok(line, ",");
if( token == NULL){
puts("memória excedida");
exit(-1);
candidatos[a].id_candidato = atoi(token);
}
code under exit will never be run
you misplaced the }
lol
you should probably write a function to take care of all that
also hold on why are you using atoi with id_candidato
yeah, the compiler should have complained otherwise
bruh u wont believe
i fixed the erros u were talking abut
and now its segmentation fault again
its because the token cant acess memory
what can be causing this
does it segfault or does it exit
because you are using strcpy in there, but the function gets called before memoria_candidato
you are strcpying to a pointer that has not been initialized yet
ok so i should put this function above?
funcao is a pretty bad name for a function btw lmao
i ll change a lot of things such as names of variables too
but i was trynna get the code to work
and after this starts working i still have to learn how to print it to a file
4 to be specific
and i have till 23.59
its 18.17 here
i am not sure but i think funcao is mean to fill in the date into the candidatos array, right?
yes
but how do you fill in the data into the array if the memory of the array isnt there
How did you guys do trying to solve the errors?
its going
Screw the deadline, focus on actually learning as you debug this issue.
Imagine racing against the clock and forgetting all the issues you encountered.
That's a waste of time.
token = strtok(NULL, ",");
if( token == NULL){
puts("memória excedida");
exit(-1);
}
// now allocate memory to hold a copy of token
char * copy = malloc(strlen(token)+1); //and hten check if null
candidatos[a].curso = copy;
strcpy(candidatos[a].curso, token);
so basically joining the the two loops togethere right?
just dont use a for loop
just use the while?
or dont use a while loop
you already know how many lines you need to process, right?
just process them in one go. with a for loop
the number of lines changes from file to file
oh ok
i understand what ur saying
something like for(i=0;i<n_cursos;i++)
but i need the while loop to exctract the data,no?
i dont need to save in memory integers right?
you just need one loop
something like
for ( i = 0 ...)
{
fgets(line ...); // get a line
token=strtok(line,","); // get a token
newstr=malloc(strlen(token)+1) //allocate space for token
strcpy(newstr,token);
candidato.something = newstr;
// and repeat for the other entries in the struct
}
thats the only way you can properly use malloc btw, if you just use hardcoded values for malloc calls you are just wasting time.
the newstr is a pointer for each of the variabkes candidato has?
so i will have to have lots of newstrs?
hmm not necessairly. newstr can be a single char * variable, and whenever you need to allocate a new string for your strict, you use the same variable
and when u said candidato.something u meant candidato[i].something right?»
theres no problem if you overwrite old values because you have already saved that in the strung
oh yeah
oh ok
that makes sense
and its probably very efficient that way right?
probably, but the best thing about it is that you get to use strlen(token) to get what the actual length of the string is
what does the strlen does
that way you could store a string of length 1000 if you wanted
strlen calculates the length of a string
strlen("hi") returns 2
so i dont need to declare the size of the array as i was doing in the malloc?
what do you mean?
like this 10 is going to be calculated by the strlen
oh yeah. instead of that hardocded 10, you get a numbers thats calculated from the input you get from your file
oh ok thats cool
btw sizeof(char) is just 1. no need to use that
i ll change this function for the one u came up with
i advice you to rewrite the entire thing from scratch
token=strtok(line,","); // get a token
newstr=malloc(strlen(token)+1) //allocate space for token
strcpy(newstr,token);
candidato.something = newstr;
thse are the things that will repeat troughout the function right?
and doesnt this : candidato.something = newstr, | need to come b4 the newstr=malloc(strlen(token)+1) //allocate space for token
yeah. of course sometimes you have integer entries so instead of strcpy you'd use something else
nonono
that would be giving the struct an uninitialized value
i ll use atoi right?
what you want to do is
atoi is bad. use strtol instead
but for the time being you can use atoi i guess
nope you dont need to
the struct has that memory within it
it also has memory for a pointer, but not necessarily for what the pointer points at
So the candidatos[I].id_candidato which is an integer will be accessible with any i
i dont understand what you mean
i was just asking if the code will always know the id_candidato of every i
i being the index
and why do u put +1 on strlen
i think you should probably separate that into a function
need space to allocate the '\0'
strlen return the number of non \0 characters
but you need the delimiter there i nthe end
should i keep the puts and exit?
probably, just to so you know somethings off if you make a mistake
i think you should write a function that looks like this:
like this?
candidato processline ( char * line )
{
candidato currentcandidato;
strtok(blabla); malloc ; strcpy .....
currentcandidato.something = newstr; //or atoi)
strtok(blabla); malloc ; strcpy .....
currentcandidato.something = newstr; //or atoi)
strtok(blabla); malloc ; strcpy .....
currentcandidato.something = newstr; //or atoi)
....
return currentcandidato;
}
Ie write a function that give a line of input from the csv file, returns a struct with all the data from the input line
that way you can later do a for loop:
for (i=0...){
fgets(line ...)
candidatos[i] = processline(line);
}
instead of having the loop related stuff mixed with the data processing stuff
i am kinda lost with this
for (i=0...){
fgets(line ...)
candidatos[i] = processline(line);
}
candidato processline ( char * line )
{
candidato currentcandidato;
strtok(blabla); malloc ; strcpy .....
currentcandidato.something = newstr; //or atoi)
strtok(blabla); malloc ; strcpy .....
currentcandidato.something = newstr; //or atoi)
strtok(blabla); malloc ; strcpy .....
currentcandidato.something = newstr; //or atoi)
....
return currentcandidato;
}
it would stay like this
?
oh you are meant to fill in the gaps lol
what i mean is, currently in your function funcao you are at the same time dealing with an array of candidatos, and with the data processing of each line
and what is candidato processline(char * line)?
thats a function definition
a function that returns a 'candidato'
you input a line, you get back your struct
what does returning the currentcandidato do?
what the function does is define a local variable currentcandidato
and it takes data from the 'line' and fills the local variable with data from there
yes
then you return the struct with all the data already filled in
yes that very smart
is this malloc suposed to be in beetween of semi colons ?
sorry i was just trying to write in shorthand
it would be pretty much the same as you had here
except with currentcandidato instead of candidato[a]
~where should i put the strlen
and i am not rly understand if the malloc stays on a line alone or if not
it doesnt matter in C
so the strcpy can be on the same line as malloc?
int i = 0 ; i += 4;
//same as
int i = 0;
i+=4;
put them each in a different line
for readabilty
but it doesnt actually matter to the C language
and i am still going to use this char * newstr = malloc(strlen(token)+1); //and hten check if null
currentcandidato.curso = newstr;
strcpy(currentcandidato.curso, token);
but not with copy there obiously
and curso changes troughout
ah ok
and you could call that copy or newstr or whatever
its the same idea
I gotta go. my advice is dont try getting everything in one step. try to incrementally get where you need to be. write a program that just processes one of the files, or only a single line, and then go to the next step. no need to get everything right from the getgo
good luck!
tks a lot man
@obtuse vapor can u help me understand something cardiou coded
can u understand that this is what he was trying to do
i am not 100%sure
and my debugger is not running
@obtuse vapor are u there man
its 30 min to enter
and i rly need to sort one thing
Sorry man I got stuck on my own project, didn't get to read your messages.
I hope you figured it out in the end..
i did
Nice!
This question is being automatically marked as stale.
If your question has been answered, run !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.
!solved