[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C++ arrays on hte free store

Posted on 2004-11-13
5
Medium Priority
?
303 Views
Last Modified: 2006-11-17
class String
    {
    public:
        //Constructors
        String();
        String(const char *const);
        String(const String &);
        ~String();
   
    private:
        String(int);
        char * itsstring;
        unsigned short itslen;
   
   
    };

String::String()
    {
        cout << "Making String Class" << endl;
    }
 
String::String(int len)
    {
        itsstring = new char[len+1];
        for(int i=0;i<len;i++)
            itsstring[i] = "\0";                    //ERROR
        itsstring[len] = "\0";                    //ERROR
        itslen=len;
        cout << "\tString(int) constructor" << endl;
    }    


In the previous piece of code, my compier comes up with an error on the two lines with the ERROR comments.

It says "invalid conversion from `const char*' to `char' ".

if i try using the indirection operator to access the value stored at where the pointer points to on the free store then it comes up with a different error.

It then says "invalid type argument of `unary *'".

Please could someone help me

Thanks
0
Comment
Question by:IrAaMnVdEeRyY
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 12573386
Try this:

itsstring[i] = '\0';

or:

itsstring[i] = 0;

itsstring[i] is char, and you are trying to assign char* to it.

0
 
LVL 30

Expert Comment

by:Axter
ID: 12573929
FYI:
I recommend  you make the len arguement, the same type as the itslen argument.

String::String(unsigned short len)

Some compilers will give you a warning if you assigned an int to an unsigned short.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 12574226
A more efficient way of doing your loop:
for(int i=0;i<len;i++)
            itsstring[i] = "\0";                    //ERROR
        itsstring[len] = "\0";                    //ERROR

is:
memset(itsstring, 0, len + 1); // Set all members to 0
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12574917
Actually, the length argument of a string class should count the number of non-zero characters as the zero character normally '\0' terminates the string. Most string classes have two lenght members, one for allocated length and one for string length. Then, the string buffer must be only reallocated when the string is growing. I agree with Axter that length type of argument and member should be same, but would recommend to change to int type rather than to a short or unsigned short as the int type is the best supported and most usual type. When using short type as an argument, you'll often get compiler warnings stating that converting an int to a short may leed to a loss of information. Though, that warning rarely is true, you could avoid that by choosing int as member type.

class String
    {
    public:
        //Constructors
        String();
        String(const char *const);
        String(const String &);
        ~String();
   
    private:
        String(int);
        char * itsstring;
        int itslen;
        int itsalloc;
    };

String::String()
    {
        cout << "Making String Class" << endl;
    }

// allocate a string buffer fro an empty string  
String::String(int alloc)
   : itslen(0), itsalloc(alloc)
    {
        itsstring = new char[alloc];
        itsstring[0] = '\0';   // It is sufficient to set the terminating zero character      
        cout << "\tString(int) constructor" << endl;
    }    

Regards, Alex


0
 
LVL 4

Expert Comment

by:Skonen
ID: 12575133
On another note, you're not freeing the memory you allocated to itsstring, it'd be wise to delete it in your deconstructor:


....

        // Constructors
        String();
        String(const char *const);
        String(const String &);

        // Deconstructor
        ~String() { if (itsstring) delete [] itsstring; }
   
 ....


Good luck,
      Stuart Konen
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question