#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Warsaw
    Posts
    46
    Rep Power
    34

    Size of array in function


    Code:
    #include "stdafx.h"
    #include <iostream>
    
    
    using namespace std;
    
    
    void printSize(int []);
    
    
    
    const int N=30;
    
    int main()
    {
    	int a[N];
    	
    
    	cout<<sizeof(a);
    
    	printSize(a);
    
    	
    
    
    	cout<<endl;
    	system("PAUSE");	
    	return 0;
    }
    
    void printSize(int matrix[])
    {
    	cout<<sizeof(matrix);
    }
    Can somebody explain why the output is "120 4"?!I was sure that I did exactly the same thing with and without function...
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    In the function printsize() matrix is 'promoted' to a pointer, which on a 32 bit machine will translate to a sizeof = 4.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Warsaw
    Posts
    46
    Rep Power
    34
    Originally Posted by mitakeet
    In the function printsize() matrix is 'promoted' to a pointer, which on a 32 bit machine will translate to a sizeof = 4.
    Are you sure :confused:

    this
    Code:
    void printSize(int matrix[])
    {
    	cout<<sizeof(&matrix);
    }
    doesn't change anything in the output :flame:
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    There are tutorials on pointers, you should read some. An array is a special case of a pointer, but only within the scope it was declared. Outside that scope it is treated as an ordinary pointer. If this is greek to you, you need to study some more.

    Comments on this post

    • kutacz agrees

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0
    We can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    313
    Rep Power
    0
    In C, declarations like :

    Code:
    void test ( int a[] );
    
    void test ( int *a );
    are equivalent.

    Inside both "tests" one can use brackets or stars:

    Code:
    a[3] = 55;
    *(a+3) = 55;
    However, I'm confused by declarations with specific length, like:
    Code:
    void test( int a[9] );
    because inside "test", the sizeof(a) is also 4 (on 32 bit systems).

    I understand, that compiler may use this 9 for compatibility checking.

    But how to use this 9 inside "test" function ?
    For example, how to print(f) it ?

    :confused:
    Last edited by leszek31417; February 1st, 2012 at 06:29 AM.
  12. #7
  13. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    I think this size element is a fairly recent addition to the standard (of course, 'recent' might be from the '90s when I last paid much attention to the standard). I believe its only purpose is to give the compiler writer an opportunity to do static bounds checking on the array and given the challenges of doing so (I know that much of the code I write is all dynamic, so there is essentially zero value in static bounds checking to me) I suspect most compilers (even 20 years later) probably ignore the value anyway.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  14. #8
  15. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,980
    Rep Power
    510
    Originally Posted by leszek31417
    However, I'm confused by declarations with specific length, like:
    Code:
    void test( int a[9] );
    But how to use this 9 inside "test" function ?
    For example, how to print(f) it ?

    :confused:
    Following program prints "2\n" on my system, and there are no compiler warnings. So I cannot get the 9 and the compiler ignores the value, confirming the answers to the original question.

    Code:
    #if 0
    -*- mode: compilation; default-directory: "/tmp/" -*-
    Compilation started at Wed Feb  1 09:23:25
    
    a=./c && make CFLAGS=-Wall $a && $a
    cc -Wall   -c -o c.o c.c
    cc   c.o   -o c
    2
    
    Compilation finished at Wed Feb  1 09:23:26
    #endif
    
    #include<stdio.h>
    #define DIM(A) ((sizeof(A))/(sizeof(*A)))
    void f(int v[9]){printf("%lu\n",DIM(v));}
    int main(){int a[4];f(a);return 0;}
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    12
    Rep Power
    0
    Just read & use this:
    http://heifner.blogspot.com/2008/04/c-array-size-determination.html
    http://heifner.blogspot.com/2008/04/c-array-size-determination-part-2.html

IMN logo majestic logo threadwatch logo seochat tools logo