Recursion Segmentation Fault - c++

#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <vector>
using namespace std;
enum ElementType { SIMPLEFILE, DIRECTORY, SYMBOLICLINK };
class Element{
public:
Element(){};
Element(char *name_, char *full_path_name_, ElementType element_type_, long element_size_)
: name(NULL), full_path_name(NULL), element_size(0)
{
memcpy (name,name_,strlen(name_)+1);
memcpy (full_path_name,full_path_name_,strlen(full_path_name_)+1);
element_type=element_type_;
element_size=element_size_;
};
char *name;
char *full_path_name;
ElementType element_type;
long element_size;
};
int inspect(const char *input_path, std::vector<Element>& result_element_array, long *dir_size,const char *full_path ) {
std::vector<Element> result_element_array_temp;
DIR *d;
struct dirent *dir;
struct stat buf;
char *mynamebuf=(char *)malloc(0);
int c=0;
size_t base_len = strlen(full_path);
long dir_size_temp=0;
char *full_path_temp=(char *)malloc(0);
char *full_path_dummy=(char *)malloc(0);
result_element_array_temp.reserve(1000);
d = opendir( full_path);
if( d == NULL ) {
return 1;
}
while( ( dir = readdir( d ) )) {
if( strcmp( dir->d_name, "." ) == 0 || strcmp( dir->d_name, ".." ) == 0 ) {
continue;
}
memcpy (mynamebuf,full_path,strlen(full_path)+1);
strcat(mynamebuf,(full_path[base_len - 1] == '/' ? "" : "/"));
strcat(mynamebuf,dir->d_name);
if (stat(mynamebuf, &buf) != 0) {
perror(mynamebuf);
continue;
}
if( S_ISDIR(buf.st_mode) ) {//if dir
chdir( dir->d_name );
memcpy (full_path_temp,full_path,strlen(full_path)+1);
strcat(full_path_temp,"/");
strcat(full_path_temp,dir->d_name);
(dir_size_temp)=0;
inspect( ".", result_element_array_temp, &dir_size_temp, full_path_temp );
chdir( ".." );
memcpy (full_path_dummy,full_path_temp,strlen(full_path_temp)+1);
strcat(full_path_dummy,"/");
strcat(full_path_dummy,dir->d_name);
Element element;
element.name=dir->d_name;
element.full_path_name=full_path_dummy;
element.element_type=DIRECTORY;
element.element_size=dir_size_temp;
result_element_array.push_back(element);
result_element_array.insert( result_element_array.end(), result_element_array_temp.begin(), result_element_array_temp.end() );
(*dir_size)+=(dir_size_temp);
} else if( S_ISREG(buf.st_mode)) {//if file
memcpy (full_path_dummy,full_path,strlen(full_path)+1);
strcat(full_path_dummy,"/");
strcat(full_path_dummy,dir->d_name);
Element element;
element.name=dir->d_name;
element.full_path_name=full_path_dummy;
element.element_type=SIMPLEFILE;
element.element_size=buf.st_size;
result_element_array.push_back(element);
(*dir_size)+=buf.st_size;
} else if( S_ISLNK(buf.st_mode) ) {//if link
memcpy (full_path_dummy,full_path,strlen(full_path)+1);
strcat(full_path_dummy,"/");
strcat(full_path_dummy,dir->d_name);
Element element;
element.name=dir->d_name;
element.full_path_name=full_path_dummy;
element.element_type=SIMPLEFILE;
element.element_size=buf.st_size;
result_element_array.push_back(element);
} else {
continue;
}
}
closedir(d);
return 0;
}
int main(){
std::vector<Element> result_element_array;
result_element_array.reserve(3000);
long dir_size;
const char *full_path="/";
inspect("/", result_element_array, &dir_size,full_path );
std::vector <Element>::iterator It;
for(It = result_element_array.begin(); It != result_element_array.end(); ++It){
printf("%s\n",(*It).full_path_name);
}
return 0;
}
The code is above. I aim to write directory explorer recursively.
I GDBed the code and it reaches return 0 when inspect method called recursively, but return 0 can not be executed. Might it be about stackoverflow caused by some other lines? Any idea would be appreciated.

On your Element constructor, you're memcpy-ing char arrays without allocating the destination char*. You should consider using std::string:
class Element{
public:
Element(){};
Element(char *name_,
char *full_path_name_,
ElementType element_type_,
long element_size_)
:name(name),
full_path_name(full_path_name_),
element_size(0)
{
element_type=element_type_;
element_size=element_size_;
};
std::string name;
std::string full_path_name;
ElementType element_type;
long element_size;
};
You are also performing a malloc of 0 bytes, and then use this pointer as if it was valid:
char *mynamebuf=(char *)malloc(0);
You should also consider using std::string, or at least fix it to allocate a valid amount of bytes.

Since you're using gdb I'm going to assume valgrind is available. Your use of memcpy is unsafe and valgrind should assist with finding the various memory corruption issues your program seems to have. Like Jeremy Friesner and fontanini both suggested, use std::string instead of naked char *.

Related

Input from text file into char *array[9]

I have a file with 9 words and i have to store each word into the char array of 9 pointers but i keep getting an error message. I cannot use vectors!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char *words[9];
ifstream inStream;
inStream.open("sentence.txt");
if (inStream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
for ( int i = 0; i < 10; i++)
{
inStream >> words[i];
}
inStream.close();
return 0;
}
The declaration
char *words[9];
declares a raw array of pointers. This array is not initialized so the pointers have indeterminate values. Using any of them would be Undefined Behavior.
Instead you want
vector<string> words;
where vector is std::vector from the <vector> header, and string is std::string from the <string> header.
Use the push_back member function to add strings to the end of the vector.
Also you need to move the close call out of the loop. Otherwise it will close the file in the first iteration.
This approach gives the code (off the cuff, disclaimer...)
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> words;
ifstream inStream;
inStream.open("sentence.txt");
for ( int i = 0; i < 10; i++)
{
string word;
if( inStream >> word )
words.push_back( word );
}
inStream.close();
}
If you can't use std::string and std::vector then you need to initialize the array of pointers, and make sure that you don't read more into the buffers than there's room for.
The main problem here is that >> is unsafe for reading into a raw array given by a pointer. It doesn't know how large that array is. It can easily lead to a buffer overrun, with dire consequences.
And so this gets a bit complicated, but it can look like this:
#include <ctype.h> // isspace
#include <fstream>
#include <iostream>
#include <locale.h> // setlocale, LC_ALL
#include <stdlib.h> // EXIT_FAILURE
using namespace std;
void fail( char const* const message )
{
cerr << "! " << message << "\n";
exit( EXIT_FAILURE );
}
void readWordFrom( istream& stream, char* const p_buffer, int const buffer_size )
{
int charCode;
// Skip whitespace:
while( (charCode = stream.get()) != EOF and isspace( charCode ) ) {}
int n_read = 0;
char* p = p_buffer;
while( n_read < buffer_size - 1 and charCode != EOF and not isspace( charCode ) )
{
*p = charCode; ++p;
++n_read;
charCode = stream.get();
}
*p = '\0'; // Terminating null-byte.
if( charCode != EOF )
{
stream.putback( charCode );
if( not isspace( charCode ) )
{
assert( n_read == buffer_size - 1 ); // We exceeded buffer size.
stream.setstate( ios::failbit );
}
}
}
int main()
{
static int const n_words = 9;
static int const max_word_length = 80;
static int const buffer_size = max_word_length + 1; // For end byte.
char *words[n_words];
for( auto& p_word : words ) { p_word = new char[buffer_size]; }
ifstream inStream{ "sentence.txt" };
if( inStream.fail() ) { fail( "Input file opening failed." ); }
setlocale( LC_ALL, "" ); // Pedantically necessary for `isspace`.
for( auto const p_word : words )
{
readWordFrom( inStream, p_word, buffer_size );
if( inStream.fail() ) { fail( "Reading a word failed." ); }
}
for( auto const p_word : words ) { cout << p_word << "\n"; }
for( auto const p_word : words ) { delete[] p_word; }
}
You never allocate any memory for your char* pointers kept in the array.
The idiomatic way to write a c++ code would be:
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::vector<std::string> words(9);
std::ifstream inStream;
inStream.open("sentence.txt");
for ( int i = 0; inStream && i < 9; i++) {
inStream >> words[i];
}
}
The inStream.close() isn't necessary, and even wrong inside the loop. The std::istream will be closed automatically as soon the variable goes out of scope.
There are a few problems with your code.
char *words[9];
This allocates space for 9 pointers, not nine strings. Since you don't know how big the strings are you have two choices. You can either "guess" how much you'll need and limit the inputs accordingly, or you can use dynamic memory allocation (malloc or new) to create the space you need to store the strings. Dynamic memory would be my choice.
for ( int i = 0; i < 10; i++)
This loop will execute on words[0] through words[9]. However, there is no words[9] (that would be the tenth word) so you'll overwrite memory that you have not allocated
inStream >> words[i];
This will send your input stream to memory that you don't "own". You need to allocate space for the words to live before capturing them from the input stream. To do this correctly, you'll need to know how much space each word will need so you can allocate it.
you could try something like this:
int main()
{
char *words[9];
char tempInput[256]; // space to capture the input, up to a maximum size of 256 chars
ifstream inStream;
inStream.open("sentence.txt");
if (inStream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
for ( int i = 0; i < 9; i++)
{
//Clear the input buffer
memset(tempInput, 0, 256);
//Capture the next word
inStream >> tempInput;
//allocate space to save the word
words[i] = new char(strlen(tempInput));
//Copy the word to its final location
strcpy(words[i], tempInput)
}
inStream.close();
return 0;
}

using strstr() function is breaking

I am using strstr() function but I am getting the crash.
This part of code is crashing with error "Access violation reading location 0x0000006c."
strstr(p_czCharactersToDelete, (const char*)p_czInputString[index]))
Here is the complete code...
#include "stdafx.h"
#include <iostream>
#include <string>
void delchar(char* p_czInputString, const char* p_czCharactersToDelete)
{
for (size_t index = 0; index < strlen(p_czInputString); ++index)
{
if(NULL != strstr(p_czCharactersToDelete, (const char*)p_czInputString[index]))
{
printf_s("%c",p_czInputString[index]);
}
}
}
int main(int argc, char* argv[])
{
char c[32];
strncpy_s(c, "life of pie", 32);
delchar(c, "def");
// will output 'li o pi'
std::cout << c << std::endl;
}
The prototype of strstr() is as follows,
char * strstr ( char * str1, const char * str2 );
The function is used to locate substring from a main string. It returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
In your case you are passing the wrong parameters to the strstr(). You are calling,
strstr(p_czCharactersToDelete, (const char*)p_czInputString[index]));, which is wrong. Because the pointer p_czCharactersToDelete points to the sub string constant and p_czInputString points to the main string. Call strstr() as strstr(p_czInputString, p_czCharactersToDelete); and make corresponding changes in the function delchar().
you are using the wrong strstr.
probably you need strchr or strpbrk.
#include <cstring>
#include <algorithm>
class Include {
public:
Include(const char *list){ m_list = list; }
bool operator()(char ch) const
{
return ( strchr(m_list, ch) != NULL );
}
private:
const char *m_list;
};
void delchar(char* p_czInputString, const char* p_czCharactersToDelete){
Include inc(p_czCharactersToDelete);
char *last = std::remove_if(p_czInputString, p_czInputString + strlen(p_czInputString), inc);
*last = '\0';
}

Reading lines from a binary file C++

I am trying to read a binary file and store it into a database, however I get a segmentation fault when I try to store a string type into the database. To be exact, the error occurs inside the push function:
new_node->name = name;
I can't seem to find a good solution on the web, and I'm aimlessly trying different things... any help would be appreciated.
//
// loadbin.cpp
//
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#include "studentsDB.h"
int main( int argc, char* argv[] ) {
string name;
string id;
int numCourses;
int crn;
vector<int> crns;
studentsDB sDB;
studentsDB::node *students = 0;
int in = 1;
if( argc > 1 ) {
ifstream infile(argv[in], ios::binary );
while( !infile.eof() ) {
infile.read( ( char* )(name.c_str()), sizeof( string ) );
infile.read( ( char* )(id.c_str()), sizeof( string ) );
infile.read( ( char* ) &numCourses, sizeof( int ) );
do{
crns.push_back( crn );
}
while( infile.read( ( char* ) &crn, sizeof( int ) ) );
sDB.push( &students, (string)name, (string)id, numCourses, crns );
}
//sDB.printList( students );
}
else
cout << "Not enough argument" << endl;
}
void studentsDB::push( struct node** head_ref, string name, string id,
int numCourses, vector<int>crns ) {
struct node* new_node = ( struct node* ) malloc(sizeof(struct node));
new_node->name = name;
//new_node->id = id;
new_node->numCourses = numCourses;
//new_node->crns = crns;
new_node->next = (*head_ref);
(*head_ref) = new_node;
size++;
}
This code is bad:
infile.read( ( char* )(name.c_str()), sizeof( string ) );
You can't write to the buffer returned by c_str(), it is not guaranteed to be long enough to hold your result. sizeof(string) has nothing to do with the size the string can hold, by the way. You will need to allocate your own char[] buffer to hold the results of infile.read, then convert to a string afterwards.

Function to parse string with tokens

I know how to program in C# and VB but not have idea about how to use C++ and have to program a little exe to a barcode scanner that use C++ :(
In this moment I try to parse a scanned barcode that have multiple data sepparated with a "/", I find that exist a strtok function, tested it "manually" and worked ok but I not implemented yet a working function to call it correctly, what I have now:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int elemStr(char *str, char sep)
{
int cantElem;
unsigned ich;
cantElem = 0;
if (strlen(str) > 0) //at least 1 elem
cantElem++;
for (ich = 0; ich < strlen(str); ich++)
{
if (str[ich] == sep)
cantElem++;
}
return cantElem;
}
char* getElemStr(char *str, char sep[], int elem)
{
char *tempStr = NULL;
char *tok;
int currElem = 1;
// 1st data
strcpy( tempStr, str);
tok = strtok( tempStr, sep);
while( currElem != elem )
{
// Get next tokens:
tok = strtok( NULL, sep );
currElem++;
}
return tok;
}
void main( void )
{
char barcode[] = "710015733801Z/1/35";
char sep[] = "/";
char sep1 = sep[0];
char barcd[20];
char piezaChar[4];
int pieza;
char mtsChar[4];
int cantElem;
cantElem = elemStr(barcode, sep1 );
if (cantElem >= 1)
{
strcpy(barcd, getElemStr(barcode,sep,1) ); //pasa a str resultado;
printf("Cod: %s\n", barcd ); //STACK OVERFLOW HERE!
}
}
if I use strtok witout a function "getElemStr" it work ok but I try to use it on other places too.
Can I use strtok like this? You have a working example?
pd: I not have idea about pointers (sorry), good doc to learn about that?
Since you specifically asked about C++, I'm going to ignore your very c-style code and show you how to do this in C++:
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string barcode = "710015733801Z/1/35";
std::string sep = "/";
std::vector<std::string> v;
boost::split(v, barcode, boost::is_any_of(sep));
for(size_t i=0; i<v.size(); ++i)
std::cout << v[i] << '\n';
}
strtok destroys your original string. So i don't think it can be used with a char* that points to a static string. Static strings get copied to a read only portion of the executable.
Here is a C++ solution that doesn't use boost.
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string barcode = "710015733801Z/1/35";
std::stringstream ss(barcode);
std::string elem;
while(std::getline(ss, elem, '/'))
{
//do something else meaningful with elem
std::cout << elem << std::endl;
}
return 0;
}
Output:
710015733801Z
1
35

Question about Null vs zero

I have this function:
void ToUpper(char * S)
{
while (*S!=0)
{
*S=(*S >= 'a' && *S <= 'z')?(*S-'a'+'A'):*S;
S++;
}
}
What does it mean for *S != 0, should it be null instead?
That is checking for the end of the string which is a character which has the value of zero. It is not connected to NULL pointers in any way.
I would write it *S != '\0' as I feel that is more idiomatic, but that is really just personal style preference. You are checking for the null character (ASCII NUL).
You might also consider checking S != 0 before any of that code as the pointer itself may be null, and you don't want to dereference a null pointer.
NULL is defined differently in C and C++
in C
#define NULL 0
in C++
#define NULL (void*) 0
I like algorithms better than loops:
#include <algorithm>
#include <cstring>
#include <cctype>
void ToUpper(char* p)
{
std::transform(p, p + strlen(p), p, toupper);
}
This solution also works for character encodings where a to z aren't sequential.
Just for fun, here is an experiment that only does one iteration with algorithms:
#include <algorithm>
#include <cassert>
#include <cstring>
#include <cctype>
#include <iostream>
#include <iterator>
struct cstring_iterator : std::iterator<std::random_access_iterator_tag, char>
{
char* p;
cstring_iterator(char* p = 0) : p(p) {}
char& operator*()
{
return *p;
}
cstring_iterator& operator++()
{
++p;
return *this;
}
bool operator!=(cstring_iterator that) const
{
assert(p);
assert(!that.p);
return *p != '\0';
}
};
void ToUpper(char* p)
{
std::transform(cstring_iterator(p), cstring_iterator(),
cstring_iterator(p), toupper);
}
int main()
{
char test[] = "aloha";
ToUpper(test);
std::cout << test << std::endl;
}
NULL is a pointer while *S is the value stored at the pointer. Thankes to Dennis Ritchie, the digit 0 is acceotable as both.