Returning char*

This is a discussion on Returning char* within the C++ Programming forums, part of the General Programming Boards category; If i create a function: Code: char *fn1(){ char *retVal=new char[100]; strcpy(retVal, "test"); return(retVal); } And use it like this: ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Returning char*

    If i create a function:
    Code:
    char *fn1(){
      char *retVal=new char[100];
      strcpy(retVal, "test");
      return(retVal);
    }
    And use it like this:
    Code:
    char *ptr=0;
    if(ptr=fn1())
      printf(ptr);
    delete [] ptr;
    Is this the correct way to return a pointer to char?
    Does the compiler know the amount of allocated memory, or am i messing everything up?

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    I believe you are correct

    Hi,

    In the current standard, if new failed to allocate memory, it is supposed to raise an exception and not the old way of returning 0.

    If we assume that your compiler returns 0 if new fails, in that case, it seems correct.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You did it correctly.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    It's right, unless the dynamic memory allocation fails... Which also doesn't mean that the code isn't correct.
    none...

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Thx guys.
    I was just insecure regarding the deletion of allocated memory...

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    think2blue

    You code is OK, but I would not return a string like this.

    The code calling the fn1 will have a requirement to delete[] the string array. This is often considered bad practice. There are better ways to return strings in C++.

    You could:

    1. Return a string object. This is best. I.e.

    Code:
    string fn1()
    {
      return "test";
    }
    2. If you want you're code to use char*, you could cause fn1 to copy the result into an existing buffer.

    Code:
    void fn1(char* s, int s_len)
    {
      strncpy(s, "test", s_len);
    }
    3. There are other ways also, including the one you suggested. But whether you should use them depends on the requirements of your project. I.e. if your code forms a DLL etc.
    Last edited by Davros; 01-13-2003 at 09:35 AM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    void fn1(char* s, int s_len)
    {  
        strncpy(s, "test", s_len);
    }
    Don't forget, this isn't guaranteed to NULL terminate the array. Only if there is room in the array will strncpy include the \0.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    #include <new>
    
    // ...
    char *retVal = new(nothrow) char[100];
    // will be NULL on failed alloc
    Use new(nothrow) if you want 0 returned on failure (instead of throwing std::bad_alloc)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. importance of returning reference in operator overloading.
    By vaibhavs17 in forum C++ Programming
    Replies: 20
    Last Post: 05-13-2009, 01:28 PM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 06:02 AM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 06:38 PM
  4. Function returning incorrect value
    By CHurst in forum C Programming
    Replies: 3
    Last Post: 12-13-2005, 01:27 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

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