allocating some memory

This is a discussion on allocating some memory within the C Programming forums, part of the General Programming Boards category; Question #1: If you write the following code to allocate some memory: void *mem (256); //Allocate 256 bytes of memory ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    11

    Question allocating some memory

    Question #1: If you write the following code to allocate some memory:
    void *mem (256); //Allocate 256 bytes of memory
    How would you access the 42nd byte in this memory??

    Question #2: using the same memory as above, how would you access bit number 356 (counted from the beginning of memory)? Use more than one line of code, and comment each line so we know what you intended it to do.

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    1. You cannot make a void array because void is not something that exists, you can however make a void pointer that points to allocated memory.
    Code:
    void* ptr = malloc(256); /Allocates 256 bytes
    if (ptr == NULL)
      return ERROR_OHNOANERROR;
    (char*) ptr[42];
    //or, if you want to be "safe" and you have stdint.h (C99 header)
    #include <stdint.h>
    (int8_t*) ptr[42];
    2.
    Code:
    (char*) ptr[356/8] & 1 << 356%8;
    [edit] Oops, forgot this (in green).
    Last edited by OnionKnight; 03-09-2006 at 09:09 PM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I might have some issues with this.
    Code:
    //or, if you want to be "safe" and you have stdint.h (C99 header)
    #include <stdint.h>
    (int8_t*) ptr[42];
    I know what you're saying, but it's kinda saying to ignore C's definition of a byte and the number of bits therein.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Quote Originally Posted by Dave_Sinkula
    I might have some issues with this.
    Code:
    //or, if you want to be "safe" and you have stdint.h (C99 header)
    #include <stdint.h>
    (int8_t*) ptr[42];
    I know what you're saying, but it's kinda saying to ignore C's definition of a byte and the number of bits therein.
    I don't follow.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If on a particular platform CHAR_BIT == 16, dividing by 8 does not give you the correct [16-bit] byte, for example.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    What sort of sick bastard would make a byte 16-bit :O
    Seems like you can't trust any types in C :/

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by OnionKnight
    What sort of sick bastard would make a byte 16-bit :O
    DSPs have been doing it for a while.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Well, anyway, a fix for answer #2:
    Code:
    #include <limits.h>
    (char*) ptr[356/CHAR_BIT] & 1 << 356%CHAR_BIT;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. allocating memory in constructor
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 07:45 AM
  5. Replies: 5
    Last Post: 11-24-2002, 10:33 PM

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