Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 07-01-05   #21 (permalink)
Overclocker
 
Join Date: Dec 2004
Posts: 401

Rep: 65 Melraidin is acknowledged by some
Unique Rep: 48
FAQs Submitted: 4
Trader Rating: 1
Default

Quote:
Originally Posted by kris_cs1
Dam! I got it to work perfectly using ^^^ but now when i tried to convert it to a function that I can use I can't get it to work It says: '╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠©ÛU╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠< ╠╠╠╠╠╠╠╠'

PHP Code:
charChangeCase(char Sentence[80], char Case)
{
char newSentence[80];
for( 
int n=0Sentence[n]!='\0'n++)
{
switch (Case)
{
case 
'u':
newSentence[n]=toupper(Sentence[n]);
break;
case 
'l':
newSentence[n]=tolower(Sentence[n]);
break;
default:
newSentence[n] = Sentence[n];
};
}
*( 
newSentence KrisIO::Len(Sentence)) = 0;
return 
newSentence;

Also you say the first problem is that i shouldn't free the memory at the end of the function... have i actually done that because i don't know how to lol

Plus, thanks to this code from you:

My Mid() function also neary works. There is just a preceding = sign I need to get rid of from the result somehow and I need to free the memory somehow.

It's all going well
Thank you

You haven't freed the memory in your own code, but you will have to at some point. Look into free () (should be used anytime you malloc something), and make sure you use it in the code that calls your function. If you freed the memory inside the function then the return value may not be valid anymore.

For the mid () function, try:

return newSentence + fromNumber;
Melraidin is offline   Reply With Quote
Old 07-02-05   #22 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Wooooo got em to work as far as i can tell now!

Just need to free the memory now.... do you mean every time I call the function, I must free the memory underneath it as well? i.e.

Variable = Mid(variable2, 2,4);
Free (Variable2);

because that would just be annoying...
Anyway, thank you everyone for all your help
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Old 07-02-05   #23 (permalink)
Overclocker
 
Join Date: Dec 2004
Posts: 401

Rep: 65 Melraidin is acknowledged by some
Unique Rep: 48
FAQs Submitted: 4
Trader Rating: 1
Default

Quote:
Originally Posted by kris_cs1
Wooooo got em to work as far as i can tell now!

Just need to free the memory now.... do you mean every time I call the function, I must free the memory underneath it as well? i.e.

Variable = Mid(variable2, 2,4);
Free (Variable2);

because that would just be annoying...
Anyway, thank you everyone for all your help
Yes, sadly, with the way it's currently coded you do need to free the memory pointed to by the returned pointer. Otherwise you've got yourself a memory leak.

To avoid this you may consider what the real purpose of the function is: do you want to just get some characters from the middle and never use the rest, or do you sometimes need the rest at a later time? If the former then instead of returning a pointer to a newly allocated char array you could simply overwrite the input char array. That way the calling code doesn't need to do any memory maintenance, as it does above.

Another option would be to have the calling code pass in a pointer to a char array for the function to place its output in. This would be my preferred method. This way it's much easier for the calling code to decide when to do this memory maintenance: perhaps you're going to be calling the mid () function many times and don't care about previous calls, in that case you could just keep passing in the same char array pointer and not need to worry about the overhead of freeing memory all the time.
Melraidin is offline   Reply With Quote
Old 07-03-05   #24 (permalink)
PC Gamer
 
kris_cs1's Avatar
 
amd ati

Join Date: Apr 2005
Location: Chester, Cheshire
Posts: 218

Rep: 27 kris_cs1 is acknowledged by some
Unique Rep: 24
FAQs Submitted: 1
Trader Rating: 0
Default

Quote:
Originally Posted by Melraidin
Yes, sadly, with the way it's currently coded you do need to free the memory pointed to by the returned pointer. Otherwise you've got yourself a memory leak.

To avoid this you may consider what the real purpose of the function is: do you want to just get some characters from the middle and never use the rest, or do you sometimes need the rest at a later time? If the former then instead of returning a pointer to a newly allocated char array you could simply overwrite the input char array. That way the calling code doesn't need to do any memory maintenance, as it does above.

Another option would be to have the calling code pass in a pointer to a char array for the function to place its output in. This would be my preferred method. This way it's much easier for the calling code to decide when to do this memory maintenance: perhaps you're going to be calling the mid () function many times and don't care about previous calls, in that case you could just keep passing in the same char array pointer and not need to worry about the overhead of freeing memory all the time.
Ok, thanks for the info! I will try and sort it all out now
You're the best!
__________________
Kris



System: My System
CPU
AMD Sempron 2800+
Motherboard
ASUS A7N8X-VM
Sound Card
5.1 SURROUND SOUND
Power Supply
350W
Case
Black
OS
Windows XP Home SP2
Monitor
19"
kris_cs1 is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 03:49 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.27700 seconds with 8 queries