Thread: help with memory size

  1. #1
    Registered User
    Join Date
    Mar 2005

    help with memory size

    hey..i m having trouble with fixed-length format..suppose i wanna create a struct to store my variables n later on convert it into binary file format from raw text file like below :

    /* this only part of the source code. not a full program*/

    struct test {
      char a[10];
      char b[10];
      char c[5];
      unsigned int d;
    how do i made the int d occupy at most 1 byte to be stored in binary format (for eg.if int d is equal to 48 and it must stored in 48(in decimal)
    but it must no be stored as the string 48, that occupying 2 bytes. Can anyone give a tips??Thanks

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Cayman Islands
    If you are sure the integer is <= 255 you can cast it as an unsigned char, but if that's the case then why not just use unsigned char to begin with

  3. #3
    Registered User
    Join Date
    Mar 2005
    yea..thanks for reply..but because i need to read in the file and store in struct and later on i will be able to write back to binary file format and if i using unsigned char will the output will be different from unsigned int??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    But if it's always <= 255, then just leave it as unsigned char.

    You can still do normal arithmetic on it if you want to.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Find Size Of Dynamic Memory Allocation?
    By appleGuy in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2007, 09:34 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. How do I check memory size after malloc and realloc?
    By electrolove in forum C Programming
    Replies: 3
    Last Post: 02-06-2003, 10:12 AM