Thread: pointers and dynamic memory

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Unhappy pointers and dynamic memory

    hello guys...

    here is my code....

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    #include <ctype.h>
    #include <stdbool.h>
    #include <string.h>
    
    #define WHITE 'w'
    #define BLACK 'b'
    
    static int numRows;
    static int numCols;
    static int whiteCount=0;
    static int blackCount=0;
    static int numMoves=0;
    static char *ptr, *tmp = NULL;
    static char *passer = NULL;
    void printMatrix(char * addr,int a, int b);
    void initializeMatrix(char * addr, int a, int b);
    void putwhite(char * addr, int num);
    
    int main (void)
    {
    	int r,w,second;
    	char first;	
    
    	bool defaultMatrix = false;
    	bool incorrectWhiteEntry = false;
    	bool incorrectBlackEntry = false;
    	bool gameFinish = false;
    	bool allOK = false;
    	int memCount = 0;
    	char str[10];
    	int temp;
    	printf("Enter the num of rows: ");
    	scanf("%d", &numRows);
    	printf("Enter the num of cols: ");
    	scanf("%d", &numCols);
    	
    	
    	if(defaultMatrix || numRows >19 || numCols >19)
    	{
    		printf("Matrix Size is: %i x %i \n", numRows, numCols);
    		printf("Defaulting the matrix to 10 x 10 \n");
    		numRows = 10;
    		numCols = 10;
    		allOK = true;		
    	}	
    	else
    	{
    		//printf("test");		
    		printf("Matrix Size is: %i x %i \n", numRows, numCols);
    		
    		allOK = true;
    	}
    	
    	ptr = malloc(numRows*numCols);
    	tmp = memset (ptr, '\0', numRows*numCols);
    	//printf("test");
    	if(tmp ==  NULL)
    	{
    		printf("Could not associate Dynamic Memory. Error !! Exiting...\n");
    		return 0;		
    	}	
    	else
    	{
    		ptr = tmp;
    	}
    	if(allOK)
    	{
    		char othelloMatrix[numRows - 1][numCols - 1];
    	}
    	
    	passer = &(*ptr);
    	initializeMatrix(passer, numRows,numCols);
    	printMatrix(passer, numRows, numCols);
    	//printf("test1");
    	while(!gameFinish)
    	{
    		
    		
    		for(memCount = 0; memCount < numRows*numCols; memCount++)
    		{
    			if(ptr[memCount] = '\0')
    				gameFinish = false;
    			else
    				gameFinish = true;
    		} 
    		
    		printf("White Player enter your matrix row :");
    		scanf("%s", &first);
    		printf("White Player enter your matrix col :");
    		scanf("%d", &second);
    		//printf("%i % i", first ,second);
    		if(first - 96 > numRows || second > numCols)
    		{
    			incorrectWhiteEntry = true;
    		}
    		else
    		{
    			incorrectWhiteEntry = false;
    		}
    		while(incorrectWhiteEntry)
    		{
    			printf("White Player enter your matrix row :");
    			scanf("%s", &first);
    			
    			printf("White Player enter your matrix col :");
    			scanf("%d", &second);
    			if(first - 96 > numRows || second > numCols)
    			{
    				incorrectWhiteEntry = true;
    			}
    			else
    			{
    				incorrectWhiteEntry = false;
    			}
    		}
    		//printf("test");
    		//printf("White player entered at %cx%i \n",first,(numCols-1)*(first -96 -1) + (second -1)+ *ptr);
    		for(temp = 0; temp < numRows*numCols; temp++)
    		{
    			printf("temp %c", ptr[temp]);
    		}
    		printf("test %i %i %i",second,  first-96, (numCols -1)*(first -96 ) + (second-1) );
    		putwhite(passer, (numCols-1)*(first -96 -1) + (second-1));
    		printMatrix(passer, numRows, numCols);
    	}	
    	int d;
    	scanf("%d", &d);
    	free(ptr);
    	return 1;
    
    }
    
    void putwhite(char *addr, int num)
    {
        char * mtrxPtr;
    	mtrxPtr = addr;
                       
    	mtrxPtr[num] = WHITE;
    	whiteCount++;
    	numMoves++;
    	
    }
    void initializeMatrix(char *addr, int a, int b)
    {
    	int firstw,firstb,secondw,secondb = 0;
    	char * mtrxPtr;
    	mtrxPtr = addr;
        
        //printf("address is:");
    	firstw = ((b* (a/2 -1)) + (b/2 -1));
    	firstb = ((b* (a/2-1)) + (b/2));
    	secondb = ((b* (a/2)) + (b/2-1));
    	secondw = ((b* (a/2)) + (b/2));
    		
    	mtrxPtr[firstw] = WHITE;//white;
        mtrxPtr[firstb] = BLACK;//blacke;
        mtrxPtr[secondw] = WHITE;//white;
        mtrxPtr[secondb] = BLACK;//black;
    	//mtrxPtr[test+1] = black;
    }
    void printMatrix(char *addr, int a, int b)
    {
    	int i,j = 0;
    	char * mtrxPtr;
    	mtrxPtr = addr;
    	system("clear");
    	//printf("rt");
    		for(i=0;i<= a; i ++)
    		{
    			printf(" %i  |",i);
    				
    		}
    		
    		printf("\n");
    		for(i=0;i<= a; i ++)
    		{
    			printf("-----");		
    		}
    		printf("\n");
    		i = 1;
    		for(j=1; j <= b; j++)
    		{
    			printf(" %c  |",(j+96));
    			for(i = 1; i <=a; i ++)
    			{
    				if(mtrxPtr[b*(i-1) + (j -1)] == '\0')
    					printf("    |");
    				else
    					printf(" %c  |",mtrxPtr[b*(i -1)+ (j-1)]);
    						
    			}
    			printf("\n");
    			for(i=0;i<= a; i ++)
    			{
    				printf("-----");		
    			}
    			
    			printf("\n");
    		}
    			
    		
    	//}	
    
    }
    my problem is that i cannot seem to save the value of ptr pointer.... every time i reference it the value seems to reset. after the initializatin it should have 2w's and 2b's in it.. but when i try and print it. it comes as empty... the entry is int for rows and cols...ne ideas ??
    for white player... it is alphabet and int...

    thanks for the help..

    tanmay

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I didn't go through the whole code, but a couple things I noticed:

    >> if(defaultMatrix || numRows >19 || numCols >19)

    It seems strange that if the dimension is >= 20 you would set it to 10. Shouldn't they be the same?

    >> tmp = memset (ptr, '\0', numRows*numCols);

    You need to check the value of ptr *before* memset (to ensure that the allocation succeeded). You don't have to check the return value of memset, either. The only way that could fail is if there was a hardware fault in the RAM, in which case the whole show would be over for your program *and* the OS. So don't bother.

    >> char othelloMatrix[numRows - 1][numCols - 1];

    You do realize that this array only exists within the scope of those braces, right?

    >> passer = &(*ptr);

    This is no different from passer = &(*(&(*&(*(&(*ptr)))))), and conveniently, passer = ptr.

    >> if(ptr[memCount] = '\0')

    Careful, there. Don't confuse == with =.

    EDIT:
    Also, the loop with the game-end detection logic looks wrong. If you're wanting to end the game only when all cells are zero, you should first set "game_over" to true before the loop, and then inside the loop, if a non-zero value is detected, set "game_over" to false and break out of the loop immediately. I'm not sure if that was your intent, though.
    Last edited by Sebastiani; 06-16-2009 at 09:29 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 05:58 AM
  4. Dynamic memory confusion
    By ripper079 in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2002, 06:15 PM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM