473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
2 2544
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nobody | last post by:
Heres the deal... I have an application where I have a list (as in a Windows list control, but thats not important) displayed to the user. I sort this list based on the list controls sort function...
5
by: nick | last post by:
1.int const x=5; const int x=5; 2.const char *p=new char; char const *p=new char; the above pairs are equivalent? thanks!
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
5
by: chris_doran | last post by:
/COMMENT has been removed from the linker that comes with VS2005, and use in source code of: #pragma comment(exestr, "this is a string") gives the message: warning LNK4224: /COMMENT is no...
7
by: Eric | last post by:
Hi For this code, int getopt (int argc, char *const argv, const char *opts) what does the "char *const argv" mean? Does it equal to "char **const argv"? Or "char *const *argv"? Which is the...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
3
by: Jess | last post by:
Hello, If I have a constant array, i.e. it's elements aren't changed, should I declare it as: const int a = {1,2,3}; or int const a = {1,2,3}
1
by: dhtmlkitchen | last post by:
Why does "foo" instanceof String return false? It just seems counterintuitive to me. I mean, sure, I can always check the constructor: var fooVar = "foo"; var isString =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.