I have some problems with the code below. (I truncated it, because its much easier to understand this way)
The problem is that the program exits with Seg. Fault., and I cannot figure out why.
#include <math.h>
#include <stdlib.h>
#include <string.h>
void novel(char *** names, int meret){
int i = 0;
if(meret == 0){
*names=(char**)malloc(sizeof(char*)*10);
while(i<10){
*names[i] = (char*)malloc(sizeof(char)*50);
i++;
}
}
return;
}
void olvas(){
char** names;
int meret = 0;
novel(&names, meret);
}
int main(){
olvas();
}
However, if I don't pass the "char** names" to the other function, rather than allocating it within the "olvas" function, everything works fine.:
void olvas(){
int i = 0;
char** names;
names=(char**)malloc(sizeof(char*)*10);
while(i<10){
names[i] = (char*)malloc(sizeof(char)*50);
i++;
}
//int meret = 0;
//novel(&names, meret);
}
int main(){
olvas();
}
I use gcc, but I dont really know what is the proper way to find the cause of the problem with gdb.
I can find which line causes the problem:
Program received signal SIGSEGV, Segmentation fault. 0x08048a46 in novel (names=0xbffff57c, meret=0) at fgvek.c:25 25
and I can check the value of the variables, but I cannot figure it out.
(One more question what is not related to this topic : Should I keep using gcc+gdb, or should I look for an IDE)
Thanks in advance,
qpai


Sign In
Create Account

Back to top









