Thread: I need help with Malloc()

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

    Cool I need help with Malloc()

    I am new to C programming and need a little help. I want to write and read 64 and 8 bit values to/from memory. I can not use a structure so I think the best idea is to use Malloc() to get some memory.

    How do I use Malloc() to get a pointer to a 1024 bytes memory area?

    How do I write 64 and 8 bit values to the area and increase the pointer after each value I write?

    How do I read 64 and 8 bit values from the area and increase the pointer after each value I read?

    As simple as that :-)

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Try the C Board insteed.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: I need help with Malloc()

    >>How do I use Malloc() to get a pointer to a 1024 bytes memory area?
    Do you mean:
    p = malloc(1024);

    >>How do I write 64 and 8 bit values to the area and increase the pointer after each value I write?
    To write to where the pointer is pointing:
    *p = data;
    To increment the pointer:
    p++;
    or
    p += somevalue;

    >>How do I read 64 and 8 bit values from the area and increase the pointer after each value I read?
    data = *p;

    >>As simple as that
    Yeah... right. My answer is a bit generic, if you still need help post some code and someone will guide you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    Re: Re: I need help with Malloc()

    Hi!

    And thanks for your answer, everything works :-)

    Originally posted by Hammer
    >>How do I use Malloc() to get a pointer to a 1024 bytes memory area?
    Do you mean:
    p = malloc(1024);

    >>How do I write 64 and 8 bit values to the area and increase the pointer after each value I write?
    To write to where the pointer is pointing:
    *p = data;
    To increment the pointer:
    p++;
    or
    p += somevalue;

    >>How do I read 64 and 8 bit values from the area and increase the pointer after each value I read?
    data = *p;

    >>As simple as that
    Yeah... right. My answer is a bit generic, if you still need help post some code and someone will guide you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM