Thread: For loop leads to segmentation fault
hello everyone,
little code of mine. problem not written @ title of thread, there more.
first of all, here code (if need know file process contains, post information too):
so, here problems havecode:#include <iostream> #include <zlib.h> #include <fstream> using namespace std; z_stream z; int _decompress(bytef * in_data, long in_size, bytef * out_data, long * out_size){ int status; z.avail_in = in_size; z.next_in = in_data; z.avail_out = *out_size; z.next_out = out_data; status = inflateinit(&z); //segmentation fault if ( status != z_ok ) return status; if ( z.avail_in != 0 && *out_size != 0 ) { status = inflate( &z, z_no_flush ); //segmentation fault if ( status < 0 ){ cout << "error: " << status << endl; return -11; } else if ( status == z_ok && *out_size > 0 ){ cout << "not enough output space!\n"; return -12; } }else if( *out_size != 0 ){ cout << "no input?\n"; return -10; } while ( status == z_ok ) { *out_size += 1024; z.avail_out = *out_size; delete out_data; out_data = new bytef[*out_size]; z.next_out = out_data; status = inflate( &z, z_no_flush ); //segmentation fault } if ( status != z_stream_end ); cout << "final error: " << status << endl; (void)inflateend(&z); return status; } int main(){ ifstream fp_in; fp_in.open("r.0.0.mcr", ios::in); char *a = new char[5]; for(int = 0; i<1024; i++){ fp_in.seekg(i*4); fp_in.read(a, 4); cout << << ":"; if((int)a[0]!=0 || (int)a[1]!=0 || (int)a[2]!=0 || (int)a[3]!=0){ cout << (i+1)%32 << "x" << (i+1)/32 << " ( pos: " << ( (int)(unsigned char)a[0]*65536 + (int)(unsigned char)a[1]*255 + (int)(unsigned char)a[2] )*4096 << " size: "; fp_in.seekg(( (int)(unsigned char)a[0]*65536 + (int)(unsigned char)a[1]*256 + (int)(unsigned char)a[2] )*4096); fp_in.read(a,5); long size = (int)(unsigned char)a[0]*16777216 + (int)(unsigned char)a[1]*65536 + (int)(unsigned char)a[2]*256 + (int)(unsigned char)a[3]; cout << (int)(unsigned char)a[0] << ":" ; //problem cout cout << (int)(unsigned char)a[1] << ":" ; cout << (int)(unsigned char)a[2] << ":" ; cout << (int)(unsigned char)a[3] << " = "; cout << size << " ) \t:"; char *buff = new char[size-1]; long out_size = 0; bytef *out; fp_in.read(buff,size-1); int status = _decompress((bytef*)buff,size-1,out,&out_size); if(status>=0) cout << "decompressed size: " << out_size << " bytes."; else cout << "error: " << status; } cout << endl; } for(int = 0; i<1024; i++) //segmentation fault cout << "anything"; fp_in.close(); return 0; }
1) [solved]i not understand why can not put z_stream z; inside int _decompress function. if do, 3 methods inside have "segmentation fault" comment produce kind of error (during runtime.)
2) @ 5 lines "problem cout" comment, if attempt in 1 line weird error think zlib, , not g++.
3) @ end, there loop. loop produces segmentation fault well. if remove it, fine. tried remove contents of loop above it. segmentation fault gone well.
appreciate any of these problems!
in advance!
a struct defined @ global scope automatically zero-initialised; 1 defined in function body not. zero-initialising presumably set zalloc , zfree null rather undefined values have if not zero-initialised (note zlib docs state zalloc , zfree must set prior call inflateinit(...), , null acceptable value them.)
edit:
also, "out" doesn't seem initialised inside main().
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk For loop leads to segmentation fault
Ubuntu

Comments
Post a Comment