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

    Debugging Arrays

    I want to change the results of function deal() below which fills out a string array. In the watch window I can change the result of the memory location that the pointer points to, but not the string that begins at that address.

    Is it possible to keep the memory location the same but alter the string?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::left;
    using std::right;
    
    #include <iomanip>
    
    using std::setw;
    
    #include <cstdlib>
    #include <ctime>
    
    void shuffle( int[][13]);
    void deal(const int [][13], const char *[], const char *[], const char *[][2]);
    bool Pair( const char *[][2], int);
    bool TwoPair(const char *[][2], int);
    
    
    int main()
    {
    	const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    
    	const char *face[13] = { "Ace","Deuce","Three","Four",
    		                     "Five", "Six", "Seven", "Eight",
    							 "Nine", "Ten", "Jack", "Queen", "King"};
    
    	const int Size=5;
    	bool pairRes =false;
    
    	int deck[4][13] = {0};
    	const char *hand[Size][2];
    	
    
    
    	srand(time(0));
    
    	shuffle( deck );
    	deal( deck, face, suit, hand);
    	pairRes = Pair(hand, Size);
    
    
    	return 0;
    }
    
    void shuffle(int wDeck[][13])
    {
    	int row;
    	int column;
    
    	for ( int card =1; card <= 52; card++){
    
    		do {
    			row = rand() % 4;
    			column = rand() % 13;
    		} while ( wDeck[row][column] != 0);
    
    		wDeck[row][column] =card;
    
    	}
    
    }
    
    void deal( const int wDeck[][13], const char *wFace[], const char *wSuit[], const char *hand[][2])
    {
    	for ( int card =1; card <= 5; card++)
    
    		for ( int row = 0; row <=3; row++)
    
    			for ( int column =0; column <=12; column++)
    				
    				if ( wDeck[row][column] == card) {
    					hand[card-1][1] = wSuit[row];
    					hand[card-1][0] = wFace[column];
    				}
    }
    
    bool Pair( const char *hand[][2], int Size)
    {
    	for ( int i = 0; i < Size; i++)
    
    		for ( int j = i+1; j< Size; j++)
    
    			if ( hand[i][0] == hand[j][0])
    
    				return true;
    			
    				return false;
    }
    
    bool TwoPair( const char *hand[][2], int Size)
    {
    	int counter =0;
    	
    	for ( int i = 0; i < Size; i++)
    
    		for ( int j = i+1; j< Size; j++)
    
    			if ( hand[i][0] == hand[j][0])
    
    			{counter++;
    			if (counter == 2)
    				return true;}
    			
    				return false;
    }
    Last edited by Bazman; August 2nd, 2008 at 06:55 AM.

  2. #2
    Join Date
    Jun 2005
    Posts
    181

    Re: Debugging Arrays

    I altered the above post just trying to get the attention of those who may have already read the old post. New one explains the problem much beter I feel.

  3. #3
    Join Date
    Dec 2007
    Posts
    17

    Re: Debugging Arrays

    the os delivered you some sized memory chunk to hold the variable,
    what if next time you asked for a larger chunk to hold the new content?

  4. #4
    Join Date
    Jul 2008
    Posts
    9

    Re: Debugging Arrays

    Quote Originally Posted by Bazman
    Is it possible to keep the memory location the same but alter the string?
    You use only one location for different strings, for example 2, at the same time, I am afraid.
    "They clearly know it is a blank document, but still keep referencing.... because they can make up other blank documents"

    Andrei Washington is keeping a "prinsess" of a Vietnamese Northerner living in Canada; that talkative man is a Brodly's friend who is also a Paul's workman. Yes they will offer, on behalf of their friendship, but now, wait up "a little"!

  5. #5
    Join Date
    Jun 2005
    Posts
    181

    Re: Debugging Arrays

    Quote Originally Posted by Voominibear
    the os delivered you some sized memory chunk to hold the variable,
    what if next time you asked for a larger chunk to hold the new content?
    Hi there,

    Thanks for the response.

    However the problem is that I can not change the string at a given address at all! No matter if I use a same size string or even a smaller one?

    If you view hand[0][0] in the watch window you can change the memory location it points to, but you cannot change the string stored at either the original location nor the one you re-point it to.

  6. #6
    Join Date
    Jun 2005
    Posts
    181

    Re: Debugging Arrays

    Hi can you please elborate on the above?

    I am not tryig to store 2 different strings in one location. I want to replace the string in a given location? via the pointer to that string.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Debugging Arrays

    Probably because the strings you're attempting to change live in read-only memory. I'm not sure if the debugger would enforce that, but it might.

    You *might* have better luck if you replace your c-style strings with std::strings. I'm not sure if the debugger will let you alter those, but it's probably a good idea in any case.

  8. #8
    Join Date
    Jun 2005
    Posts
    181

    Re: Debugging Arrays

    Hi there,

    I'd forgotten that hand was definted as const char. I'll dee if taking off the const constraint makes any difference! I suspect it will.

    Haven't covered std::strings yet so they'l have to wait!

    Thanks

    Baz

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