Like Tree1Likes
  • 1 Post By Elysia

Determining size of array returning strange values?

This is a discussion on Determining size of array returning strange values? within the C++ Programming forums, part of the General Programming Boards category; The following code returns 8 followed by 26, the second number obviously being correct and first being wrong despite having ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    Determining size of array returning strange values?

    The following code returns 8 followed by 26, the second number obviously being correct and first being wrong despite having identical code. Any ideas what's wrong with my getarraysize function?

    Code:
    static char key[26] = {'r','t','c','t','b','f','n','l','a','f','s','t','t','t','s','e','a','n','i','m','(','m','o','c','i','r'};
    
    int getarraysize(char *input) {
    
    	int output;
    	output = sizeof(input) / sizeof(input[0]);
    
    	return output;
    }
    
    int main(int argc, char *argv[]) {
    
    	cout << getarraysize(key) << "\n";
    	cout << (sizeof(key) / sizeof(key[0]));
    	return 0;
    }

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    sizeof(input)
    ..here..only returns the size of the pointer, which is always same.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Nothing is wrong.
    You just can't use sizeof() like that.

    In order to count the elements of an array, the array has to be IN SCOPE.
    Otherwise, all you have is a pointer, and sizeof tells you the size of the pointer.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    I think that's because in the getarraysize function, sizeof(input) will give you the size of a pointer, which is 8, and sizeof(input[0]) is the size of a char, which is 1.

    Quote Originally Posted by edddo View Post
    The following code returns 8 followed by 26, the second number obviously being correct and first being wrong despite having identical code. Any ideas what's wrong with my getarraysize function?

    Code:
    static char key[26] = {'r','t','c','t','b','f','n','l','a','f','s','t','t','t','s','e','a','n','i','m','(','m','o','c','i','r'};
    
    int getarraysize(char *input) {
    
    	int output;
    	output = sizeof(input) / sizeof(input[0]);
    
    	return output;
    }
    
    int main(int argc, char *argv[]) {
    
    	cout << getarraysize(key) << "\n";
    	cout << (sizeof(key) / sizeof(key[0]));
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    OK thanks for that, so is there another way to find the size of an array passed as a function's argument?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No, unless you massage the data in some way (e.g., C-strings always ending in \0).

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Actually, yes, there is. Let me show you an example in a minute or two.

    EDIT:
    Code:
    #include <crtdefs.h>
    #include <iostream>
    
    template<typename T, size_t size>
    size_t array_size(const T (&arr)[size])
    {
    	return size;
    }
    
    int main()
    {
    	int arr[20];
    	std::cout << array_size(arr);
    }
    You can also use std::array and std::vector which has a size() function.
    Last edited by Elysia; 07-27-2011 at 01:36 PM.
    King Mir likes this.
    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.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Isn't this just 'managing the data in' like tabstop said? I am confident I am not as good of a programmer as you guys, but I'm pretty sure that C++ trickery wouldn't solve the OPs problem about the array not being in scope.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    So long as you pass the full array type, such as replacing the T there with the actual type, no size information will be lost and you can always query the size.
    If you pass the array to a function the usual way (T arr[n]), then the above method will not work.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,326
    Quote Originally Posted by AndrewHunter
    Isn't this just 'managing the data in' like tabstop said?
    tabstop's phrase was actually "massage the data in some way". There is no need to tailor the data here, so no.

    Quote Originally Posted by AndrewHunter
    I'm pretty sure that C++ trickery wouldn't solve the OPs problem about the array not being in scope.
    The "trickery" is just a matter of avoiding the conversion to a pointer to the first element. If you pass a pointer to an array, then you need to know the array size, and the array is not converted to a pointer to its first element (unless you dereference the pointer and use the array in an applicable context within the function). From this you can write a function template to get the array size. It turns out that you can simplify by having the array passed by reference instead of passing a pointer to the array.
    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

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Hmm, I see. Thanks Elysia.

    EDIT: Thank you as well Laser for your explanation.
    Last edited by AndrewHunter; 07-27-2011 at 02:11 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    Thanks I've integrated it into my program and it works but I'm having a little trouble understanding why it works. I don't know what the "T" is for. Also this usage of template is not what I'm used to, what's going on with that "size_t size" part?

    You also mentioned replacing the "T" with the desired type (in my case "char") but when I try that I get a syntax error.

    If you could explain a little bit that would really great

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,326
    Quote Originally Posted by edddo
    Thanks I've integrated it into my program and it works but I'm having a little trouble understanding why it works. I don't know what the "T" is for.
    It is as if you overloaded the function for each array type for which you provided an array as an argument to the function, e.g.,
    Code:
    size_t array_size(const int (&arr)[20])
    {
        return 20;
    }
    So you need the T because the element type is part of the array type.

    Quote Originally Posted by edddo
    Also this usage of template is not what I'm used to, what's going on with that "size_t size" part?
    It is just a template parameter.
    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

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    Thanks very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining file size
    By waterborne in forum C Programming
    Replies: 5
    Last Post: 12-15-2009, 07:56 AM
  2. determining size of struct
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-09-2008, 07:10 AM
  3. sizeof: determining the size of char array problems...
    By what3v3r in forum C++ Programming
    Replies: 17
    Last Post: 02-09-2006, 01:40 AM
  4. Determining values on a web page
    By AaA in forum C Programming
    Replies: 1
    Last Post: 06-28-2005, 04:47 AM
  5. determining size
    By gooddevil in forum C Programming
    Replies: 5
    Last Post: 05-22-2004, 11:10 AM

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