Thread: Memory pointers

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Red face Memory pointers

    I use malloc(1024) to allocate a memory area of 1024 bytes. I can write a 8 bit variable to the pointer position. But If I write a 64 bit variable to the pointer position only 8 bit's are written.

    My 64 bit variable value looks like this: F1F2F3F4F5F6F7F8
    My 8 bit variable value looks like this: FF


    If I write the 8 bit variable to memory it looks like this:

    FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    If I write the 64 bit variable to memory it looks like this:

    F1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    But I want the memory to look like this when my 64 bit variable is written:

    F1 F2 F3 F4 F5 F6 F7 F8 00 00 00 00 00 00 00 00

    After the 8 bit write I increase the pointer by 1, after the 64 bit write I increase the pointer by 8.

    What am doing wrong here?

    Can someone please help me with some example code?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Can someone please help me with some example code?
    Post your code.

    Suggestions:

    Lookup memcpy() / memmove()

    64bits is 8 bytes, so make a loop that runs 8 iterations and copies one character at a time.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Hammer
    >>Can someone please help me with some example code?
    Post your code.

    Suggestions:

    Lookup memcpy() / memmove()

    64bits is 8 bytes, so make a loop that runs 8 iterations and copies one character at a time.
    Here is some code snippets:

    Byte *myPtr;

    // get some memory for the record function
    myPtr = malloc(1024); // allocate 1024 bytes
    myPtrCopy = myPtr;

    // saving a 64 bit value
    *myPtr = timestamp;
    // increase pointer by 8 bytes
    myPtr+=8;

    // save a 8 bit value
    *myPtr = length;
    // increase pointer by 1 byte
    myPtr+=1;

    Why can't I just save a 64 bit value to memory in the same way as saving 8 bit values?

    Lookup memcpy() / memmove()
    What does that mean?

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Why can't I just save a 64 bit value to memory in the same way as saving 8 bit values?
    You do save them the same way, copy the value and then step over it to the next open space.

    >>What does that mean?
    memcpy() and memmove() copy n bytes of data to memory, like strcpy() except a nul terminator isn't required. memmove() can handle overlapping memory as well :-)
    *Cela*

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Wink

    Originally posted by Cela
    >>Why can't I just save a 64 bit value to memory in the same way as saving 8 bit values?
    You do save them the same way, copy the value and then step over it to the next open space.

    >>What does that mean?
    memcpy() and memmove() copy n bytes of data to memory, like strcpy() except a nul terminator isn't required. memmove() can handle overlapping memory as well :-)
    I just can't get it working. If I look at the memory after I have written a couple of 8 bit and 64 bit values I see the following:

    0x1b000000 0x00000000 0x03903c3a 0x00280000 0x00000000 0x0003903c

    First 8 bytes is the 64 bit value, and as you can see it's only 1b (first byte) that is written, not all 8 bytes, the 8 bit values starting at 03 is ok.

    Why?

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Talking

    Originally posted by electrolove
    I just can't get it working. If I look at the memory after I have written a couple of 8 bit and 64 bit values I see the following:

    0x1b000000 0x00000000 0x03903c3a 0x00280000 0x00000000 0x0003903c

    First 8 bytes is the 64 bit value, and as you can see it's only 1b (first byte) that is written, not all 8 bytes, the 8 bit values starting at 03 is ok.

    Why?
    I have tried this and it works, but it seems complicated when all I want to do is to write the 64 bit value to memory.

    UInt64 variable1 = 0xF1F2F3F4F5F6F7F8;
    Byte variable2;
    for (a = 0; a < 8; ++a)
    {
    variable2 = variable1;
    printf("%02X ", variable2);
    variable1 = variable1 >> 8;
    }

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I just can't get it working.
    It's tricky when you want to put different sized data at random places in a block of memory that doesn't distinguish between them :-) I would do it with a flag before every subblock telling the type so that you can read the right values
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    enum {BIT8 = -1, BIT32 = -2};
    
    static signed char *mem_block;
    static size_t       index;
    
    static void seek_begin(void)
    {
      index = 0;
    }
    
    static void set_block(signed char type, int data)
    {
      size_t size = (type == BIT8) ? sizeof(signed char) : sizeof(int);
    
      mem_block[index++] = type;
      memcpy(mem_block + index, &data, size);
      index += size;
    }
    
    static int get_block(void)
    {
      size_t size;
      int buffer = 0;
      
      size = (mem_block[index++] == BIT8) ? sizeof(signed char) : sizeof(int);
    
      memcpy(&buffer, &mem_block[index], size);
      index += size;
    
      return buffer;
    }
    
    int main(void)
    {
      mem_block = malloc(1024);
    
      set_block(BIT8, 127);
      set_block(BIT32, 5000);
    
      seek_begin();
      printf("Recovered -- %d\n", get_block());
      printf("Recovered -- %d\n", get_block());
    
      free(mem_block);
    
      return 0;
    }
    The above code doesn't worry about errors by the way, you'd have to add such checking :-)
    *Cela*

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Red face

    Originally posted by Cela
    >>I just can't get it working.
    It's tricky when you want to put different sized data at random places in a block of memory that doesn't distinguish between them :-) I would do it with a flag before every subblock telling the type so that you can read the right values
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    enum {BIT8 = -1, BIT32 = -2};
    
    static signed char *mem_block;
    static size_t       index;
    
    static void seek_begin(void)
    {
      index = 0;
    }
    
    static void set_block(signed char type, int data)
    {
      size_t size = (type == BIT8) ? sizeof(signed char) : sizeof(int);
    
      mem_block[index++] = type;
      memcpy(mem_block + index, &data, size);
      index += size;
    }
    
    static int get_block(void)
    {
      size_t size;
      int buffer = 0;
      
      size = (mem_block[index++] == BIT8) ? sizeof(signed char) : sizeof(int);
    
      memcpy(&buffer, &mem_block[index], size);
      index += size;
    
      return buffer;
    }
    
    int main(void)
    {
      mem_block = malloc(1024);
    
      set_block(BIT8, 127);
      set_block(BIT32, 5000);
    
      seek_begin();
      printf("Recovered -- %d\n", get_block());
      printf("Recovered -- %d\n", get_block());
    
      free(mem_block);
    
      return 0;
    }
    The above code doesn't worry about errors by the way, you'd have to add such checking :-)
    Well, I have no problem to read the values because all data is written at a pattern. Let me explain. First I write (or try to write) a 64 bit value, after that I write a 8 bit counter, after that as many 8 bit values as the counter. And everything starts all over again with the 64 bit value :-) So this is really not the problem. The problem is that when I write 64 bit values only 1 byte is written and the other 7 bytes is zero regardless of what they are in my variable. Strange...

  9. #9
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>The problem is that when I write 64 bit values only 1 byte is written and the other 7 bytes is zero regardless of what they are in my variable.
    Have you tried memcpy() as my example used? The problem is that you're not writing more than one byte of the 8 byte value, the rest of the memory block remains whatever you had before, which is probably 0 :-)
    *Cela*

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    63
    Originally posted by Cela
    >>The problem is that when I write 64 bit values only 1 byte is written and the other 7 bytes is zero regardless of what they are in my variable.
    Have you tried memcpy() as my example used? The problem is that you're not writing more than one byte of the 8 byte value, the rest of the memory block remains whatever you had before, which is probably 0 :-)
    I have tried memcpy now and it works, nice :-)

    Last question, or I will not learn anything from this ;-)

    To write a 64 bit value to memory the way I was trying seems not possible when programming in C, right or wrong?

  11. #11
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>To write a 64 bit value to memory the way I was trying seems not possible when programming in C, right or wrong?
    Wrong, it just takes a little bit of work to get it right. This works just fine, I used a 32 bit variable to make things a little shorter :-)
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int recover = 0;
      int big_val = 12345;
      char mem_block[5] = {0};
    
      /* Write a 32 bit integer to multiple mem_block cells */
      mem_block[0] = (char)(big_val & 0xff);
      mem_block[1] = (char)((big_val >> 8) & 0xff);
      mem_block[2] = (char)((big_val >> 16) & 0xff);
      mem_block[3] = (char)((big_val >> 24) & 0xff);
    
      /* Read a 32 bit integer from multiple mem_block cells */
      recover = mem_block[0];
      recover |= mem_block[1] << 8;
      recover |= mem_block[2] << 16;
      recover |= mem_block[3] << 24;
    
      printf("%d == %d\n", big_val, recover);
    
      return 0;
    }
    *Cela*

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Thumbs up

    Originally posted by Cela
    >>To write a 64 bit value to memory the way I was trying seems not possible when programming in C, right or wrong?
    Wrong, it just takes a little bit of work to get it right. This works just fine, I used a 32 bit variable to make things a little shorter :-)
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int recover = 0;
      int big_val = 12345;
      char mem_block[5] = {0};
    
      /* Write a 32 bit integer to multiple mem_block cells */
      mem_block[0] = (char)(big_val & 0xff);
      mem_block[1] = (char)((big_val >> 8) & 0xff);
      mem_block[2] = (char)((big_val >> 16) & 0xff);
      mem_block[3] = (char)((big_val >> 24) & 0xff);
    
      /* Read a 32 bit integer from multiple mem_block cells */
      recover = mem_block[0];
      recover |= mem_block[1] << 8;
      recover |= mem_block[2] << 16;
      recover |= mem_block[3] << 24;
    
      printf("%d == %d\n", big_val, recover);
    
      return 0;
    }
    Nice work Maestro :-)

    Thanks...

    End Of File... ;-)

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Talking

    Originally posted by electrolove
    Nice work Maestro :-)

    Thanks...

    End Of File... ;-)
    Well, not really end of file...

    I just learned something new called 'casting'.

    * (data type *) myPtr = variable;

    This forces the pointer to be a 64 bit pointer only on this line, and a 8 bit pointer everywhere else, as simple as that :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and dynamic memory
    By tkansara in forum C Programming
    Replies: 1
    Last Post: 06-16-2009, 09:22 AM
  2. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  3. Replies: 2
    Last Post: 11-28-2003, 11:50 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. pointers and memory
    By itld in forum C++ Programming
    Replies: 4
    Last Post: 02-06-2002, 11:34 PM