Something I can answer! omg im giddy! XD
if you do something like malloc or realloc there is a chance you might not have enough memory available to be allocated. Basically you want a setup like this :
Code:
void *p;
p = realloc . . . // strut your stuff
if ( p != NULL )
{
//Do whatever you intended to do
StrokeKitty(3);
free(p);
//free the memory you used so the operating system can use it again.
//I'm not sure about this but if I remember correctly you only free memory that was allocated successfully
//Makes sense no?
}
else
{
//Break or something from the function because well you didn't get
//that memory from the os. darn.
//Fml right?
}
The same goes for malloc and calloc. I want to throw something about the stack in here but I'm too afraid Salem , Grumpy or laserlight will prove me wrong :/
As for free , you call free to release the memory that you've been using. If you don't free the memory and your program keeps running ( a background process or something ) then well that memory won't be accessibile because you ( and your program which happens to never end ) never told the OS that it can have it's crap back now . I had a post somewhere. . .
String Handling - Memory Leak?
laserlight explains what I was doing wrongly. I think it might be the same as an assembly program that I wrote although in this particular example I was trying to see if I could force a stack overflow.
Code:
BITS 32
extern __cprintf
extern __getch
extern _ExitProcess
global main
section .data
StackGrowth dd 0
StackState dd 0
StackGrowthMessage db "Stack Growth -> %i",0ah,0
CounterMessage db "Counter -> %i",0ah,0
EspMessage db "ESP Register -> %i",0ah,0
EbpMessage db "EBP Register -> %i",0ah,0
ThreeNewLines db "",0ah,0ah,0ah,0
ThreeNewLinesMessage db "%s",0
Counter dd 0
section .text
main:
mov dword [StackState],esp
push ebp
push EbpMessage
call __cprintf
add esp,8;2*4 bytes = 8
push esp
push EspMessage
call __cprintf
add esp,8
push dword [Counter]
push CounterMessage
call __cprintf
add esp,8
;call __getch
push ThreeNewLines
push ThreeNewLinesMessage
call __cprintf
add esp,8 ;If this op is not defined a stack overflow will occur
cmp [Counter],dword 1000000000
je exit
jmp Increment
exit:
push 0
call _ExitProcess
Increment:
add [Counter],dword 1
; INVALID : mov [StackGrowth],[StackState]
mov ebx,[StackState]
mov [StackGrowth],ebx
mov ebx,esp
sub ebx,[StackGrowth]
mov [StackGrowth],ebx
neg dword [StackGrowth]
push dword [StackGrowth]
push StackGrowthMessage
call __cprintf
add esp,8
JMP main
I couldnt upload the exe but thats fine. It really just was for the fun of it.
I just know someone's going to jump me on this but feel free to do so. Basically what happens here is I was pushing values to the stack (which is kind of like allocating memory ) but I wasn't popping them / changing the esp register accordingly (which is kind of like free ) which results in a stack overflow ( think of a tower that keeps getting higher and higher until finally well lots of people are dead - OK SO THE STACK UUUUSUUAAALLY GROWS DOWNWARDS THATS NOT REALLY THE POINT XD - ).
I really did try on this post I swear I tried