C gurus ans this?

This is a discussion on C gurus ans this? within the C Programming forums, part of the General Programming Boards category; I need to return a char pointer from func. The function takes a string and then always returns a 40 ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    12

    C gurus ans this?

    I need to return a char pointer from func.
    The function takes a string and then always returns a 40 char value as a result of calling "giveout". I want to optimize this code in efficient way and want to use malloc and free to aviod any mem leak issue.

    Code:
    char * retrieve ( char *para1)
    {
    }
    Last edited by Jenny; 08-11-2009 at 06:17 PM. Reason: acadamic reason

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    So what is your question? Are there any problems with your code? Due to the fact I have no knowledge of what your other functions do, I can make no assumptions.

    Also this line of code:
    Code:
    outputstr = (char *) malloc( sizeof(outputstr) * 40 );
    Is there a reason you are using the size of a char pointer and multiplying it by 40? Doesn't seem to make a whole lot of sense.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    12
    Quote Originally Posted by carrotcake1029 View Post
    So what is your question? Are there any problems with your code? Due to the fact I have no knowledge of what your other functions do, I can make no assumptions.

    Also this line of code:
    Code:
    outputstr = (char *) malloc( sizeof(outputstr) * 40 );
    Is there a reason you are using the size of a char pointer and multiplying it by 40? Doesn't seem to make a whole lot of sense.

    1) Are there any problems with your code?
    >> No , there is no problem in current code but I want to optimize this code in efficient way and want to use malloc and free to avoid any mem leak issue.

    2) Is there a reason you are using the size of a char pointer and multiplying it by 40?
    >> The function takes a string and then always returns a 40 char value as a result of calling "giveout" func hence I multiplying it by 40.

    Looking forward for your help in optimizing the code.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Since your malloc is a fixed (and small size), and you don't even return it, why bother?

    char outputstr[41];
    would be more efficient

    41? Yes, you forgot to count the \0 at the end.

    You also forgot the \0 at the end of str[2] as well.
    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.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    There are a few holes, or should I say undocumented "assumptions", in your logic.

    1) You don't verify if inputstr will hold your "doubled-in-length" answer
    2) There's really no point to return a pointer to the same storage location you were passed
    3) There is no warning or error when your malloc fails, therefore, your answer will not be converted
    4) Depending on the compiler, this:
    Code:
    if ( outputstr != NULL )
    might be better written as this:
    Code:
    if ( outputstr )
    5) You really don't need the err variable
    6) You could initialize RC=0 during the compile instead of at runtime
    7) You could calloc() the area for outputstr instead of using malloc() and then clearing it manually
    8) You could actually get rid of variable rc since you are not using it for anything
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Quote Originally Posted by Jenny View Post
    1) Are there any problems with your code?
    >> No , there is no problem in current code but I want to optimize this code in efficient way and want to use malloc and free to avoid any mem leak issue.
    You keep saying that but it keeps not making sense. Using malloc when you don't need to (as is the case here) if anything can introduce the possibility of a memory leak. Not using it removes that possibility, and improves speed also.
    2) Is there a reason you are using the size of a char pointer and multiplying it by 40?
    >> The function takes a string and then always returns a 40 char value as a result of calling "giveout" func hence I multiplying it by 40.
    Yes but you're not returning 40 strings, you're returning a string of length 40, except that you're allocating 160 bytes to do so.
    Looking forward for your help in optimizing the code.
    Why would it need to be any faster? Sure there is plenty of unnecessary junk such as zeroing the first 40 bytes of the buffer and then overwriting that, but it needs to be correct first. At the moment a memory allocation failure would silently go unnoticed, plus there is a buffer overflow in the "str" array.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    12
    Quote Originally Posted by Dino View Post
    1) You don't verify if inputstr will hold your "doubled-in-length" answer
    2) There's really no point to return a pointer to the same storage location you were passed
    Can you explain in details what you mean here?

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    1) You don't verify if inputstr will hold your "doubled-in-length" answer
    I'm not sure what they mean by "doubled-in-length" answer - but in general you should always verify that your input is what you think it is, to avoid undefined behavior later.

    2) There's really no point to return a pointer to the same storage location you were passed
    Your function takes a pointer as an argument, and that same pointer is returned. Even though the actual data stored in memory at that location (i.e. the string itself) is modified, the location you're pointing to is still the same location. In other words:

    Code:
    char * one;
    char * two;
    ...
    two = retrieve(one);
    // at this point, two and one hold the same value
    // even though the string was modified

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    12
    Thanks Sean for your reply.
    Quote Originally Posted by sean View Post
    Your function takes a pointer as an argument, and that same pointer is returned. Even though the actual data stored in memory at that location (i.e. the string itself) is modified, the location you're pointing to is still the same location.
    [/code]
    So you recommend that I should change the below form
    Code:
    char * retrieve ( char *inputstr )
    {
       .
       .
       .
    
    }
    to ?

    Can you tell me what I should change it to?
    Last edited by Jenny; 08-11-2009 at 06:16 PM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    >> Can you tell me what I should change it to?
    Well you don't need to change it to anything. Returning a pointer that was passed as a parameter is actually kind of common (see strcpy() for instance). But if you didn't want this redundancy, you could change your function to not return anything at all. ie:
    Code:
    void retrieve (char *inputstr)
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Jenny View Post
    Can you explain in details what you mean here?
    Hi Jenny.

    In the original code you posted for your function, (you've since edited it out), you were converting from hex to printable hex, therefore, the end result is going to be twice as long when you are done with it.

    For example. if the input value is X'012345', which is 3 bytes, the output answer will be '012345', which is 6 bytes.

    My point was that you need to be certain that the storage pointed to by inputstr is large enough to hold double its initial value.

    Dino
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  2. calculating e
    By warg in forum C Programming
    Replies: 14
    Last Post: 01-29-2006, 03:57 PM
  3. Infinte loop with switch, Why??
    By Kate in forum C Programming
    Replies: 8
    Last Post: 06-16-2005, 02:15 AM
  4. don't know where to start
    By spikerotc04 in forum C Programming
    Replies: 5
    Last Post: 04-26-2005, 04:21 PM
  5. having problem with function
    By spikerotc04 in forum C Programming
    Replies: 6
    Last Post: 04-22-2005, 03:37 PM

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