Thread: (float) or (float*) wrt sizeOf()

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

    (float) or (float*) wrt sizeOf()

    I keep seeing these calls when allocating memory and its confusing the **** out of me. Please can someone clarify for me.

    Here are the two calls that are confusing me.

    malloc(arraySizeX*sizeof(float*))
    malloc(arraySizeX*sizeof(float))

    Whats the difference between
    sizeof(float*)
    and
    sizeof(float)

    More precisely what affect does the * symbol have on the parameter float?

    Not sure if Its a typo I keep seeing pop up when I see this function used (mostly in malloc() calls) or if these two calls have different results?
    Thank you.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    More precisely what affect does the * symbol have on the parameter float?
    The * symbol is representative of the pointer in C.
    A pointer is basically a variable which holds the address of another variable in the program.\

    eg:

    Code:
    float x = 5.0;
    float* px = &x;// & is the address of operator which returns the address of x
    printf("The variable x = %f lives at the adress %p",*px, px);//*px dereferences the pointer and returns the value the pointer is pointing to
    a pointer is usually 4 bytes in memory.

    in your example
    Code:
    malloc(arraySizeX*sizeof(float*))
    is usually used when allocating memory to a pointer to a pointer or perhaps a 2D array.

    and

    Code:
    malloc(arraySizeX*sizeof(float)
    is used when allocating memory for a pointer, or in your case and array.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    30
    Awesome. That cleared it up. Thank you for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM