Segmentation fault message in dev C++

Hi,
when i run this program, the program hangs when i enter input value as CTRL +Z.
Also i am getting a segmentation fault when i debug this program.
also please check the for loop at the end after the while. strange thing is that for all values of i, im getting the same char* value as the last one i entered.
Please check out the below code..

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int getline(char[],int);
int readlines(char*[], int);
//char *malloc(int);
void sorts(char** entry,int);
main()
{
char* entry[1000];
char* a;
int i = 0,nlines;
char s[10];

if((nlines = readlines(entry, 1000)) > 0);
//cout << nlines;
//entry[i++] = a;


//sorts(entry,i);

for( int j = 0; j < i; j++ )
{

if( j == 0)
cout << entry[j]<<"\n";
else
{
int k = 0;
bool ispresent = false;
for( k = j-1; k >=0 ;k--)
{
//cout << j;
if(entry[j] == entry[k])
{
// cout << j;
ispresent = true;
break;
}
if( k == 0 && !ispresent)
{

cout << entry[j]<<"\n";
}

}


}
}
system("pause");
return 0;
}
void sorts(char** entry,int i)
{
cout << ++(entry[1]);
char k;
for(int j = 0; j < i; j++ )
{
while( k!='\0')
{
cout << "entered loop \n";
cout << entry[j];
}
}
}



int getline( char s[], int lim)
{

int c,i;
for( i = 0; i < lim-1 && (c = getchar())!=EOF && c!='\n';i++)
s[i] = c;
if( c=='\n'){
s[i] = c;
++i;
}
s[i] = '\0';

return i;

}

int readlines(char* lineptr[], int maxlines)
{

int maxlen = 1000;
int len, nlines;
char *p ;
char lines[maxlen];
nlines = 0;
//len = getline(lines,maxlen);
//cout << len;
while((len = getline(lines,maxlen)) > 0 ){
//cout << lines;
if(nlines >= maxlines)
{
cout << "entered if\n";
return -1;
}
else{

lines[len - 1] = '\0';
strcpy(p,lines);
lineptr[nlines] = p;
cout << lineptr[nlines];
nlines++;
}
}
for( int i = 0; i < nlines; i++)
cout << lineptr[i]<<"\n";
return nlines;
}
1
2
3
char *p ;
   // ...
strcpy(p,lines);


What does p point to and why do you think it's okay to write to what p points to?
Hi,

P points to an array of chars.i am copying the array of chars(lines) to p because lineptr is an array of pointers to char. Is something wrong here?
because the program hangs only when i enter ctrl+Z to terminate the input(EOF).
p does not point to an array of chars.

p is a wild pointer that may point anywhere unless the code you have posted is not the actual code.
Where are you allocating any arrays? I see alot of pointers but no allocations (new or malloc). But then again it's hard to read unformatted code, use [code ][/code ] tags.
Last edited on
in the getline function,lines is an array or chars whos length is returned by getline which is called in readlines function. since there is a while loop with a condition that len .....> 0, when i enter CTRL +Z, len = 0, hence it does not enter the while loop. Anything wrong here ? This is one of the example programs in Retchie's C book. It worked fine using borland c compiler though.
As cire noted you are copying lines to p, but p hasn't been allocated any memory.
yeah i think this is where i missed to malloc..
......
while((len = getline(lines,maxlen)) > 0 )
if(nlines >= maxlines)..
it should have been if(nlines >= maxlines||((p = (char*) malloc(len)) = NULL)
.......
......

lemme try this.

anyways where are you people located? cire and naraku9333 ?
Last edited on
You have a few other problems:

1
2
int maxlen = 1000;
char lines[maxlen];


isn't legal C++.

getline is capable of a buffer overrun (the for condition should be i < lim-2.

getline returns the length of the c-string found, and not the actual size so your malloc(len) would be allocating an incorrect size. Should be malloc(len+1), but why are you using malloc in C++?



Last edited on
im in the process of moving from c to c++. so i guess in many places, i have written for c way. for..

1 int maxlen = 1000;
2 char lines[maxlen];

i tried using a macro for maxlen. but got some error. i will change this. and also i think u are referring to the new operator in c++ instead of malloc.
but the point is dev c++ compiler comes with a stdio.h which does support C like commands. i have also included stdio.h in my code.
Do u think this is totally illegal ?
Yes, it's completely illegal.

const unsigned maxlen = 1000 ;
char lines[maxlen] ;

is legal (and you should prefer unsigned types for representing sizes.)

Note that the way you did it is legal for C variable length arrays, but we don't have those in C++.

You may also wish to check out:

http://www.cplusplus.com/articles/36vU7k9E/
great thanx!!
i dont like dev c++ anyways. i tried running turbo c++ 3.0, since i have windows 7 64 bit machine, i also tried mounting the TC directory on the MS DOS X shell and running turbo c++. but i am facing some issues doing that.

i did download visual c++ express 10.0 which was an evaluation version.
also i think the best is to try eclipse and use a maven project (pom.xml) to build the project.
which one do u recommend ?
The evaluation version, perhaps more commonly known as the free version, of VC++10 is fully functional, you just don't get the extras you're not paying for.

eclipse is an ide.

TC++ 3.0... You wanted to use a compiler that hasn't been updated since 1991?

For Windows programming, I think the popular free compiler choices boil down to a variant of GCC (either mingw or cygwin), clang, or VC++. There used to be Open Watcom... I don't know if that is still being developed. I haven't tried out clang yet.

The VS IDE is top-notch and the biggest reason I prefer using visual studio.
I got the VS IDE working , thanx a lot for ur advise cire.
Topic archived. No new replies allowed.