Thread: sizeof returns unexpected result

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    59

    sizeof returns unexpected result

    Considering the following code:

    Code:
    int fnParser(char *buffer,float* pOutput){
        int c = 0;
        char * pch;
        pch = strtok (buffer,",");
        pOutput[c] = atof(pch);
        while (pch != NULL)
        {
            c++;
            pch = strtok(NULL, ",");
            if(pch != NULL) pOutput[c] = atof(pch);
        }
        return sizeof(pOutput);
    }
    
    char buffer[] = "200,300,123456,1.00000,.707,.4,'.404";float *fParseData;
    
    int iSize=fnParser(buffer,fParseData);
    printf("Buffer size = %i\n",iSize);
    fnParser converts 'buffer' into a 7 member array 'pOutput' so why does sizeof(pOutput) aka iSize keep coming up 4 instead of 7?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why would it come up with 7? Where is 7 associated with sizeof(float *)?

    It's returning the size of a float pointer, which on your system is 4. If you're trying to find out how much space you allocated for it, sizeof won't work.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    I had expected buffer to be broken down into 7 comma separated values, one value placed into each cell of of pOutput. When I execute the following code:

    Code:
      float *fParseData;            int iSize=fnParser(buffer,fParseData);
                printf("RECEIVED TARGET:\n");
                printf("  x: %f\n",fParseData[0]);
                printf("  y: %f\n",fParseData[1]);
                printf("  z: %f\n",fParseData[2]);
                printf("  q1: %f\n",fParseData[3]);
                printf("  q2: %f\n",fParseData[4]);
                printf("  q3: %f\n",fParseData[5]);
                printf("  q4: %f\n",fParseData[6]);
                printf("Buffer size = %i\n",iSize);
                printf("\n");
    Each cell contains the 'expected' value so I had assumed sizeof(fParseData) would be 7.

    But I think I understand: sizeof(fParseData) would be 7 if:
    Code:
    float fParseData[6];
    which is not what I did.
    Instead I can:

    Code:
    return (c);
    But is there a better way to determine how many valid (not NULL) members are being pointed to by fParseData?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    If you're only using stack arrays, you can do
    Code:
    float fParseData[6];
    printf("%lu\n", sizeof(fParseData));
    
    /* meaning, 6 times 4 (sizeof(float)), so it correctly returns 24 */
    /* if you want elements in the array, do this: */
    
    printf("%lu\n", sizeof(fParseData) / sizeof(float));
    If you're doing malloc()/heap arrays, and they are properly null terminated, you can write your own function to increment a copy of the pointer until the pointee is zero.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    But is there a better way to determine how many valid (not NULL) members are being pointed to by fParseData?
    Ummm...count them as you put them in? Or am I being too logical?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    > Each cell contains the 'expected' value so I had assumed sizeof(fParseData) would be 7.
    sizeof operates at compile time. Since fParseData is only known to be a float * at compile time, all the compiler can do is tell you the size of a float * (4 bytes on your system).

    >
    But I think I understand: sizeof(fParseData) would be 7 if:
    Code:
    float fParseData[6];
    No, it would not be 7. There are 6 elements, so a more reasonable guess would be 6. But sizeof returns the number of bytes used for storage, not the number of elements. Thus, the above declaration has a size of 6 * sizeof(float). If a float is 4 bytes, that's 24 bytes for fParseData.

    >
    But is there a better way to determine how many valid (not NULL) members are being pointed to by fParseData?
    If you don't want to track the length in a separate variable, another way is to use a sentinel value. This is how strings work in C, there is a null character ('\0') that marks the end of the string. Just allocate one more spot than you need. Depending on what values you expect to be storing in fParseData, you may be able to use -1 or FLOAT_MIN to mark the end of the list. It's really up to you to pick an appropriate sentinel value for your application. Just remember, floating point comparisons are not exact, so you need to check for something like
    Code:
    if (fParseData[i] - sentinel_value < 0.0001)  // You can change 0.0001 to any reasonable tolerance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with clock(); getting unexpected result
    By matrixx333 in forum C Programming
    Replies: 16
    Last Post: 06-03-2010, 11:28 AM
  2. unexpected result
    By abotaha in forum C++ Programming
    Replies: 14
    Last Post: 11-17-2009, 08:32 PM
  3. No error, but the result is unexpected
    By Nimbuz in forum C Programming
    Replies: 10
    Last Post: 07-24-2009, 03:10 PM
  4. an unexpected result.
    By System_159 in forum C Programming
    Replies: 7
    Last Post: 01-22-2008, 07:05 AM
  5. unexpected result, no display of input string
    By xephyr in forum C Programming
    Replies: 11
    Last Post: 08-04-2004, 07:22 PM