Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Translations from Turing
Index -> Programming, Turing -> Turing Tutorials
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu Nov 18, 2004 6:53 pm   Post subject: (No subject)

Amazingly enough, that list is probably missing a lot of languages.
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Wed May 20, 2009 7:38 pm   Post subject: RE:Translations from Turing

C:

char * greeting(const char * name)
{
   char * output = (char *)malloc(sizeof(char) * (9 + strlen(name)));
   sprintf(output, "Hello, %s!", name);
   return output;
}


Wouldn't the line with malloc(); lead to a memory leak? You're not freeing it.

[edit] Sorry, for bumping, I didn't realize this had been dead for years Sad
wtd




PostPosted: Fri May 22, 2009 12:06 am   Post subject: Re: RE:Translations from Turing

DtY @ Thu May 21, 2009 8:38 am wrote:
C:

char * greeting(const char * name)
{
   char * output = (char *)malloc(sizeof(char) * (9 + strlen(name)));
   sprintf(output, "Hello, %s!", name);
   return output;
}


Wouldn't the line with malloc(); lead to a memory leak? You're not freeing it.


Why in the world would I free that memory within the function itself?

The whole point of allocating on the heap is so that the function returns a valid pointer.
DtY




PostPosted: Fri May 22, 2009 7:01 am   Post subject: RE:Translations from Turing

Yes, I know if you freed it inside that function the pointer would be useless, but you never free that memory.
DemonWasp




PostPosted: Fri May 22, 2009 11:00 am   Post subject: RE:Translations from Turing

He has a point. In your main(), you should be capturing that pointer and freeing it properly, but you're not. Probably a non-issue since the program ends immediately but better style regardless.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 20 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: