the strcpy function keeps crashing - c++

#include <iostream>
using namespace std;
defining class
class fancyString {
private:
char *content;
bool flag_bold;
bool flag_italics;
public:
fancyString(){
content="";
flag_bold= false;
flag_italics=false;
}
in both functions I'm asked to use the old fashioned calloc
fancyString(char* cntnt){
content=(char *) calloc(strlen(cntnt)+1, sizeof(char*));
Usually the strcpy is the main reason of the crash
strcpy(cntnt,content);
}
fancyString(fancyString & f1){
content=(char *) calloc(strlen(f1.content)+1, sizeof(char*));
Usually the strcpy is the main reason of the crash
strcpy(f1.content,content);
flag_bold=f1.flag_bold;
flag_italics=f1.flag_italics;
}
friend ostream& operator<<(ostream& os, const fancyString& FS){
os<<"string is "<<FS.content<<endl<<"bold status is "<<FS.flag_bold<<endl<<"italics status is "<<FS.flag_italics<<endl;
return os;
}
~fancyString(){
cout << "Destroying the string\n";
if ( content != NULL )
free (content);
}
};
main function
int main(int argc, const char * argv[]) {
fancyString fs1 ("First Example");
fancyString fs2(fs1);
cout<<fs2;
return 0;
}

You have the arguments to the strcpy calls the wrong way round! See the definition on cppreference:
char * strcpy ( char * destination, const char * source );
So, in your your first constructor, the call:
strcpy(cntnt,content);
is attempting to copy the newly-allocated buffer into the passed argument, which is actually a (constant) string literal:
int main(int argc, const char * argv[]) {
fancyString fs1 ("First Example");
//...
NOTES based on suggestions made in the comments:
(1) Note that, in your calloc call - which is allocating an 'array' of char, the elements' size is sizeof(char) not sizeof(char*) (which would be appropriate for an 'array' of pointers). Use:
content = (char *) calloc(strlen(cntnt)+1, sizeof(char));
and similarly in the copy constructor.
(2) Assuming that your first constructor is never intended to modify the string given as its argument, you should really specify it as const:
fancyString(const char* cntnt){
//...
(This would have flagged the error of having the strcpy arguments wrong!)
Please feel free to ask for further clarification and/or explanation.

Related

Implementing a String class with implicit conversion to char* (C++)

It might not be advisable according to what I have read at a couple of places (and that's probably the reason std::string doesn't do it already), but in a controlled environment and with careful usage, I think it might be ok to write a string class which can be implicitly converted to a proper writable char buffer when needed by third party library methods (which take only char* as an argument), and still behave like a modern string having methods like Find(), Split(), SubString() etc. While I can try to implement the usual other string manipulation methods later, I first wanted to ask about the efficient and safe way to do this main task. Currently, we have to allocate a char array of roughly the maximum size of the char* output that is expected from the third party method, pass it there, then convert the return char* to a std::string to be able to use the convenient methods it allows, then again pass its (const char*) result to another method using string.c_str(). This is both lengthy and makes the code look a little messy.
Here is my very initial implementation so far:
MyString.h
#pragma once
#include<string>
using namespace std;
class MyString
{
private:
bool mBufferInitialized;
size_t mAllocSize;
string mString;
char *mBuffer;
public:
MyString(size_t size);
MyString(const char* cstr);
MyString();
~MyString();
operator char*() { return GetBuffer(); }
operator const char*() { return GetAsConstChar(); }
const char* GetAsConstChar() { InvalidateBuffer(); return mString.c_str(); }
private:
char* GetBuffer();
void InvalidateBuffer();
};
MyString.cpp
#include "MyString.h"
MyString::MyString(size_t size)
:mAllocSize(size)
,mBufferInitialized(false)
,mBuffer(nullptr)
{
mString.reserve(size);
}
MyString::MyString(const char * cstr)
:MyString()
{
mString.assign(cstr);
}
MyString::MyString()
:MyString((size_t)1024)
{
}
MyString::~MyString()
{
if (mBufferInitialized)
delete[] mBuffer;
}
char * MyString::GetBuffer()
{
if (!mBufferInitialized)
{
mBuffer = new char[mAllocSize]{ '\0' };
mBufferInitialized = true;
}
if (mString.length() > 0)
memcpy(mBuffer, mString.c_str(), mString.length());
return mBuffer;
}
void MyString::InvalidateBuffer()
{
if (mBufferInitialized && mBuffer && strlen(mBuffer) > 0)
{
mString.assign(mBuffer);
mBuffer[0] = '\0';
}
}
Sample usage (main.cpp)
#include "MyString.h"
#include <iostream>
void testSetChars(char * name)
{
if (!name)
return;
//This length is not known to us, but the maximum
//return length is known for each function.
char str[] = "random random name";
strcpy_s(name, strlen(str) + 1, str);
}
int main(int, char*)
{
MyString cs("test initializer");
cout << cs.GetAsConstChar() << '\n';
testSetChars(cs);
cout << cs.GetAsConstChar() << '\n';
getchar();
return 0;
}
Now, I plan to call the InvalidateBuffer() in almost all the methods before doing anything else. Now some of my questions are :
Is there a better way to do it in terms of memory/performance and/or safety, especially in C++ 11 (apart from the usual move constructor/assignment operators which I plan to add to it soon)?
I had initially implemented the 'buffer' using a std::vector of chars, which was easier to implement and more C++ like, but was concerned about performance. So the GetBuffer() method would just return the beginning pointer of the resized vector of . Do you think there are any major pros/cons of using a vector instead of char* here?
I plan to add wide char support to it later. Do you think a union of two structs : {char,string} and {wchar_t, wstring} would be the way to go for that purpose (it will be only one of these two at a time)?
Is it too much overkill rather than just doing the usual way of passing char array pointer, converting to a std::string and doing our work with it. The third party function calls expecting char* arguments are used heavily in the code and I plan to completely replace both char* and std::string with this new string if it works.
Thank you for your patience and help!
If I understood you correctly, you want this to work:
mystring foo;
c_function(foo);
// use the filled foo
with a c_function like ...
void c_function(char * dest) {
strcpy(dest, "FOOOOO");
}
Instead, I propose this (ideone example):
template<std::size_t max>
struct string_filler {
char data[max+1];
std::string & destination;
string_filler(std::string & d) : destination(d) {
data[0] = '\0'; // paranoia
}
~string_filler() {
destination = data;
}
operator char *() {
return data;
}
};
and using it like:
std::string foo;
c_function(string_filler<80>{foo});
This way you provide a "normal" buffer to the C function with a maximum that you specify (which you should know either way ... otherwise calling the function would be unsafe). On destruction of the temporary (which, according to the standard, must happen after that expression with the function call) the string is copied (using std::string assignment operator) into a buffer managed by the std::string.
Addressing your questions:
Do you think there are any major pros/cons of using a vector instead of char* here?
Yes: Using a vector frees your from manual memory management. This is a huge pro.
I plan to add wide char support to it later. Do you think a union of two structs : {char,string} and {wchar_t, wstring} would be the way to go for that purpose (it will be only one of these two at a time)?
A union is a bad idea. How do you know which member is currently active? You need a flag outside of the union. Do you really want every string to carry that around? Instead look what the standard library is doing: It's using templates to provide this abstraction.
Is it too much overkill [..]
Writing a string class? Yes, way too much.
What you want to do already exists. For example with this plain old C function:
/**
* Write n characters into buffer.
* n cann't be more than size
* Return number of written characters
*/
ssize_t fillString(char * buffer, ssize_t size);
Since C++11:
std::string str;
// Resize string to be sure to have memory
str.resize(80);
auto newSize = fillSrting(&str[0], str.size());
str.resize(newSize);
or without first resizing:
std::string str;
if (!str.empty()) // To avoid UB
{
auto newSize = fillSrting(&str[0], str.size());
str.resize(newSize);
}
But before C++11, std::string isn't guaranteed to be stored in a single chunk of contiguous memory. So you have to pass through a std::vector<char> before;
std::vector<char> v;
// Resize string to be sure to have memor
v.resize(80);
ssize_t newSize = fillSrting(&v[0], v.size());
std::string str(v.begin(), v.begin() + newSize);
You can use it easily with something like Daniel's proposition

Passing char ** as reference and returning const char * as reference

I have a class which parses the Command line arguments and then returns the parsed value to the client class. For parsing, I need to pass argv to parse function. I would like to pass by reference but from what I know , we never use the '&' symbol when passing arrays. Arrays are not objects that can be passed by reference. Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
class cmdline
{
const char * ifile;
public:
cmdline():ifile(NULL){}
const char * const getFile() const
{
return (ifile);
}
void parse(int argc,const char** argv)
{
//parse and assign value to ifile
// ifile = optarg;
// optarg is value got from long_getopt
}
};
int main(int argc, char ** argv)
{
cmdline CmdLineObj;
CmdLineObj.parse(argc, const_cast<const char**>(argv));
const char * const ifile = CmdLineObj.getFile();
ifstream myfile (ifile);
return 0;
}
1) Is the way argv is treated, correct?
2) Better way to handle, ifile?
3) I want to return ifile as reference, what change should I do, if needed?
My code works the way it is supposed to work, but the reason I came to SO is to "not-just-make-it-work" but do it properly.
Thanks for your help.
EDIT:: After Mehrdad's comment, I edited like this:
class CmdLine
{
const char * ifile;
public:
const char * & getFile() const
{
return (ifile);
}
But I get the error - invalid initialization of reference of type ‘const char*&’ from expression of type ‘const char’
Arrays are not objects that can be passed by reference.
What makes you think that?
1) Is the way argv is treated, correct?
CmdLineObj.parse(argc, const_cast< const char** >(argv));
Why are you const casting that? Instead of casting, you could change your definition of main to const char** argv.
2) Better way to handle, ifile?
Well, there is always std::string, but since all you seem to do is then pass the value to an std::ifstream I don't see a point in using it.
3) I want to return ifile as reference, what change should I do, if needed?
What would be the point of returning a pointer as a reference? Are you expecting callers of getFile to actually change the member that points to such string? You shouldn't be doing that since getFile is a const member function. If you are thinking performance, then returning a reference to a pointer in this case will actually be worse than returning the pointer by value. The string contents are not getting copied when returned from getFile, like they would if ifile was instead an std::string (in which case returning a const reference would make sense).

String Class C++

Assume I want to write my own string class. The string has a property char * s which is a pointer that points to a character.
Now in the constructor, String::String(), what would you pass in to assume another char * to that? You can't really do something like the code below since both pointers will point to the same thing (and we don't want that):
String::String(const char *str) {
s = str;
}
Any help is appreciated!
You need to deep copy the string, i.e. create a character buffer long enough to incorporate the contents of str, then copy the contents into it. The simplest way to achieve that would be using strdup strcpy, since the former is nonstandard:
s = new char[strlen (str) + 1];
if (s == NULL) throw some_exception;
strcpy (s, str);
Please do not write your own string class. There are hell of a lot of details you have to know not to introduce mistakes (for example, overloaded operators, boolean idioms etc), and a lot more details to make that string class efficient (for example, implement copy-on-write) etc. But just for educational purposes, you have to make a copy of the passed string. Here is an example:
#include <cstdint>
#include <cstring>
#include <cstdio>
class String
{
char *p_;
public:
explicit String (const char *str)
{
auto length = std::strlen (str) + 1;
p_ = new char [length];
std::memcpy (p_, str, length);
}
~String ()
{
delete [] p_;
p_ = nullptr;
}
inline const char *c_str () const
{
return p_;
}
};
int
main ()
{
String s ("Hello, world!");
std::printf ("%s\n", s.c_str ());
}
You should copy contents of null-terminated string that is passed as parameter.
One thing is that you might remove terminating null, because if you have your own class you can manage string boundaries manually by keeping current length.

How do I return a char array from a function?

I've tried the following:
char[10] testfunc()
{
char[10] str;
return str;
}
Best as an out parameter:
void testfunc(char* outStr){
char str[10];
for(int i=0; i < 10; ++i){
outStr[i] = str[i];
}
}
Called with
int main(){
char myStr[10];
testfunc(myStr);
// myStr is now filled
}
You have to realize that char[10] is similar to a char* (see comment by #DarkDust). You are in fact returning a pointer. Now the pointer points to a variable (str) which is destroyed as soon as you exit the function, so the pointer points to... nothing!
Usually in C, you explicitly allocate memory in this case, which won't be destroyed when the function ends:
char* testfunc()
{
char* str = malloc(10 * sizeof(char));
return str;
}
Be aware though! The memory pointed at by str is now never destroyed. If you don't take care of this, you get something that is known as a 'memory leak'. Be sure to free() the memory after you are done with it:
foo = testfunc();
// Do something with your foo
free(foo);
A char array is returned by char*, but the function you wrote does not work because you are returning an automatic variable that disappears when the function exits.
Use something like this:
char *testfunc() {
char* arr = malloc(100);
strcpy(arr,"xxxx");
return arr;
}
This is of course if you are returning an array in the C sense, not an std:: or boost:: or something else.
As noted in the comment section: remember to free the memory from the caller.
As you're using C++ you could use std::string.
With Boost:
boost::array<char, 10> testfunc()
{
boost::array<char, 10> str;
return str;
}
A normal char[10] (or any other array) can't be returned from a function.
When you create local variables inside a function that are created on the stack, they most likely get overwritten in memory when exiting the function.
So code like this in most C++ implementations will not work:
char[] populateChar()
{
char* ch = "wonet return me";
return ch;
}
A fix is to create the variable that want to be populated outside the function or where you want to use it, and then pass it as a parameter and manipulate the function, example:
void populateChar(char* ch){
strcpy(ch, "fill me, Will. This will stay", size); // This will work as long as it won't overflow it.
}
int main(){
char ch[100]; // Reserve memory on the stack outside the function
populateChar(ch); // Populate the array
}
A C++11 solution using std::move(ch) to cast lvalues to rvalues:
void populateChar(char* && fillme){
fillme = new char[20];
strcpy(fillme, "this worked for me");
}
int main(){
char* ch;
populateChar(std::move(ch));
return 0;
}
Or this option in C++11:
char* populateChar(){
char* ch = "test char";
// Will change from lvalue to r value
return std::move(ch);
}
int main(){
char* ch = populateChar();
return 0;
}
With c++17 you can use next code:
auto testfunc() {
union {
char str[14];
} res = { .str = "Hello, World!" };
return res;
}
and then use you string as
const auto str = testfunc();
std::cout << str.str;

What is the right way of implementing C++ setters in char array wrapper class?

I'm working on the class that simply contains a character array and it's size (length in bytes). At the moment, I want to overload '+' operand for this class (to implement concatenation). Constructors work fine. Objects are created and I can see their fields and values in the debugger. I'm stuck at the point where '+' is used (main(line 13)). Code compiles well, without even warnings, but as I run it, my program fails with "invalid pointer message". And I found where exactly that invalid pointer is. It is in '+' implementation (BufferArray.cpp, line 39). When i call SetBuffer, char array is assigned properly (I saw it's value 'qwasd' in the operator implementation scope), but right at the next line it's vanished when I call SetSize. I have no idea why.
What is wrong with my setters and how can I implement '+' operand in this case?
Thanks in advance.
Here's the code I work with:
BufferArray.h:
#include <string.h>
#include <stdio.h>
#ifndef BUFFERARRAY_H
#define BUFFERARRAY_H
class BufferArray {
public:
BufferArray(char* reservedPlace);
BufferArray();
void SetSize(int sz);
int GetSize();
void SetBuffer(char* buf);
char* GetBuffer();
BufferArray operator+ (BufferArray bArr) const;
virtual ~BufferArray();
private:
int size;
char *buffer;
};
#endif /* BUFFERARRAY_H */
Implementation is in the next file BufferArray.cpp:
#include "BufferArray.h"
// Constructors.
BufferArray::BufferArray(){
size = 0;
strcpy(buffer, "");
}
BufferArray::BufferArray(char* reservedPlace) {
size = strlen(reservedPlace);
buffer = reservedPlace;
}
// Getters and setters.
void BufferArray::SetSize(int sz)
{
size = sz;
}
int BufferArray::GetSize()
{
return size;
}
void BufferArray::SetBuffer(char* buf)
{
buffer = buf;
}
char* BufferArray::GetBuffer()
{
return buffer;
}
// Operator +.
BufferArray BufferArray::operator+ (BufferArray bArr) const
{
char tempCharArray[strlen(buffer) + strlen(bArr.GetBuffer())];
strcpy(tempCharArray, buffer);
strcat(tempCharArray, bArr.GetBuffer());
BufferArray tempBA;
tempBA.SetBuffer(tempCharArray);
tempBA.SetSize(strlen(bArr.GetBuffer()) + strlen(buffer)); // Vanishes buffer field.
printf("%d",tempBA.GetSize());
return tempBA;
}
// Destructor.
BufferArray::~BufferArray() {
// Destroy the pointer.
delete [] buffer;
}
And the main function:
#include <cstdlib>
#include <iostream>
#include "BufferArray.h"
using namespace std;
int main(int argc, char** argv) {
BufferArray ba1;
char tmp1[3] = "qw";
char tmp2[4] = "asd";
ba1.SetSize(strlen(tmp1));
ba1.SetBuffer(tmp1);
BufferArray ba2(tmp2);
BufferArray ba3 = ba1 + ba2; // Runtime error is here.
cout << ba3.GetBuffer() << endl;
return 0;
}
in BufferArray::operator+, tempCharArray is a temporary buffer that will be destroyed when the function completes. There a basically two ways to handle this:
1/ allocate the temporary buffer with new[] in operator+, that way you'll make sure the buffer survives the call to operator+ but you'll either have a memory leak or require the caller to invoke delete[] later on, which is rather clumsy and error-prone
2/ or better yet, modify setBuffer so it does an internal copy of the buffer and add a call to delete[] in your own destructor :
BufferArray::~BufferArray() {
delete[] buffer;
}
void BufferArray::setBuffer(char *otherBuffer) {
buffer = new char[strlen(otherBuffer) + 1];
strcpy(buffer, otherBuffer);
}
Note that you'll have to modify the constructor so it also copies the input buffer (otherwise you'll have an illegal call to delete[] when the object is destroyed), and then you may want to overload the copy-constructor and assignment operator to prevent shallow copy which would result in double-deleting the buffer.
In actual production code, you'd want to use a managed pointer of some sort to avoid doing the delete yourself (e.g. std::vector or boost::shared_array), but for homework the above solution should be fine.
On a side note, don't forget to add +1 when using strlen to determine the size of your buffer ;)
You need to use new to create these char arrays, otherwise the temporaries (like tempBA) are destroyed when you exit the scope.
void BufferArray::SetBuffer(char* buf)
{
buffer = new char[strlen(buf)+1]; //edit, my size param wasn't necessary
strcpy(buffer,buf);
}