Thread: Loading the stack pointer

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    Question Loading the stack pointer

    Hi,

    I am using the visual studio 06 compiler.. I am trying to load the stack pointer so to monitor where and when my stack overflows during runtime of a C code...

    I have searched the web but there doesn't seem to be a formal description how to do it ...

    Is there any geek here who can help please?

    best regards,
    bouvett

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You would need to write ASM code into your program to watch the stack pointer...

    A far simpler method would be to compile with debugging symbols and run your code in a debugger (WinDebug) and watch from there.

    What are you doing to put such a load on the stack?
    Maybe if you post your code we can help...

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    reply

    Hi thanks commontater,

    Can you please explain further how to use the simpler method please?

    Code:
    /*This function will take an input image from a webcam and apply FAST corner detection
    algorithm so to find the corners in the image.*/
    
    #include <stdio.h>
    #include "cv.h"
    #include "highgui.h"
    #include <stdlib.h>
    #include "stdbool.h"
    
    #define start_tree 17
    #define stop_tree 18 
    #define corner 19
    #define not_corner 20
    
    #define frame_width 640
    #define frame_height 480
    
    //This stucture will be used as the template node of the FAST tree
    struct tree_node {
              int pixel; 
              struct tree_node *parent; 
    		  struct tree_node *brighter; 
    		  struct tree_node *similar; 
    		  struct tree_node *darker; 
    };
    
    //global variables
    struct tree_node *root_node;//root node of the tree
    struct tree_node *current_node; //root node of the tree
    FILE *file_pointer; //pointer to the file 'FAST_tree_array.txt'
    IplImage  *frame_ = 0; //Grayscale frame
    int counter = 3;
    
    //function prototypes here
    void load_tree(void);  
    void load_tree_recursively(void);
    void FAST_corner_detector();
    void ummy(void);
    
    //main function begins here
    int main(void)
    {
    	int data= 0;
    
    	//Initialize camera using the OpenCV library
    	CvCapture *capture = 0;
        IplImage  *frame = 0; //RGB frame
    	//IplImage  *frame_ = 0; //Grayscale frame
        capture = cvCaptureFromCAM(0);
    
    	//check whether camera initialised correctly
        if ( !capture ) {
            fprintf( stderr, "Cannot open initialize webcam!\n" );
            return 1;
        }
    
    	//create a window for the captured image
    	cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
    
    	//First thing to do is to setup the FAST tree structure.
    	//'root_node' is the first node of the tree.
    	load_tree();
    
    	//Pre-processing settings
    	frame = cvQueryFrame( capture );
    	frame_ = cvCreateImage( cvSize( frame_width, frame_height ), IPL_DEPTH_8U, 1 ); //This matrix will hold the grayscale version of 
    																							  //the input frame
    	//Process camera frames until escape button is pressed
    	while(1){
    		 //capture frame
    		 frame = cvQueryFrame( capture );
    		 cvCvtColor(frame,frame_,CV_RGB2GRAY);
    
    		 //Extract Corners
    		 ummy();
    		 ummy();
    		 FAST_corner_detector();
    
    		 //print frame
    		 cvShowImage( "result", frame_);
    
    		 //Break is Esc button is pressed
    		 if ( (cvWaitKey(10) & 255) == 27 ) break;     
    	}
    
    	return 0;
    }
    
    
    //This function will convert the text file 'FAST_tree_array.txt' to a data structure of
    //tree_node.
    void load_tree(void)
    {
    	int data = 0;
    
    	root_node = (struct tree_node *)malloc(sizeof(struct tree_node)); //allocate memory to the tree
    	current_node = (struct tree_node *)malloc(sizeof(struct tree_node)); //allocate memory to the tree
    	
    	//check that file exists
    	if((file_pointer = fopen ("FAST_tree_array.txt", "rt")) == NULL){
    		printf("Filecould not be found \n");
    	}
    	else{
    
    		data = read_file(); //read first value
    
    		//Initial test
    		if(data<=16){ //load tree only if non trivial case
    			current_node = root_node; //copy pointer
    			current_node->pixel = data;
    			
    			data = read_file(); //dummy read start_tree
    
    			counter = 0;
    			counter = counter + 1;
    		    printf("%d\n",counter);
    			load_tree_recursively();
    			printf("%d\n",counter);
    		}
    		else{
    			root_node->pixel = data; //trivial case, when all training set image pixels are corners or non corners. 
    		}
    
    		fclose(file_pointer);
    	}
    }
    
    //This function will recursively create the tree structure of tree_nodes'
    void load_tree_recursively(void){
    
    int dummy, data;
    int section =1; //this variable will record the status of the operation
    				//1 = brighter, 2 = similar, 3 = darker.
    struct tree_node *temp_node; //will temporary hold a pointer to the current tree node
    
    	while(1){
    
    		temp_node = current_node; //keep record of mother node address
    
    		switch(section)
    		{
    			case 1: //brighter
    				current_node -> brighter = (struct tree_node *)malloc(sizeof(struct tree_node));
    				current_node = current_node->brighter;
    				current_node -> parent = temp_node;
    				break;
    			case 2: //similar
    				current_node -> similar = (struct tree_node *)malloc(sizeof(struct tree_node));
    				current_node = current_node->similar;
    				current_node -> parent = temp_node;
    				break;
    			case 3: //darker
    				current_node -> darker = (struct tree_node *)malloc(sizeof(struct tree_node));
    				current_node = current_node->darker;
    				current_node -> parent = temp_node;
    				break;
    			case 4: //finished processing subtree therefore return from recursive call
    				current_node = current_node->parent; //go back one tree node
    				data = read_file();
    				counter = counter - 1;
    		        printf("%d\n",counter);
    				return;
    		}
    
    		data = read_file(); //read first value
    
    		if(data<=16){
    			 current_node -> pixel = data; //update tree
    			 data = read_file(); //dummy read start_tree
    			 counter = counter + 1;
    		     printf("%d\n",counter);
    			 load_tree_recursively(); //recursive call
    			 printf("%d\n",counter);
    			 section = section + 1;
    		}
    		else if(data == corner){
    			 current_node -> pixel = corner;
    			 current_node = current_node->parent;
    			 section = section + 1;
    		}
    		else if(data == not_corner){
    			 current_node -> pixel = not_corner;
    			 current_node = current_node->parent;
    			 section = section + 1;
    		}
    	}
    }
    
    int	read_file(void){
    	//Reads an element from the text file pointed to by the global pointer 'file_pointer'
    	//sequentially
    
    	char string[2] = {0,0}; //will hold data from file, max sequence is 1 9
    	int string_counter = 0;
    	char temp = 'a'; 
    	int result = 0;
    
    
    	while(1){
    
    		fread(&temp,sizeof(temp),1,file_pointer);
    
    		if(temp != ','){
    			if(string_counter == 0){
    				string[0] = temp;
    				string_counter = string_counter + 1;
    			}
    			else{
    				string[1] = temp;
    				string_counter = 0;
    			}
    		}
    		else{ 
    			break;
    		}
    	}
    
    	//convert string to integer
    	result = atoi(string);
    	return result;
    }
    
    void FAST_corner_detector()
    {
    	//This function will be used to extract corners from 'image' using the FAST tree.
    
    	int  row=1,column=1;
    	uchar* data  = (uchar *)frame_->imageData;
    	int pixel_value;
    	int hey[480][640] ={0};
    	//bool corner_image[frame_height][frame_width] = {false};
    
    
    	for(row=0;row<frame_height;row++){
    		for(column=0;column<frame_height;column++){
    			pixel_value = data[row*(frame_ ->widthStep) + column];
    			hey[row][column] = pixel_value;
    			printf("%d \n",pixel_value);
    			//uchar* data = (uchar *)(image->imageData + row*(image ->widthStep) + column);
    		}
    	}
    
    	cvShowImage( "result", hey);
    
    }
    
    void ummy(void){
    	printf("heyyy");
    }
    The code is shown above. the function load tree() is a recursive loop to build a tree.. When I process load_tree() it functions correctly. When I then process the dummy function ummy() there is no problem also...but as soon as i process the next function "FAST_corner_detector" the debugger goes into chkstk.asm and breaks there saying "Unhandled exception at 0x00cf2157 in run_FAST_corner_detection.exe: 0xC0000005: Access violation reading location 0x000f0000."...

    Thanks for anybody's help in advance

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by bouvett View Post
    Hi,

    I am using the visual studio 06 compiler.. I am trying to load the stack pointer so to monitor where and when my stack overflows during runtime of a C code...

    I have searched the web but there doesn't seem to be a formal description how to do it ...

    Is there any geek here who can help please?

    best regards,
    bouvett
    Without using a debugger:
    Use SEH/VEH to trap STATUS_STACK_OVERFLOW and then examine the exception record.

    With a debugger:
    Just let it run until it crashes.

    Quote Originally Posted by CommonTater View Post
    A far simpler method would be to compile with debugging symbols and run your code in a debugger (WinDebug) and watch from there.
    I assume you mean WinDbg? (I googled WinDebug as I had never heard it before but all the hits I got actually meant WinDbg instead)
    WinDbg is a very powerful debugger and in my opinion by far the best debugger available for windows. But it also has a high learning curve and I get the feeling the OP hasn't even heard the word "debugger" before, or he shouldn't need to be asking this question.
    The visual studio debugger is good enough for what he's asking for, and a lot easier for a beginner to learn.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    Hi,

    I have been debugging the problem and found that the problem is not from the stack pointer.... if you look at the code, in the function FAST_corner_detector, there is an array 480*640 large. When I reduce the size of this array the compiler doesn't go to chkstk.asm... I am loading the function from main() so why is this happening?

    thanks again

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int hey[480][640]
    What is 640*480*4 ?
    Answer = 1,228,800

    What is the default stack size?
    /F (Set Stack Size) (C++)

    You could hack it by declaring
    Code:
    static int hey[480][640] ={0};
    But you would have to make sure it was cleared to 0 each time the function was called.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bouvett View Post
    Hi,

    I have been debugging the problem and found that the problem is not from the stack pointer.... if you look at the code, in the function FAST_corner_detector, there is an array 480*640 large. When I reduce the size of this array the compiler doesn't go to chkstk.asm... I am loading the function from main() so why is this happening?

    thanks again
    As Salem points out...

    When you create an array inside a function, it is placed on your program's stack which, depending on many things, will have an unknown amount of free space at that time. Your array is clearly too large to be placed on the default size stack... I would solve that problem by allocating memory (calloc() also sets to 0) on entry to the function and then using free() right after displaying it.

    As a matter of policy, I rarely create arrays on the program stack for just this reason.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by _Mike View Post
    I assume you mean WinDbg?
    Yeah... I suppose I did. But it looks like the OP has sussed it out anyway.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by CommonTater View Post
    Yeah... I suppose I did. But it looks like the OP has sussed it out anyway.
    Ah okey. I was just worried there might be some new debugger around that I had missed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Pointer offset
    By Eman in forum C Programming
    Replies: 1
    Last Post: 12-11-2010, 04:34 PM
  2. returning a pointer to stack variable
    By justAlamer in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2010, 08:41 AM
  3. corrupted stack pointer
    By megadeath16 in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2009, 01:52 PM
  4. Help is needed on creating a pointer stack
    By yuzhangoscar in forum C Programming
    Replies: 3
    Last Post: 09-15-2008, 07:57 PM
  5. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM