Segmentation fault (core dumped)

Hi everyone, I'm trying to make a program that reads a few strings from a file and writes it out in another file in which every '\n', '\t' are changed in ' '. It compiles but when I run it it crashes and return "Segmentation fault (core dumped)". Does anyone knows how to fix it without allocate the memory statically?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
#define MAX 300

void concatena1(char *pun, char s[][MAX], int *i, char *c){
  if(strlen(pun)){
    pun=strchr(s[*i],'\t');
    strncat(c,s[*i],((long)s-(long)pun));       
    concatena1(pun,s,i,c);
  }
  else{
    strcat(c,s[*i]);
    return;
  }
}

void concatena(char s[][MAX], char *c,int *righe){
  for(int i=0;i<*righe;++i){
    char *pun;
    concatena1(pun,s,&i,c);
  }
  *righe--;
}      


int main(int argc,char *argv[]){
  ifstream entrata;
  ofstream uscita;
  char s[MAX][MAX];
  entrata.open(argv[1],ios::in);
  if(entrata.fail()){
    cerr << "Il file " << argv[1] << " non esiste\n";
    exit(0);
  }
  int j=0;
  int lunghezza=0;
  do{
    entrata.getline(s[j++],MAX-1);
    lunghezza=+strlen(s[j-1]);
  }
  while(entrata.eof());
  entrata.close();
  char c[lunghezza];
  concatena(s,c,&j);
  uscita.open(argv[2],ios::out);
  uscita<<c;
  uscita.close();
  return 0;
}
Last edited on
Ciao, vedo che sei italiano. Anche io... :)

Anyway this is an international forum so...
My first question is: why do you want to use the old char* representation of a string? Why not using directly std::string? It is much easier to manipulate and less error prone!
Then there is a (little) mistake at line 43: it should be += and not =+. Anyway that is not the cause of the segfault. Almost certainly you are making a mistake with pointers. This happens because it is quite easy to do something wrong with them, and this is the reason why you should try to avoid using pointers as much as possible!
foo.cpp: In function ‘void concatena(char (*)[300], char*, int*)’:
foo.cpp:26:10: warning: value computed is not used [-Wunused-value]
  *righe--;
http://www.cplusplus.com/doc/tutorial/operators/ (precedence)
You are decrementing the pointer, and then dereferencing

foo.cpp: In function ‘int main(int, char**)’:
foo.cpp:47:18: warning: ISO C++ forbids variable length array ‘c’ [-Wvla]
  char c[lunghezza];
that's a gcc extension, that violates the c++ standard.

foo.cpp: In function ‘void concatena(char (*)[300], char*, int*)’:
foo.cpp:24:25: warning: ‘pun’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   concatena1(pun,s,&i,c);
¿why is `pun' a parameter?

> ((long)s-(long)pun)
¿why the casting?

> void concatena1(char *pun, char s[][MAX], int *i, char *c)
¿why int *i instead of simply int i?


1
2
3
4
5
  do{
    entrata.getline(s[j++],MAX-1);
    lunghezza=+strlen(s[j-1]);
  }
  while(entrata.eof());
bad reading method, in the last iteration you do process a bad input
Also, ¿why do you increment the index there, if you are going to use the previous value?
1
2
3
4
while( entrata.getline(s[j], MAX-1) ){
   lunghezza += strlen(s[j]);
   ++j;
}



> how to fix it without allocate the memory statically?
I'm not sure if you know what you are saying. ¿why do you want dynamic allocation?


Also, if your program ask for input, you ought to provide an example input
Last edited on
Topic archived. No new replies allowed.