Thread: pointer display data problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    74

    pointer display data problem

    hi all,
    see the following code:

    Code:
    for (int i=0; i<=10; i++){
    	
    
    if(data.gettxt(i).Find(editbxtxt)>=0){ 
    
    CString *p = new CString[80];
    p = new CString(details);
    
    resultlist result;
    result.buffer= p;
    
    p++;
    }
    
    
    //result dialog:
    for((int i=0; i<=10; i++){
    list.addstring(*buffer);
    buffer++
    }
    is there any wrong with my code?since the list only display the last result, and some additional strange character row. anyone know why is that? thanks!!

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I think you want to move the bolded part outside the for loop.
    Code:
    for (int i=0; i<=10; i++){
        if(data.gettxt(i).Find(editbxtxt)>=0){ 
        CString *p = new CString[80];
        p = new CString(details);
        resultlist result;
        result.buffer= p;
        p++;
    }
    
    //result dialog:
    for((int i=0; i<=10; i++){
    list.addstring(*buffer);
    buffer++
    }
    Also, you should have use a second pointer to store the value returned by new so you can delete it later.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    i've moved that part outside but the result is ame as before, what's the right code for that?
    thx!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would hazard a guess that the "additional strange character row" is because you loop one more time than needed due to an incorrect loop condition, i.e., instead of:
    Code:
    for (int i=0; i<=10; i++){
    you should write:
    Code:
    for (int i=0; i<10; i++){
    Other than that, I suggest posting a little more code, copied from your actual code. Your current code example has syntax errors and so should not even compile.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Are you sure that you really want an array of CString objects in the first place and not something like

    scoped_array< char > p ( new char[80] );

    or even

    string p; ?

    What you've got appears to lack a lot of sense.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by citizen View Post
    Are you sure that you really want an array of CString objects in the first place and not something like

    scoped_array< char > p ( new char[80] );

    or even

    string p; ?

    What you've got appears to lack a lot of sense.

    why i use these code since if not using pointer and new and got the searching so slow and overflow if the loop increase to 200. is that pointer and dynamic memory avoid overflow and search faster, isn't it?
    thanks!

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A scoped array is an array of objects that deletes itself when it goes out of scope. You would have to treat it like any other array. Overflowing the reserved space is an issue though. You can find an implementation in boost's libraries.

    OTOH, a std::string is a STL data type for text data, and resizes itself, only making sure that there is at least enough space for the text.

    As to what you really need: it looks like you are only using one CString object, which makes me wonder why you allocated an array of them. So a simple string variable looks to be more efficient.
    Last edited by whiteflags; 01-01-2008 at 05:31 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mr_empty View Post
    hi all,
    see the following code:

    Code:
    for (int i=0; i<=10; i++){
    	if(data.gettxt(i).Find(editbxtxt)>=0){ 
    		CString *p = new CString[80];
    		p = new CString(details);
    		resultlist result;
    		result.buffer= p;
    		p++;
    	}
    
    	//result dialog:
    	for((int i=0; i<=10; i++){
    		list.addstring(*buffer);
    		buffer++
    	}
    is there any wrong with my code?since the list only display the last result, and some additional strange character row. anyone know why is that? thanks!!
    Oops! Indentation error!
    That's why we typically indent our code properly to make sure it works.

    Your code is so flawed and doesn't match that it really doesn't make sense to optimize it. Post your real code instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  5. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM