|
-
August 1st, 2008, 11:09 PM
#1
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.
-
August 2nd, 2008, 06:54 AM
#2
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.
-
August 2nd, 2008, 08:31 AM
#3
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?
-
August 2nd, 2008, 08:38 AM
#4
Re: Debugging Arrays
 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"!
-
August 2nd, 2008, 12:51 PM
#5
Re: Debugging Arrays
 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.
-
August 2nd, 2008, 12:53 PM
#6
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.
-
August 2nd, 2008, 12:56 PM
#7
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.
-
August 2nd, 2008, 12:59 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|