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

Discussion in 'C++' started by srktnc, Sep 16, 2005.

  1. srktnc

    srktnc Guest

    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;
    };
    ---------------------------------------------------
    srktnc, Sep 16, 2005
    #1
    1. Advertising

  2. srktnc

    Jay Nabonne Guest

    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!=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
    Jay Nabonne, Sep 16, 2005
    #2
    1. Advertising

  3. * 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.

    --
    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?
    Alf P. Steinbach, Sep 16, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,030
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,320
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,803
    David Harmon
    Jan 23, 2004
  4. Replies:
    6
    Views:
    495
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,202
Loading...

Share This Page