a question on my constructor "STRING (const char*)"

S

srktnc

When I run the program, I get a Debug Error saying
"This application has requested the Runtime to terminate it in an
unusual way.
Please contact the application's support team for more information."

I put a cout statement (//cout << "len of cPtr: " << _len << endl;
) in my constructor and see that _len is 3435973837 though my character
pointer has only a few characters. Then I get the usual message as
state above.

Can someone help?

* * * * * * * * * * * * * *

Here is what I am doing:

--------------------------------------
In header file, I have the following 2 private data members along with
constructors:

char* _str;
unsigned _len;

The constructor I am having problem with is
"STRING (const char*)" that converts a null-terminated array to a
string.

-----------------------------------------------

I set private data member values as shown below:

_len=0;
_str=NULL;


My implementation for THAT constructor is as shown below:

STRING::STRING(const char* c)
/* Pre condition: Array passed must be a valid character array */
{
for (int i=0; c!=NULL; i++)
_len++;

//cout << "len of cPtr: " << _len << endl;

if (_len!=0)
{
_str = new char[_len];
for (unsigned i=0; i <_len+1; i++)
_str = c;
}
else
_str = NULL;
}

------------------------------------------

My main file has


#include "MyString.h" // my header file

int main()
{
char *cPtr= "C";

STRING S(cPtr);
cout << "S(cPtr) is " << endl;
S.Display();

return 0;
};
---------------------------------------------------
 
J

Jay Nabonne

When I run the program, I get a Debug Error saying
"This application has requested the Runtime to terminate it in an
unusual way.
Please contact the application's support team for more information."

I put a cout statement (//cout << "len of cPtr: " << _len << endl;
) in my constructor and see that _len is 3435973837 though my character
pointer has only a few characters. Then I get the usual message as
state above.

Can someone help?

* * * * * * * * * * * * * *

Here is what I am doing:

--------------------------------------
In header file, I have the following 2 private data members along with
constructors:

char* _str;
unsigned _len;

The constructor I am having problem with is
"STRING (const char*)" that converts a null-terminated array to a
string.

-----------------------------------------------

I set private data member values as shown below:

_len=0;
_str=NULL;


My implementation for THAT constructor is as shown below:

STRING::STRING(const char* c)
/* Pre condition: Array passed must be a valid character array */
{
for (int i=0; c!=NULL; i++)
_len++;


Unrelated to problem but you could use:
for (_len=0; c[_len]!='\0'; _len++)
;

or
_len = strlen(c);

I would *not* use NULL for a non-pointer.
//cout << "len of cPtr: " << _len << endl;

if (_len!=0)
{
_str = new char[_len];

should be:

_str = new char[_len+1];

Since that's how many characters you copy below.
for (unsigned i=0; i <_len+1; i++)
_str = c;
}
else
_str = NULL;
}

<snip>

- Jay
 
A

Alf P. Steinbach

* srktnc:
In header file, I have the following 2 private data members along with
constructors:

char* _str;
unsigned _len;

The constructor I am having problem with is
"STRING (const char*)"

Do not use all uppercase for non-macro names. All uppercase is a macro name
convention that helps avoid name collisions, _if_ you don't use it elsewhere.

that converts a null-terminated array to a string.

-----------------------------------------------

I set private data member values as shown below:

_len=0;
_str=NULL;

Actually you don't.

My implementation for THAT constructor is as shown below:

STRING::STRING(const char* c)
/* Pre condition: Array passed must be a valid character array */
{
for (int i=0; c!=NULL; i++)
_len++;



'_len' is uninitialized, an arbitrary value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top