Thread: Problems with unsigned char and mem management.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Unhappy Problems with unsigned char and mem management.

    I'm having some problems to deal with this stuff. I call a function of a library that returns an unsigned char *. Now what I need is to create a new unsigned char *, allocate the space with the proper size and then put all the data that the function in the lib returns me into the unsigned char * I just created. The first problem is that I don't actually know the size of the unsigned char * that the function will return me, how can I know that? Once I know that how can I copy all the data from the function to the unsigned char * I just created?

    Note: I can just do unsigned char *mychar = libfunction(libobject); because libobject will be destroyed after it leaves this function and I need the value returned by libfunction for another stuff in the app(that means that I will get rid of the memory I just allocated in another place, don't worry about mem leaks ).

    I hope any of you can help me. Thanks in advance.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    new?

    Code:
    #include <cstring>
    
    char *s = "hippo";
    char *p = new char[strlen(s)+1];
    cout << s << endl;
    cout << p << endl;
    
    delete [] p;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    This is an unsigned char *, it isn't a char * so I can't use strlen. Also this isn't text data, its binary data stored in a "BYTE *" which is defined as unsigned char in win.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well then, write your own strlen:
    Code:
    char *p = new BYTE[custom_strlen(s)+1];
    
    int custom_strlen(BYTE *s) {
        int x = 0;
    
        while(*s++) x ++;
    
        return x;
    }
    Although strlen() actually returns a size_t.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    How do you know how long the array returned by the libfunction is? Does it have a terminating null? Is it always a set size? There has to be a way to know. Look at the documentation of the lib if you aren't sure.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The first problem is that I don't actually know the size of the unsigned char * that the function will return me,
    Are you absolutely sure of this?
    Because if it doesn't, there is nothing you can do since there can be no magic value which can be used to determine the end of the buffer (like '\0' is used by strlen).
    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.

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by dwks
    Well then, write your own strlen:
    Code:
    char *p = new BYTE[custom_strlen(s)+1];
    
    int custom_strlen(BYTE *s) {
        int x = 0;
    
        while(*s++) x ++;
    
        return x;
    }
    Although strlen() actually returns a size_t.
    Forgive my ignorance, but what exactly is size_t?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by dwks
    Well then, write your own strlen:
    Code:
    char *p = new BYTE[custom_strlen(s)+1];
    
    int custom_strlen(BYTE *s) {
        int x = 0;
    
        while(*s++) x ++;
    
        return x;
    }
    Although strlen() actually returns a size_t.
    Forgive my ignorance, but what exactly is size_t defined as?

    *edit*

    Ah sorry about the double post - I clicked stop after I clicked submit because I wanted to change it.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Thanks guys, it seems I will have to ask the person that wrote the lib how to get the size of the array.

    Ahluka: A size_t is an unsigned int(or int64, depending on the architecture that you are going to compile on).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-02-2009, 07:24 AM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  5. static char in function problem
    By cpeschke in forum C Programming
    Replies: 3
    Last Post: 02-15-2005, 07:44 PM