Thread: displaying stack contents

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    displaying stack contents

    hi

    i thought it would be funny to see what the stack looks like in general so i prorgammed something that shows it in a very rough way.
    that code took me less than 5 minutes, so dont blame me for it.
    maybe you know how to make it better?

    Code:
    /* ever wondered what your stack may look like? */
    
    #include <stdio.h>
    
    /* uncomment lines to log to a file called \'memlog\' */
    
    int main (void)
    {
        //FILE *fd;
        char buffer[] = "this is a test!\n";
        int i;
        
        /*
        if(((fd = fopen("memlog", "a"))) == 0){
    	exit(1);
        }
        */
        
        for(i=0;i<20000;i++){
    	printf("%p\t%c\n", &buffer[i], buffer[i]);
    	//fprintf(fd, "%p\t%c\n", &buffer[i], buffer[i]);
        }
        
        //fclose(fd);
        
        return 0;
    }
    thanks

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Add a error handler in case of a memory access error:

    Code:
        for(i=0;i<20000;i++){
    	try {
                printf("%p\t%c\n", &buffer[i], buffer[i]);
                //fprintf(fd, "%p\t%c\n", &buffer[i], buffer[i]);
            }
    
            catch( ... ) {
                printf( "%p\tUnaccessible\n", &buffer[ i ] );
                //fprintf( fd, "%p\tUnaccessible\n", &buffer[ i ] );
            }
    	
        }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Sorry, didn't realize this was the Linux forum.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Originally posted by vVv
    Oh boy...
    This is the most useless snippet I've seen for a long time. It buys nothing and makes very poor assumptions about memory mapping (this would dump core with SIGBUS/SIGSEGV on many systems). My advice: Throw this trash away and use gdb if you want to see useful stack traces.
    oh boy, ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM