Thread: Viewing variable memory...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    23

    Viewing variable memory...

    hi, i wrote the following program to print out the contents of the memory of a variable. to handle different types, i have used macros definition combined with a void pointer...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define type_scan(type, type_specifier)	 a_number = (type *)malloc(sizeof(type));\
    					 scanf(#type_specifier, (type *)a_number);\
    					 segment = sizeof(type) * 8;
    
    int main()
    {
    	void *a_number;
    	int segment;
    
    	int choice;
    	unsigned char *byte;
    	char memory[1024];
    	int i, j;
    
    	printf("Enter type (i - int, f - float, l - long, u - unsigned int, d - double) : ");
    	choice = getchar();
    
    	printf("Enter any number: ");
    	switch(choice) {
    	case 'i':
    		type_scan(int, %d);
    		break;
    	case 'f':
    		type_scan(float, %f);
    		break;
    	case 'l':
    		type_scan(long, %D);
    		break;
    	case 'u':
    		type_scan(unsigned int, %u);
    		break;
    	case 'd':
    		type_scan(double, %g);
    		break;
    	}
    
    	j = i = 0;
    	byte = (unsigned char *)a_number;
    	
    	while(j < segment) {
    		for(i = 0; i < 8; i++) {
    			memory[j++] = ((*byte & (1 << i)) > 0) ? '1' : '0';
    		}
    		byte++;
    	}
    	
    	while(j--) { 
    		printf("%c", memory[j]);
    		if((j % 8) == 0) printf(" ");
    	}
    
    	printf("\n");
    	exit(0);
    }
    the output works for int type, for eg,

    Code:
    Enter type (i - int, f - float, l - long, u - unsigned int, d - double) : i
    Enter any number: 120
    00000000 00000000 00000000 01111000
    but if a double type input is given, it shows all the high order four bytes (double being 8 bytes long) = 0 irrespective of input.

    why?? and is this even right way to print out the memory??

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The 'scanf' format string for double is "%lf". Also, creating a macro that references variables in 'main' is a bad practice, IMO.
    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;
    }

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    thanks...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're leaking memory...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing memory from variable, is something wrong?
    By Subsonics in forum C Programming
    Replies: 14
    Last Post: 08-29-2009, 11:09 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Variable Storing in memory
    By karb0noxyde in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 07:31 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM