Thread: help with dynamic allocation.

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    help with dynamic allocation.

    Hello,

    I am working on an assignment in which I have to dynamically allocate an array of two bytes and in which the memory should not be initialized at first.

    This is for a univ class that I am taking without the pre req. I know basically nothing of C, have never written anything in C, but I have been reading up on notes everywhere for the last few days. I have a good Java background and I am building on that.

    Regarding the assignment, I have gathered a few ideas but I have some questions.

    I understand I have to use the malloc(2) function to do this. And here are my questions:
    what happens when you call that function exactly? I know it reserves space in the memory BUT does it fill it up with anything? does it direct a pointer to it? How do I access that memory space afterward essentially?

    One of the outputs of the program should be: The number of 'ones' in the memory allocated and the number of 'zeros' in the memory allocated.

    So the question is:is the memory already filled with 1 and 0 after the malloc instruction? If not, how do I fill it up??

    Thanks guys,

    Francesco

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    malloc returns a pointer to the allocated memory, so you would do whatever = malloc(2) and now whatever points to your new memory. It doesn't do anything to the memory, so what was there before is still there now.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Other question: when I "reserve" the two bytes..are they automatically considered as an array or is that an extra step that I have to code?

    Also, if the memory is not initialized, it will have nothing in it..so how am I supposed to print "the number of 'ones' in the memory allocated and the number of 'zeros' in the memory allocated".

    I dont want code here...not trying to have you guys do it..I am missing the concept...

    Francesco

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    int arr[10];
    int *arr = malloc ( 10 * sizeof *arr );
    For most purposes, they can be used interchangeably.
    Notably for your purposes, arr[0] through arr[9] access the consecutive elements of the array.

    You first need to do say
    arr[0] = 0;
    to initialise an element.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    They're just consecutive bytes. Whether you want to treat that as an array or as some kind of structure is entirely up to you.
    Do you understand the task of counting the number of 1 bits?
    Lets say that the data initially held 9, 5.
    that's 00001001, 00000101. There are 4 ones, and there are of course 16-4 = 12 zeros.
    You need an algorithm to count these binary bits. There are some very interesting "Bit Twiddling Hacks" around that do this:
    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"

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Fran View Post

    Also, if the memory is not initialized, it will have nothing in it..

    Francesco
    This is of course not true -- the memory is not initialized, so it will have something in it -- you just don't know what. Could be all zeros. Could be all ones. Could be half and half.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Quote Originally Posted by iMalc View Post
    They're just consecutive bytes. Whether you want to treat that as an array or as some kind of structure is entirely up to you.
    Do you understand the task of counting the number of 1 bits?
    Lets say that the data initially held 9, 5.
    that's 00001001, 00000101. There are 4 ones, and there are of course 16-4 = 12 zeros.
    You need an algorithm to count these binary bits. There are some very interesting "Bit Twiddling Hacks" around that do this:

    Hmm i see your point...what is the preferred way to handle the situation?
    There are more "counting" type of things that I have to do to those bytes so I thought making them into an array where every position was a 1 or a 0 would help keep things easy by using for loops.

    What I wouldnt know how to do is how to fill the array properly to get that result...
    If I DONT put them in an array, how can I access the different values??

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Hye guys, this is what i understand things to be like so far...can someone give me some feedback?

    Code:
    int *arr = malloc (2);
    what that does is direct a pointer to a dynamically allocated area of memory of 2 bytes in size. At this point the memory is not initialized and stil is made up of random binary content.

    Then I wold go throught the memory space using a for loop and pointers:

    Code:
    for (int i=0; i<7; i++){
     if (&arr == 1 )
       counter_ones++;
     else (&arr == 0 )
       counter_zeros++;
    *arr++;
    }
    printf ("There are &#37;d ones, and %d zeros in the bytes", counter_ones, counter_zeros);

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And how does &arr relate to what you want to do? Why do you only want to go through the loop 7 times (there are 16 bits in two bytes)? Do you know what ++ means in the context of pointer arithmetic, and the difference between arr++ and *arr++? Do you know why else (&arr==0) is a syntax error?

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    the reason i am using &arr is beacuse I want to look at the content of the memory slot as opposed to the address..which is what I thought the pointer owould be returning me.

    Good point about the 7 times loop. No discussino there.

    ++ as far as I know means to increase the value of whatever preceeds it by 1 so i thought this was one way to walk throught the memory.

    The difference between arr++ and *arr++ should be according to what I just said that arr++ changes the value of the memor content to arr+1 whereas *arr++ should move the pointer to the next element of the memory....

    How off am I?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Fran View Post
    the reason i am using &arr is beacuse I want to look at the content of the memory slot as opposed to the address..which is what I thought the pointer owould be returning me.

    Good point about the 7 times loop. No discussino there.

    ++ as far as I know means to increase the value of whatever preceeds it by 1 so i thought this was one way to walk throught the memory.

    The difference between arr++ and *arr++ should be according to what I just said that arr++ changes the value of the memor content to arr+1 whereas *arr++ should move the pointer to the next element of the memory....

    How off am I?
    A lot. "&" means "address of", so since arr is a pointer, &arr is the address of the pointer. If you want "stuff pointed to by", you need to use "*", which means "stuff pointed to by".

    Since arr is a pointer to int, *arr is an int, not a bit. You'll need to figure out how to access individual bits of an int separately. (A for-loop is involved here, but mostly the bit-manipulating things like & and <<.)

    And for the same reason, arr++ moves forward by one int, not one bit.

    Another interesting thing to deal with: you should check your system, but int may be bigger than two bytes. You can do something like
    Code:
    printf("An integer is %u bytes.\n", sizeof(int));
    to check -- you'll probably get four. Since you're dealing with bytes, and you seem to want an array of two bytes, you'll need to use a data type that's one byte long (for hysterical reasons, that's called "unsigned char"). (All the stuff about figuring out the bits in a byte above still apply, though.)

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Fran View Post
    This is for a univ class that I am taking without the pre req. I know basically nothing of C, have never written anything in C, but I have been reading up on notes everywhere for the last few days. I have a good Java background and I am building on that.
    The school let you take the course without taking the pre-requisite first? Why not make things easier on yourself and take the pre-requisite?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Difference between straight and dynamic allocation?
    By darsunt in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2008, 05:47 PM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM