Thread: HELP: Function returning char *

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Question HELP: Function returning char *

    Hello,

    I need a function in C that accepts some const arguments, and return a string (a char *). In my functions, I get memory fault error at execution time.

    Could you please provide me with a small sample function, and its invokation?

    Thanks

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Post your code so we can take a look at it.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Ok here an example:
    Code:
    char *mystrcat(char *dest, const char *src1, const char *src2)
    {
    	char *tmp = dest;
    	while(*tmp) tmp++;
    	for(; *src1; tmp++, src1++) *tmp = *src1;
    	for(; *src2; tmp++, src2++) *tmp = *src2;
    	*tmp = *src2;
    	return dest;
    }
    
    int main(void)
    {
    	char buf[1024];
    	strcpy(buf, "Hello");
    	printf(mystrcat(buf, " world", "!\n"));
    	return 0;
    }
    The memory fault is probably because you didn't allocate space for your buffers. If you change char buf[1024] to char *buf, you will get an memory failure because there is no space allocated to copy the strings in.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: HELP: Function returning char *

    Originally posted by FaridEsna
    Hello,

    I need a function in C that accepts some const arguments, and return a string (a char *). In my functions, I get memory fault error at execution time.

    Could you please provide me with a small sample function, and its invokation?

    Thanks
    You never mention what the function should do
    Code:
    char* MyFunction(const char* Arg1, const char* Arg2)
    {
       char* String = new char[32];
       if(String != NULL)
       {
          strcpy(String, "Well, what should I return?");
       }
       return String;
    }
    
    int main()
    {
       char* MyPointer;
       MyPointer = MyFunction("Something", "Something else");
       printf("%s", MyPointer);
       if(MyPointer != NULL) delete[] MyPointer;
    }
    Just remember to deallocate the allocated memory with delete
    Last edited by Magos; 10-17-2002 at 11:16 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> In my functions, I get memory fault error at execution time.

    That's because you are returning the address of a local variable. If you declared the char* on the stack (in the function), then you will get that error. So what you should do is what Magos suggested, allocate the memory dynamically and return the pointer. And like Magos said "Just remember to deallocate the allocated memory with delete".
    Code:
    char* function()
    {
         char array[10];
    
         strcpy( array, "Hello" );
    
         return array;  // Dragons be here!!!! You cannot do this
    }
    Magos has already shown you how it should be done.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Alternatively, you could declare the local array as static, and it'll remain even after you return from the function:
    Code:
    char* MyFunction(...)
    {
       static char MyArray[64];
       strcpy(MyArray, "No what purpose does this function have?");
       return MyArray;
    }
    Observe: There can be only 1 MyArray in your whole program, even if you run the function twice. This means that running the function twice doesn't guaranty that the first return value is the same when the second function finish (unless you stored it elsewhere).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Re: Re: HELP: Function returning char *

    Hi,

    Thanks for your good explanations. But seems that "new" function is a C++ one. I'm using C and I cannot use "new" function. But I'm sure that the problem is lack of memory.

    Unfortunately, the program is somewhat complicated, and calls lots of functions. So, I think it is not usefull to attach it.

    Thanks again.
    FE

    Originally posted by Magos
    You never mention what the function should do
    Code:
    char* MyFunction(const char* Arg1, const char* Arg2)
    {
       char* String = new char[32];
       if(String != NULL)
       {
          strcpy(String, "Well, what should I return?");
       }
       return String;
    }
    
    int main()
    {
       char* MyPointer;
       MyPointer = MyFunction("Something", "Something else");
       printf("%s", MyPointer);
       if(MyPointer != NULL) delete[] MyPointer;
    }
    Just remember to deallocate the allocated memory with delete

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, the "C++ People" get lost some times and wander in here.

    You want 'malloc' (or calloc) and 'free' instead.

    char *s;
    s = malloc( 32 * sizeof( char ) );

    ...

    free( s );

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> But I'm sure that the problem is lack of memory.
    Unlikely. Most likely caused by the program trying to access memory it's not supposed to.

    >> Unfortunately, the program is somewhat complicated, and calls lots of functions. So, I think it is not usefull to attach it.
    Step through the code and find out where it crashes. Post that section of code and its related parts.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    4
    Thanks everybody,

    It is working now, using malloc function...

    FE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM