This is my first time here. Hello everyone
I had this posted over at dreamincode.net for about 4 days now. None over there can help me.
So i came here.
Intent:
This code is intended to be used latter in an encryption program.
Notices:
I have provided a BackTrace.
Also, when i remove the function call seed_gen() the next function
in line is identified as the problem. This is od,, i have looked at my
code in detail, i can find no problem (and neither can anyone at </D.I.C>).
Where i am seeding srand() with 3.141, i intedn to use time() in the final release.
Code / Errors / Debugg:
Main.cpp:
Cipher.h:Code:#include <cstdlib> #include <cstdio> #include <ctime> #include <iostream> #include <fstream> #include "cipher.h" using namespace std; cipher Ciph1; int main(){ Ciph1.seed_gen(); Ciph1.ciph_gen(); if(Ciph1.cipher_any() != true){ cout << "Cipher out of range.\n"; cin.get(); return 0; } Ciph1.ciph_pri(); cin.get(); }
VALGRIND and GDB:Code:#ifndef CIPHER_H #define CIPHER_H /** _--+++Written by Delta+++--_ **/ /** Editing and advice: dreaminocde.net members: polymath cerolobo AmitTheInfinity **/ #include <stdio.h> #include <cstdlib> #include <time.h> #include <iostream> using namespace std; class cipher{ public: // Public Variables int MyCipher[512][10]; int MySeed[512]; // Public Functions // ***Seed Generator*** void seed_gen(){ srand(time(NULL)); //Only seed random once in a function for (int a = 0;a < 511;a++){ MySeed[a] = (rand() %10); } } // ***Cipher Generator*** // ciph_gen(); void ciph_gen(){ // Stage 1: Set each first element of // each line to the seed value of that // line for (int a = 0;a < 512;a++){ MyCipher[a][1] = MySeed[a]; } // Stage 2 for (int b = 0;b < 511;b++){ for (int c = 0;c < 9;c++){ if (MyCipher[b][c--] == 9){ MyCipher[b][c--] = 0; } MyCipher[b][c] = (MyCipher[b][c--]) +1; } } } // ***Cipher Anylizer*** // ciph_any(); bool cipher_any(){ for (int a = 0;a < 512;a++){ for(int b = 0;b < 10;a++){ if (MyCipher[a][b] > 9 || MyCipher[a][b] < 0){ return false; // Integrity = VOID } } } } // ***Print Cipher*** // ciph_pri(); void ciph_pri(){ for (int a = 0;a < 512;a++){ for (int b = 1;b < 10;b++){ cout << MyCipher[a][b]; } cout << "\n"; } cin.get(); } }; #endif
Any help would be greatly appreciated.Code:[Delta@localhost ~]$ cd Crypt [Delta@localhost Crypt]$ g++ main.cpp -o build1 -g [Delta@localhost Crypt]$ ./build1 Segmentation fault [Delta@localhost Crypt]$ gdb build1 GNU gdb Fedora (6.8-11.fc9) Copyright (C) 2008 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu"... (gdb) start Breakpoint 1 at 0x8048751: file main.cpp, line 13. Starting program: /home/Delta/Crypt/build1 main () at main.cpp:13 13 Ciph1.seed_gen(); (gdb) bt #0 main () at main.cpp:13 (gdb) list 8 using namespace std; 9 10 cipher Ciph1; 11 12 int main(){ 13 Ciph1.seed_gen(); 14 Ciph1.ciph_gen(); 15 if(Ciph1.cipher_any() != true){ 16 cout << "Cipher out of range.\n"; 17 cin.get(); (gdb) print Ciph1 $1 = {MyCipher = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0} <repeats 512 times>}, MySeed = {0 <repeats 512 times>}} (gdb) quit The program is running. Exit anyway? (y or n) y [Delta@localhost Crypt]$ valgrind ./build1 ==3243== Memcheck, a memory error detector. ==3243== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al. ==3243== Using LibVEX rev 1804, a library for dynamic binary translation. ==3243== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP. ==3243== Using valgrind-3.3.0, a dynamic binary instrumentation framework. ==3243== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al. ==3243== For more details, rerun with: -v ==3243== ==3243== Invalid free() / delete / delete[] ==3243== at 0x40052EA: operator delete(void*, std::nothrow_t const&) (vg_replace_malloc.c:354) ==3243== by 0x42AE118: std::__verify_grouping(char const*, unsigned, std::string const&) (locale_facets.cc:108) ==3243== by 0x42AF04C: std::locale::_Impl::_Impl(char const*, unsigned) (localename.cc:218) ==3243== by 0x42AF0CC: std::locale::_Impl::_Impl(char const*, unsigned) (localename.cc:206) ==3243== by 0x42B01F7: std::locale::locale() (basic_string.h:2189) ==3243== by 0x42AB1CC: std::locale::_Impl::_Impl(std::locale::_Impl const&, unsigned) (locale.cc:249) ==3243== by 0x8048704: __static_initialization_and_destruction_0(int, int) (iostream:77) ==3243== by 0x804873D: _GLOBAL__I_Ciph1 (main.cpp:22) ==3243== by 0x8048ADC: (within /home/Delta/Crypt/build1) ==3243== by 0x8048557: (within /home/Delta/Crypt/build1) ==3243== by 0x8048A78: __libc_csu_init (in /home/Delta/Crypt/build1) ==3243== by 0x5AD570: (below main) (libc-start.c:179) ==3243== Address 0x4347188 is not stack'd, malloc'd or (recently) free'd ==3243== ==3243== Process terminating with default action of signal 11 (SIGSEGV) ==3243== Bad permissions for mapped region at address 0x8048FFC ==3243== at 0x8048899: cipher::ciph_gen() (cipher.h:50) ==3243== by 0x8048768: main (main.cpp:14) ==3243== ==3243== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 15 from 1) ==3243== malloc/free: in use at exit: 0 bytes in 0 blocks. ==3243== malloc/free: 0 allocs, 1 frees, 0 bytes allocated. ==3243== For counts of detected errors, rerun with: -v ==3243== All heap blocks were freed -- no leaks are possible. Segmentation fault [Delta@localhost Crypt]$
Thanks
Live long and code strong.



LinkBack URL
About LinkBacks




I used to be an adventurer like you... then I took an arrow to the knee.