Dynamically allocate an array of two bytes?

This is a discussion on Dynamically allocate an array of two bytes? within the C Programming forums, part of the General Programming Boards category; I would like to be able to do this in order to do some manipulation with the two bytes eventually...can ...

  1. #1
    dre
    dre is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    20

    Dynamically allocate an array of two bytes?

    I would like to be able to do this in order to do some manipulation with the two bytes eventually...can anybody show me how to do this?

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Code:
    char *ptr
    
    ptr = malloc(2 * sizeof(char));
    This?
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by GL.Sam View Post
    Code:
    char *ptr
    
    ptr = malloc(2 * sizeof(char));
    This?
    no this

    Code:
    char *lol=malloc(2);
    free(lol);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    hmm... but if you know that the array will consist of exactly two bytes, why bother with dynamic memory allocation?
    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

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by laserlight View Post
    hmm... but if you know that the array will consist of exactly two bytes, why bother with dynamic memory allocation?
    Maybe the bytes are big? :-), as in CHAR_BIT is like 50,000.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by zacs7 View Post
    Maybe the bytes are big? :-), as in CHAR_BIT is like 50,000.
    ROFL!

    Laserlight has the right idea. Depending on what you're doing you can perhaps just declare a local array or something like that. Even if it has to exist longer than the lifetime of the function call, you still have return values and out-parameters as an option.

    Using dynamic memory allocation for something so small is like going to the shop to buy exactly two grains of salt.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Quote Originally Posted by imalc View Post
    using dynamic memory allocation for something so small is like going to the shop to buy exactly two grains of salt.
    lol

  8. #8
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    From now on I'm going to dynamically allocate my bools

  9. #9
    dre
    dre is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    lol you guys are bullies...i know its easy to say it now, but when i first read that i had to do that i thought to myself the same thing, like whats the point of dynamically allocating a set amount of bytes. but then i thought my prof probably knows more about C than I do so I just went with it.

    but thanks guys, appreciate the help.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    It's just that 2 bytes is such a tiny amount... easily representable within a basic data type. Perhaps the professor simply wanted the students to be clear on the different uses of memory. Once the concepts are clear, then one can decide whether there is efficiency of one type over another.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  2. Run-time error with dynamically allocated 3-D array
    By OceanDesigner in forum C Programming
    Replies: 2
    Last Post: 10-21-2005, 02:29 PM
  3. Unable to allocate array size at runtime
    By CookieMonster in forum C Programming
    Replies: 16
    Last Post: 04-03-2004, 05:48 PM
  4. How to init an array dynamically on the heap?
    By karantsch in forum C Programming
    Replies: 6
    Last Post: 01-25-2003, 12:07 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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