![]() |
| | #1 |
| Registered User Join Date: Feb 2009
Posts: 278
| malloc/free of return value Code: char * sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2))); |
| Bladactania is offline | |
| | #2 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |||
| laserlight is offline | |
| | #3 |
| Registered User Join Date: Feb 2009
Posts: 278
| Thanks for the quick reply... How does the caller deallocate the memory for a pointer with function level scope? |
| Bladactania is offline | |
| | #4 |
| Registered User Join Date: Feb 2009
Posts: 278
| Another thing... If I'm returning a pointer to a string like this, do I need to allocate the memory for the pointer I'm returning the value into? Code: test = malloc(.....); test = mystringfunction(s1, s2); |
| Bladactania is offline | |
| | #5 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is offline | |
| | #6 |
| Registered User Join Date: Feb 2009
Posts: 278
| Here's my function Code... Code: char * String_Concat (const char *s1, const char *s2) {
char *sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2)));
strcpy(sTemp, s1);
strcat(sTemp, s2);
return sTemp;
}
Code: char *sDiagnostic_Log_Path; // Full path of diagnostic log file sDiagnostic_Log_Path = String_Concat(sFlash_Card_Base_Path, sDiagnostic_Log_Name); |
| Bladactania is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Code: char* foo() { return malloc(10); }
void foo2(char** p) { *p = malloc(10); }
int main()
{
char* p;
p = foo();
free(p);
foo2(&p);
free(p);
}
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #8 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > char *sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2))); Why sizeof(s1) ? That's the size of the pointer, not a single char. Cheaply, it's char *sTemp = malloc( strlen(s1) + strlen(s2) + 1); Pedantically, in line with what you're doing char *sTemp = malloc((sizeof(s1[0]) * strlen(s1)) + (sizeof(s2[0]) * strlen(s2)) + 1);
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #9 |
| Registered User Join Date: Feb 2009
Posts: 278
| Could I use sizeof(*s1)? I suppose I could use sizeof(char) since I know it will be a character. |
| Bladactania is offline | |
| | #10 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| If you like, it's the same thing.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #11 |
| Registered User Join Date: Feb 2009
Posts: 278
| when using sizeof(*s1) I get a "Nonportable pointer conversion" warning... Is there a way to avoid this? |
| Bladactania is offline | |
| | #12 |
| Registered User Join Date: Feb 2009
Posts: 138
| avoid it by not using sizeof(*s1). sizeof(char) is 1 anyway and N*1 is always going to be N so it's redundant. |
| Meldreth is offline | |
| | #13 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| What a strange error message from applying sizeof to something.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #14 |
| Registered User Join Date: Feb 2009
Posts: 278
| I think the warning comes from the strlen(s1) not the sizeof(char). |
| Bladactania is offline | |
| | #15 |
| Registered User Join Date: Feb 2009
Posts: 278
| I am using Turbo C version 3.0... *sigh* |
| Bladactania is offline | |
![]() |
| Tags |
| functions, malloc, memory, pointers, strings |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another weird error | rwmarsh | Game Programming | 4 | 09-24-2006 10:00 PM |
| Why only 32x32? (OpenGL) [Please help] | Queatrix | Game Programming | 2 | 01-23-2006 02:39 PM |
| opengl help | heat511 | Game Programming | 4 | 04-05-2004 01:08 AM |
| opengl code not working | Unregistered | Windows Programming | 4 | 02-14-2002 10:01 PM |
| Algorithm to walk through a maze. | Nutshell | C Programming | 30 | 01-21-2002 01:54 AM |