Thread: arrays dimensioning: malloc and sizeof

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    3

    arrays dimensioning: malloc and sizeof

    I'm not sure whether I'm doing this wrong or sizeof can't read arrays dimensioned with the memory allocation functions. Here's what I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {	char *NEW_ARRAY;
    	char NEW_ARRAY2[5];
    
    	NEW_ARRAY = malloc(5 * sizeof(char));
    
    	if(NEW_ARRAY != NULL)
    	{	printf("&#37;d\n", sizeof(NEW_ARRAY));
    		printf("%d\n", sizeof(NEW_ARRAY2));
    	}
    
    	free(NEW_ARRAY);
    }
    NEW_ARRAY returns 4 (no matter what I put in as the MALLOC size value)
    NEW_ARRAY2 returns the correct number of 5 positions within the array.

    Any ideas? The solution I'm using this in pulls characters from a static array. The amount of characters it pulls varies so I need to create a dynamic container character array.
    Last edited by c programmer; 03-27-2007 at 03:38 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is NEW_ARRAY? Now look at what sizeof gives you.
    What is NEW_ARRAY2? Now look at what sizeof gives you.

    The sizeof operator gives you the size of what you pass it. You pass it a pointer, it tells you how big that pointer is.


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

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    3
    Thank you for your quick response, Quzah. Going by what you told me, I did some research into pointers and they often times return an integer size of 4, so that answers my question about where the 4 comes from. Why is it 4, though? Since its type is char, shouldn't my character pointer return 1?

    How would I get the size of my allocated array when using malloc like when I use sizeof on a static array (see NEW_ARRAY2).

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by c programmer View Post
    Thank you for your quick response, Quzah. Going by what you told me, I did some research into pointers and they often times return an integer size of 4, so that answers my question about where the 4 comes from. Why is it 4, though? Since its type is char, shouldn't my character pointer return 1?

    How would I get the size of my allocated array when using malloc like when I use sizeof on a static array (see NEW_ARRAY2).
    A pointer contains a memory address, which is usually a 4-byte number (an integer). The pointer size is independent of the size of the pointee (what is being pointed).

    There is no way to get the size of a dynamically allocated array apart from saving the size in an additional variable.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    3
    Ah, OK. I understand now. Thank you both for your help. I appreciate it.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    ints are 2-bytes (2^16-1), doubles are 4 bytes(2^32-1), chars are one byte(2^8-1). All though ints are machine based and the OS can have them as for the current general standard is 2 bytes on a 32 bit machine.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by KoG Metalgod View Post
    ints are 2-bytes (2^16-1), doubles are 4 bytes(2^32-1), chars are one byte(2^8-1). All though ints are machine based and the OS can have them as for the current general standard is 2 bytes on a 32 bit machine.
    Haven't seen the 2 byte ints for about 8 years... What time are you living in?

    doubles are 8 bytes mostly, while floats are 4bytes length...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    a 32 bit machine max is 2^32 -1, thats 32/8 (bytes/bits) which is 4 bytes max able to compuate on 1 variable on a 32 bit machine.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    The required ranges for signed and unsigned int are identical to those for signed and unsigned short. On compilers for 8 and 16 bit processors (including Intel x86 processors executing in 16 bit mode, such as under MS-DOS), an int is usually 16 bits and has exactly the same representation as a short. On compilers for 32 bit and larger processors (including Intel x86 processors executing in 32 bit mode, such as Win32 or Linux) an int is usually 32 bits long and has exactly the same representation as a long.

    A signed int can hold all the values between INT_MIN and INT_MAX inclusive. INT_MIN is required to be -32767 or less, INT_MAX must be at least 32767. Again, many 2's complement implementations will define INT_MIN to be -32768 but this is not required.

    An unsigned int can hold all the values between 0 and UINT_MAX inclusive. UINT_MAX must be at least 65535. The int types must contain at least 16 bits to hold the required range of values.

    A signed long can hold all the values between LONG_MIN and LONG_MAX inclusive. LONG_MIN is required to be -2147483647 or less, LONG_MAX must be at least 2147483647. Again, many 2's complement implementations will define LONG_MIN to be -2147483648 but this is not required.

    An unsigned long can hold all the values between 0 and ULONG_MAX inclusive. ULONG_MAX must be at least 4294967295. The long types must contain at
    least 32 bits to hold the required range of values.

    and the proof is here:

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    volatile int char_min = CHAR_MIN;
    
    int main(void)
    {
        printf("\n\n\n\n\n        Character Types\n");
        printf("Number of bits in a character: &#37;d\n",
            CHAR_BIT);
        printf("Size of character types is %d byte\n",
            (int)sizeof(char));
        printf("Signed char min: %d max: %d\n",
            SCHAR_MIN, SCHAR_MAX);
        printf("Unsigned char min: 0 max: %u\n",
            (unsigned int)UCHAR_MAX);
    
        printf("Default char is ");
        if (char_min < 0)
            printf("signed\n");
        else if (char_min == 0)
            printf("unsigned\n");
        else
            printf("non-standard\n");
    
        printf("\n        Short Int Types\n");
        printf("Size of short int types is %d bytes\n",
            (int)sizeof(short));
        printf("Signed short min: %d max: %d\n",
            SHRT_MIN, SHRT_MAX);
        printf("Unsigned short min: 0 max: %u\n",
            (unsigned int)USHRT_MAX);
    
        printf("\n           Int Types\n");
        printf("Size of int types is %d bytes\n",
            (int)sizeof(int));
        printf("Signed int min: %d max: %d\n",
            INT_MIN, INT_MAX);
        printf("Unsigned int min: 0 max: %u\n",
            (unsigned int)UINT_MAX);
    
        printf("\n        Long Int Types\n");
        printf("Size of long int types is %d bytes\n",
            (int)sizeof(long));
        printf("Signed long min: %ld max: %ld\n",
            LONG_MIN, LONG_MAX);
        printf("Unsigned long min: 0 max: %lu\n",
            ULONG_MAX);
    
        return 0;
    }
    Last edited by KONI; 03-27-2007 at 07:38 AM.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by KoG Metalgod View Post
    a 32 bit machine max is 2^32 -1, thats 32/8 (bytes/bits) which is 4 bytes max able to compuate on 1 variable on a 32 bit machine.
    Max for what?
    Variable of what type?
    For example for double it is
    DBL_MAX 1.7976931348623158e+308 Maximum value

    And it is a little bit more than 2^32 -1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by KONI View Post
    The pointer size is independent of the size of the pointee (what is being pointed).
    Not always true, unfortunately. On a segmented architecture, a pointer might be "near" (meaning the segment it is based from is implicit) or "far" (the base segment is included in the pointer itself). Back in the days of 16-bit DOS and Windows, this was something you had to deal with on a daily basis. A near pointer was 16 bits in size, and a far pointer was 32 bits.

    DOS may be obsolete (or at least obsolescent) but it's not the only platform out there with a segmented or otherwise non-flat memory architecture. In general there is no guarantee that all pointers are the same size.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc and sizeof question
    By cus in forum C Programming
    Replies: 4
    Last Post: 01-03-2009, 12:23 PM
  2. More malloc questions...
    By kermit in forum C Programming
    Replies: 3
    Last Post: 09-13-2008, 06:25 PM
  3. malloc sizeof struct
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 07:49 AM
  4. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM