pointer display data problem

This is a discussion on pointer display data problem within the C++ Programming forums, part of the General Programming Boards category; 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 = ...

  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,053
    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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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
    Posts
    23,021
    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, 04:45 AM
  4. Replies: 4
    Last Post: 06-14-2005, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21