CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 1999
    Posts
    32

    double pointer problem..

    #include <iostream.h>
    void main()
    {
    char **pp = new char*[10];
    char *p[10];
    for(int i=0; i<10; i++)
    p[i] = new char[10];
    p[0] = "This";
    p[1] = " is";
    p[2] = " a";
    p[3] = " test";
    p[4] = " version.";
    p[5] = " You";
    p[6] = "know?";
    p[7] = " I";
    p[8] = " love";
    p[9] = " you..";
    for(i=0; i<10; i++)
    {
    pp[i] = p[i];
    cout << pp[i] << endl;
    }

    for(i=0; i<10; i++)
    delete [] pp[i]; // <== Here is the problem
    delete [] pp;
    }

    This is my test version of double pointer.
    Almost everything is executed well.
    But the execution is halted in the arrow marked
    position.
    Could you give me a solution to this problem.
    I programmed this source in Visual C++ 6.0.
    I can't understand the fact that
    when I execute this source in Debug mode,
    the execution is halted but in Release mode,
    it's OK.
    Need your help


  2. #2
    Guest

    Re: double pointer problem..

    There appears to be more than one problem with that source.

    1) although you declare

    char *p[10];

    you then use

    p = new char[10]; // in your contest you are creating
    // 10, 10 char arrays and assigning
    // each array the same address effectively
    // overwriting the previous on

    I think what you actually meant to say was

    p[i] = (char *)new char[10];

    2. you use

    p[0] = "This";

    and you actually intend

    strcpy(p[0],"This");

    3. In your delete loop you are actually delete pp 10 times?
    You allocate it once, and should only delete it once. So this loop
    should look something like

    for (int i = 0; i < 10 ; i++)
    {
    delete [] p[i];
    }
    delete [] pp;


    Look at this source and see if isn't closer to way you intend
    #include <string.h>
    #include <iostream.h>

    char *samples[] =
    {
    { "This" },
    { "is" },
    { "a" },
    { "test" }
    };

    void main()
    {
    char **pp = new char * [4];
    for(int i=0; i<4; i++)
    {
    pp[i] = (char*)new char[10];
    strcpy(pp[i],samples[i]);
    }


    for(i=0; i<4; i++)
    delete [] pp[i] ;
    delete [] pp;
    }



  3. #3
    Join Date
    Jun 1999
    Posts
    6

    Re: double pointer problem..

    Hey, i'm not used to all this pointer stuff, even though i use them alot,...

    Whats the delete []pp;

    mean???

    Word,
    Ryan "Radman" Du Bois

  4. #4
    Join Date
    May 1999
    Posts
    44

    Re: double pointer problem..

    It should be a good idea to use j for counters instead of i as i between brackets is the tag for putting text in italic.


  5. #5
    Guest

    Re: double pointer problem..

    delete the array pp;




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured