467,144 Members | 1,126 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,144 developers. It's quick & easy.

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

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[i]!=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[i] = c[i];
}
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;
};
---------------------------------------------------

Sep 15 '05 #1
  • viewed: 2115
Share:
2 Replies
On Thu, 15 Sep 2005 16:07:10 -0700, srktnc wrote:
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[i]!=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[i] = c[i];
}
else
_str = NULL;
}

<snip>

- Jay
Sep 15 '05 #2
* 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[i]!=NULL; i++)
_len++;

'_len' is uninitialized, an arbitrary value.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Nobody | last post: by
5 posts views Thread by nick | last post: by
4 posts views Thread by C. J. Clegg | last post: by
5 posts views Thread by chris_doran@postmaster.co.uk | last post: by
7 posts views Thread by Eric | last post: by
20 posts views Thread by liujiaping | last post: by
3 posts views Thread by Jess | last post: by
1 post views Thread by dhtmlkitchen@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.