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
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
Ok here an example:
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.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; }
You never mention what the function should doOriginally 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
Just remember to deallocate the allocated memory with deleteCode: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; }
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.
>> 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".
Magos has already shown you how it should be done.Code:char* function() { char array[10]; strcpy( array, "Hello" ); return array; // Dragons be here!!!! You cannot do this }
Alternatively, you could declare the local array as static, and it'll remain even after you return from the function:
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).Code:char* MyFunction(...) { static char MyArray[64]; strcpy(MyArray, "No what purpose does this function have?"); return MyArray; }
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.
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
Just remember to deallocate the allocated memory with deleteCode: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; }
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.
>> 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
Thanks everybody,
It is working now, using malloc function...
FE