Thread: Malloc function problem

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    Malloc function problem

    Hi Guys,

    I have been using the malloc function in order to make use of a memory block.. The code is as shown below:

    Code:
    int * corner_image; 
    corner_image = (int*) malloc (480*640);
    When I try to access location 78282 i.e. corner_image[78282]=60; the compiler returns an error.

    Now I only need to store 8 bit images, perhaps it is because type 'int' is 4 bytes wide? What type should I choose so that it becomes 1 byte wide only?

    Thanks a lot. best regards,
    bouvett

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. Don't cast malloc.
    2. What compiler are you using? If you are using Turbo C, you can't have numbers as big as what you want.
    3. You aren't checking to see if your malloc fails, at least not in this example.
    4. If you want to store one byte at each array index, why aren't you using a char / unsigned char instead of an int?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    I changed the type to short int, the range doubled yet I cannot go up to (640*480) -1... any help is appreciated

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bouvett
    When I try to access location 78282 i.e. corner_image[78282]=60; the compiler returns an error.
    You probably got a run time error instead of a compile error. Anyway, the point is that 480*640 is the number of bytes requested, so the number of elements is (480*640) / sizeof(*corner_image).

    Quote Originally Posted by bouvett
    perhaps it is because type 'int' is 4 bytes wide?
    If sizeof(int) is 4, then you only allocated space for 76800 elements, hence an index of 78282 is out of bounds.

    Quote Originally Posted by bouvett
    Now I only need to store 8 bit images (...) What type should I choose so that it becomes 1 byte wide only?
    char, perhaps unsigned char. CHAR_BIT is guaranteed to be at least 8, so this works for your purposes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    oh sorry quzah i didn't see your post. changed to char and it worked. thanks

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    You probably got a run time error instead of a compile error. Anyway, the point is that 480*640 is the number of bytes requested, so the number of elements is (480*640) / sizeof(*corner_image).

    If sizeof(int) is 4, then you only allocated space for 76800 elements, hence an index of 78282 is out of bounds.

    char, perhaps unsigned char. CHAR_BIT is guaranteed to be at least 8, so this works for your purposes.
    Since 480 * 640 is too big, you're going to need to divide by the pointer size first. Do something like:
    Code:
    size_t  howmany = 480 * (640 / sizeof(*corner_image));

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc in function
    By mugen in forum C Programming
    Replies: 13
    Last Post: 03-14-2010, 11:27 AM
  2. malloc function
    By roaan in forum C Programming
    Replies: 9
    Last Post: 08-14-2009, 04:48 AM
  3. using malloc in a function
    By cuizy in forum C Programming
    Replies: 10
    Last Post: 08-13-2009, 01:56 PM
  4. Help with Malloc() function
    By xp5 in forum C Programming
    Replies: 12
    Last Post: 09-19-2007, 03:17 PM
  5. about malloc function
    By enes in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 09:33 AM