|
|
|
#21 (permalink) | |
|
Overclocker
|
Quote:
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;
__________________
DD TDX waterblock DD Maze 4-1 CPU (w. Peltier) Swiftech pump DD reservoir DD radiator Dual heatercore |
|
|
|
|
|
|
#22 (permalink) | ||||||||
|
PC Gamer
|
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
|
||||||||
|
|
|
|
|
#23 (permalink) | |
|
Overclocker
|
Quote:
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.
__________________
DD TDX waterblock DD Maze 4-1 CPU (w. Peltier) Swiftech pump DD reservoir DD radiator Dual heatercore |
|
|
|
|
|
|
#24 (permalink) | |||||||||
|
PC Gamer
|
Quote:
You're the best!
__________________
Kris
|
|||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|