Thread: dynamically allocating memory

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    20

    dynamically allocating memory

    ive dynamically allocated an array of two bytes using
    char *ptr1 = (char *)malloc (2*sizeof(char));

    now how can I print the number of ones and zeros in the memory allocated?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Someone was here asking this exact question with the same code and everything earlier. You should search for it.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    20
    that was me, but i was asking how to find the hamming distance and havent been able to figure it out thus far. so im taking a step back and trying to figure out how to just print the 0 and 1s first and work my stuff out from there.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, I mean someone was asking this very question and you should have searched before you asked.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    20
    haha oh jeez, that guy is more than likely in my class. small world. i just assumed you meant me, but thanks for directing me.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And of course, for any given system, if you do this twice in a row, it may give exactly the same result or a completely different result. It is COMPLETELY unpredictable what the memory contains after malloc() - it is "whatever happens to be in that memory", and that could be all ones, all zeros or any of the 65534 other possible combinations. A LIKELY scenario is that if you have not allocated the memory previously, it contains zero. But there is no guarantee - and in particular, Visual Studio (for Windows), when the code is in debug mode will fill the malloc() memory with a pattern (0xCD?) .

    I fail to see the point of such an excercise. It would be a better excercise to produce a 16-bit random number and calculate whatever it is you want to calculate (hamming distance, number of ones and zeros or whatever).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention casting the return of malloc may cause some very bad things™.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  2. Dynamically allocating memory...
    By Junior89 in forum C++ Programming
    Replies: 28
    Last Post: 05-08-2007, 10:17 PM
  3. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM
  4. allocating memory in constructor
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 07:45 AM
  5. Checking if memory has been dynamically allocated
    By Xzyx987X in forum C Programming
    Replies: 28
    Last Post: 03-14-2004, 06:53 PM