sizeof always returns 4.

This is a discussion on sizeof always returns 4. within the C Programming forums, part of the General Programming Boards category; I'm trying to get to grips with malloc however the sizeof function - no matter what I do, always returns ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Unhappy sizeof always returns 4.

    I'm trying to get to grips with malloc however the sizeof function - no matter what I do, always returns 4. Can someone explain to me what is up?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        int *ptr = malloc(sizeof(int) * 10);
        int len = sizeof(ptr);
        printf("Len: %i\n", len);
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    You are getting the size of the pointer, most likely.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why don't you check out a char, a char array with 20 elements, a double, a long int, etc. ?

    Two quantities being the same, does not an "always" make.

    And as noted above, you're only showing the size of the pointer.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Ploe View Post
    I'm trying to get to grips with malloc however the sizeof function - no matter what I do, always returns 4. Can someone explain to me what is up?
    sizeof is an operator, not a function. Secondly, C does not automatically maintain any size information at runtime for you. sizeof is done at compile time, it's not going to de-reference your pointer and work out how many bytes you asked to be allocated.

    All that it has told you is, sizeof(int *) is 4 for your machine.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    So at runtime I'd need some form of variable as a reference for the size of the array? I didn't realise that sizeof was an operator, however that would explain why Code::Blocks was all neat and changed its colour.

    Thanks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by Ploe
    So at runtime I'd need some form of variable as a reference for the size of the array?
    Yes. Alternatively, you designate some special value to serve as the "last" value, e.g., how the null character in a string is used. Passing the size tends to be less error prone though.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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