Thread: (char) array initialization, size etc.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    (char) array initialization, size etc.

    Code:
    1:
    char parray[10];
    strcpy(parray, "abcdefghij");
    printf("%s, %lu\n", parray, sizeof(parray)/sizeof(char));
    
    2:
    char *marray = malloc(10*sizeof(char));
    strcpy (marray, "abcdefghij");
    printf("%s, %lu\n", marray, sizeof(marray)/sizeof(char));
    1: The size is printed as 10, which is the specified size of the array.
    2: The size is printed as 8, no matter what number I initialize it to.

    Why?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    1: The size is printed as 10, which is the specified size of the array.
    When declare an array, certain bytes are allocated for the array by the compiler and the while block is refered by base array name. When you try to find or sizeof the array, your're bound to get the size of the array. As this is predefined and not at the runtime allocation.

    2: The size is printed as 8, no matter what number I initialize it to.
    Where as in pointers, char *marray is pointer, a pointer something which can point to some part in the memory location. So when you find the sizeof a pointer it always give you the size of pointer variable rather than where its pointing. After all the a pointer variable hold just an address.

    Hence array's and pointer are not equivalent.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    So how can I get the size of an array created by using malloc()?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    You know the size because you passed it to malloc to begin with!

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by cnewbie1 View Post
    So how can I get the size of an array created by using malloc()?
    You know how much memory you allocated when you call malloc. Sounds like you may need to keep track of that value yourself through the lifetime of the program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Logical, of course.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initialization of char array (c style string)
    By manzoor in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2008, 06:29 AM
  2. Replies: 17
    Last Post: 10-31-2007, 08:43 AM
  3. dynamically setting size of 2d char array
    By waxydock in forum C Programming
    Replies: 4
    Last Post: 05-13-2007, 10:58 PM
  4. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  5. char array size question, please help!
    By Ash1981 in forum C Programming
    Replies: 4
    Last Post: 01-29-2006, 02:30 AM