Thread: Another Question...memory allocation

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    6

    Another Question...memory allocation

    1) Which of the following would correctly allocate enough memory to store N integers? The integer pointer in this question is p and N is an integer with a value greater than 0 but not so large taht the amount of memory requested exceeds the amount available.

    A. p = (int*)malloc(sizeof(int) + N);
    B. p = (int*)malloc(sizeof(int * N));
    C. p = (int*)malloc(sizeof(int) * N);

    I haven't really worked with memory allocation...but i'm going to make a guess here. I don' think its A because that would just be adding the number of integers to the size of int.
    B would actually change the value of the number and C multiplies the size of the int times the number of ints...so i guess C makes the most sense? I don't really have a clue though...
    Thanks in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There isn't the option I like: None of the above. You should not cast malloc() in standard C, so all of them having casts mean that none of the alternatives are correct.

    C is the "nearest to correct" answer.
    B will not compile at all, and A will not be correct [except for the special case of 16-bit integers and N = 2 - as that will be 2 + 2 = 2 * 2 = 4 bytes].

    --
    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.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You could also leave off the data type as the argument to the sizeof operator.

    That could result in:

    Code:
    p = (int *)malloc(sizeof(*p) * N);
    And of course, the whole "not casting malloc()" thing.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > You should not cast malloc() in standard C, so all of them having casts mean that none of the alternatives are correct.

    The cast may be bad practice, but it's not actually wrong (assuming it's the right cast). But not prototyping malloc() properly with #include <stdlib.h>, which the OP didn't mention, would be.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    thanks for the help :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Memory allocation
    By p3rry in forum C Programming
    Replies: 25
    Last Post: 12-23-2008, 08:30 AM
  2. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  3. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM
  4. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM