Error in malloc

This is a discussion on Error in malloc within the C Programming forums, part of the General Programming Boards category; i started learning c,i know malloc is for dynamic allocation but i dont know how to use with structure,using the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    Error in malloc

    i started learning c,i know malloc is for dynamic allocation but i dont know how to use with structure,using the image1 parameters i have to allocate space in memory
    //it showing error "incompatible types when assigning ------

    Code:
    typedef struct
    {
    height;
    width;
    depth;
    }VALUES;
    VALUES image1,image2;
    
    image2 = (char*)malloc (image1.height,image1.width,image1.depth(sizeof(char));//how to write this code (image 2 is of type structure)

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    You have to give malloc a single number - the total size of everything you want to malloc in bytes.

    So:

    Code:
    image2 = malloc(image1.height * image1.width * image1.depth * sizeof(char))
    Assuming the height, width and depth are all stored in bytes. More likely you want to divide them by 8 because they are more likely to correspond to the number of BITS and there are 8 bits in a byte.

    P.S. I removed your cast of the return value of malloc because people hate that. FAQ's are available telling you why.
    P.P.S. sizeof(char) is by definition 1.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    thanks,but it still says
    incompatible types when assigning to type VALUES from type void*

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Try this
    Code:
    // a collection of whatever it is you want to store
    typedef struct
    {
        int height;
        int width;
        int depth;
    }VALUES;
    
    VALUES *image1; // a pointer to some VALUES
    
    image1 = malloc( howManyValuesYouWantToStore * sizeof(*image1) );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    it says height in something not a structure or unoin

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    thanks,i fix it
    no error

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    oooh again it showing errors
    i need to assign the image1 size to image2
    but it says
    height in something not a structure or union
    width in something not a structure or union
    -------------

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    It would really help us if you show us the relevant code.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc() pointer error
    By in_ship in forum C Programming
    Replies: 4
    Last Post: 08-11-2012, 02:55 AM
  2. Segmentation error... malloc. Please help
    By Rollo in forum C Programming
    Replies: 8
    Last Post: 12-07-2010, 08:34 AM
  3. malloc, iterations, bus error and more
    By z0diark in forum C Programming
    Replies: 8
    Last Post: 12-01-2009, 03:51 AM
  4. Weird Malloc Error
    By someprogr in forum C Programming
    Replies: 5
    Last Post: 12-28-2007, 04:00 AM

Tags for this Thread


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