Thread: Determining size of array returning strange values?

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    sizeof(input)
    ..here..only returns the size of the pointer, which is always same.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  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,336
    No, unless you massage the data in some way (e.g., C-strings always ending in \0).

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    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,949
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,412
    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.
    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

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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
    28,412
    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.
    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

  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, 08: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, 02: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