Allocate memory, inform user of memory start and end address and store hex data

This is a discussion on Allocate memory, inform user of memory start and end address and store hex data within the C Programming forums, part of the General Programming Boards category; Hello all Beginner here, running through some exercises I've found. I would like to allocate 64k of memory, printf to ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    27

    Allocate memory, inform user of memory start and end address and store hex data

    Hello all

    Beginner here, running through some exercises I've found. I would like to allocate 64k of memory, printf to the user the starting and ending location of this memory, then store some hex data to this memory block.

    Then further down the line i'll be reading this hex data etc. but I'm not at that stage yet

    It has to be ansi c and fully portable.

    So far I have

    Code:
    char *hex;
    hex = (char*)malloc(65553);
    hex = NULL;
    
    
    sscanf("0123050086FF9701867FB70601C60FF7060286009700BD051F86019700BD05267E050EFE90", "%x", &hex);
    Thanks very much, it may be simple but i'm trying to get my head around it

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    If you want just to print the starting and ending location of the memory you only need the size of the memory and the pointer to the memory. The start address is hex and one past the end is hex + 65553. You just have to print the value of the pointer. Not the value of the object being pointed to, even.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    char *hex;
    hex = (char*)malloc(65553);
    malloc returns a pointer to the start of the memory block which you store in hex. (You don't need to cast the pointer malloc returns in ANSI C).
    Code:
    hex = NULL;
    After this line hex will point to NULL which means you have deleted any reference to the allocated memory block and a little more than 64K of memory are lost (64K would be 65536 bytes).
    So do what whiteflags told you and don't change the address hex points to.

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Ok so

    Code:
    char *point
    point = (unisgned char *)malloc(65553)
    if (point == NULL)
    {
    printf("Cannot allocate memory \n\r");
    exit(1);
    }
    
    /* store data */
    *point= 0x86;
    *(point+1) = 0xff;
    Is what I currently have preeeetty much

    So I just printf(hex);

    printf(hex+'65553')

    I think is the jist of what you said

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by mrapoc View Post
    So I just printf(hex);

    printf(hex+'65553')
    I'm not sure if your are just sloppy or don't know the syntax for printf().
    Both lines should be:
    Code:
    printf("Start of memory block: %p\n", point);   // the pointer's name is point not hex
    printf("End of memory block: %p\n", point + 65553 - 1)
    %p is the format specifier for printing a memory address, point + 65553 - 1 for the last byte of the allocated block you can access.


    Manual page for printf()


    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    I do know it, sometimes need reminding, but in that case I was just writing noteform :P

    Thanks though

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    So I cannot get

    printf("Start of memory block: %p\n", point); // the pointer's name is point not hex
    printf("End of memory block: %p\n", point + 65553 - 1)

    to work within my main without declaring

    unsigned char *hex;

    above my main. It compiles when I do, but I get a different address when called this way, than if I call it from within the function which is actually malloc-ing and storing the data.

    How do I get the same pointer address from the function? My guess is that by doing it within main, using a global variable (bad) I am pretty much screwing it up

    edit: sorted i think, cheers
    Last edited by mrapoc; 07-04-2012 at 08:49 AM.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    nearly there

    I have one issue at the moment. When i ask for memory location within the test data function it works fine. But I want it passed back to the main function so it can be displayed within the menu. How would you propose I do this?

    Code:
    /***********************************************************************//*Digital embedded retake*/
    /*By Sam James JW004171*/
    /***********************************************************************/
    
    
    /***********************************************************************/
    /*Inclusions*/
    /***********************************************************************/
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    
    
    /***********************************************************************/
    /*Indentifiers*/
    /***********************************************************************/
    
    
    int testData(void);
    
    
    /***********************************************************************/
    /*Main program*/
    /***********************************************************************/
    
    
    int main (void)
    {
    	/***********************************************************************/
    	/*Declarations*/
    	/***********************************************************************/
    	
    	unsigned char *hex;
    	int i;
    
    
    	/*Create test data in memory*/
    	testData();
    
    
    	/*Display menu*/
    		
      printf("Please make a choice: \n");
      
      /*Choices*/
      printf("1. Display contents of memory\n");
    
    
      /*Choice made*/
      scanf("%d", &i);
      switch(i) {
        case 1:      
    /*This doesn't work as intended*/
          printf("Start of memory block: %p\n", hex);   
    	  printf("End of memory block: %p\n", hex + 65553 - 1);
        
        default:
          printf("Incorrect option");
      }
    
    
      return 0;
    }
    	
    
    
    	/*printf("Start of memory block: %p\n", hex);   
    			printf("End of memory block: %p\n", hex + 65553 - 1);*/
    
    
    
    
    /***********************************************************************/
    /*Setup test data*/
    /***********************************************************************/
    int testData(void)
    {
    	/*Declarations*/
    	unsigned char *hex;
    	int t = 0;
    	int i = 0;
    	int p = 0;
    	int x = 0;
    	char str[3];
    	char testData1[65] = "BDB653CC0800FD1018FD101AFD101CFD101EBDB665CC0002DD007F00067F0010";
    
    
    	/*Allocate memory*/
    	hex = (unsigned char *)malloc(65553);
    	if (hex == NULL)
    	{
    		printf("Cannot allocate memory \n\r");
    		exit(1);
    	}
    
    
    	/*Store data*/
    	x = sizeof(testData1) / sizeof(char);
    	p = x-2;
    	for (i = 0; i <= p; i += 2)
    	{
    		strncpy(str, testData1 + i, 2);
    		str[2] = '\0';		
    		hex[t++] = (unsigned char)strtoul(str, NULL, 16);	
    
    
    		}
    /* This works as intended*/
    	printf("Start of memory block: %p\n", hex);   
    			printf("End of memory block: %p\n", hex + 65553 - 1);
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    The variable named hex in main is a different variable from the one named hex in testData; they are both local to the respective functions that they are declared in. One way out is to have testData return hex, but you must remember to free() in main.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    FYI: 64K or 2 to the power of 16 is 65536; not the value 65553 or 65535.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Thanks very much guys/gals

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  2. Finding memory address using memory pattern
    By MindWorX in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2008, 07:20 AM
  3. Can't allocate enough memory for image data
    By slowcoder in forum C Programming
    Replies: 5
    Last Post: 08-06-2006, 05:13 AM
  4. Using extended memory to store program data
    By Robby in forum C Programming
    Replies: 1
    Last Post: 01-11-2005, 07:19 AM
  5. when to allocate memory
    By SAMSAM in forum Windows Programming
    Replies: 3
    Last Post: 01-22-2003, 10:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21