Why does this not work when passed through a function?

This is a discussion on Why does this not work when passed through a function? within the C Programming forums, part of the General Programming Boards category; Why is this not working when called in a function? This does NOT work (see next code section for one ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Why does this not work when passed through a function?

    Why is this not working when called in a function?

    This does NOT work (see next code section for one that does work):
    Code:
    int getString(usb_device_handle* h, int index, char* buff)
    {
          return usb_get_string_simple( h, index, buff, sizeof( buff ) );
    }
    
    char* getValue(usb_device_handle* h, usb_device* dev)
    {
          char value[ 256 ];
          getString( h, dev->descriptor.iManufacturer, value );
          return value;
    }
    This DOES work:
    Code:
    char* getValue(usb_device_handle* h, usb_device* dev)
    {
          char value[ 256 ];
          usb_get_string_simple( h, dev->descriptor.iManufacturer, value, sizeof( value ) );
          return value;
    }
    For some reason, with the first one, it only grabs the first three characters of the Manufacturer string. Why?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    You need to pass the size of the array as another parameter.
    sizeof(buff) does NOT do what you want. It just tells you the size of the pointer, not how much space it points to.
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not to mention that the second variant doesn't actually work correctly, as it returns an address to a local variable that is gone (as in destroyed) by the time you actually get back to the function calling it. It may appear like it's actually working, but it is just the same fluke that can get you across a junction against a red light - if you are lucky, there's nothing coming the other way.

    Doing this:
    Code:
    char* getValue(usb_device_handle* h, usb_device* dev)
    {
          char value[ 256 ];
          usb_get_string_simple( h, dev->descriptor.iManufacturer, value, sizeof( value ) );
          return value;
    }
    
    void foo()
    {
       char dummy[256] = { 0 };
       memset(dummy, 0x33, sizeof(dummy)-2);
    }
    
    int main()
    {
    ...
       char *val = getValue(...);
       foo();
       printf("val = %s\n", val);
    ..
    }
    will show how it DOESN'T work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 05:30 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM

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