Thread: problem with sizeof operator

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    problem with sizeof operator

    i have initialized an array using calloc.
    i can only pass the pointer to a function.
    in the function i need to find the length of the array. how do i do it??

    wen i use the sizeof() operator it always gives me the value 4 ?? i dont understnd.

    #include <stdio.h>
    #include <stdlib.h>

    int main() {
    double *array;

    array = (double *)malloc(10*sizeof(double));
    printf("size = %d",sizeof(array));
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by slash_axl
    in the function i need to find the length of the array. how do i do it??
    Pass the length of the dynamic array as an argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And don't forget to free!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    is there any other method other than passing the length as an argument??

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not in C, no. You need to keep track of the length.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by slash_axl
    is there any other method other than passing the length as an argument??
    You could designate some special value to terminate the array, like how the null character is used for a string. However, there is then the danger of out of bounds access should you forget to actually place that value at the appropriate point in the array. Of course, this cannot be used if there is no suitable value, and then it potentially wastes a little space.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    thanx a lot!!!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And don't forget code tags either.
    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.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, no need to cast malloc, if you are using a C compiler.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        double *array;
    
        array = (double *) malloc(10 * sizeof(double));
        printf("size = %d", sizeof(array));
        return 0;
    }
    Something to keep in mind as well is that you should get into the habit of checking the returned value from malloc(). If it returns NULL, and you dereference that, you run into trouble.

    As for what claudiu wrote [above] about casting malloc(), here are some links from FAQs:


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Operator Overload problem
    By Kasatka in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2004, 09:29 PM
  3. overload insert operator problem
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 04:34 PM
  4. problem with new operator
    By codefx in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2002, 05:04 PM
  5. A newby question on the sizeof operator
    By Evo in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2002, 03:17 AM