Search This Blog

Sunday, November 05, 2006

Memory Troubles in C++

Peace be upon you

today it's a little strange topic :D, I will talk about memory & Stack"the precious thing for c++ developers",
I will tell you some troubles that you could meet,
  1. Memory Allocation and De-allocation.
  2. Memory fragmentation.
  3. Heap over flow.
  4. Stack over flow.
  5. Buffer over flow.
  6. which to use ( Virtual memory or heap )

ahhhhh, all of this could turn your life into hell if you don't use it right, if you miss use memory or stack, you will start cutting your hear one by one :D

first of all when you miss use "Memory Allocation and De allocation." you got something called memory leaks, which means that you ask operating system to allocate some memory space, and operating system reserve it for you.

all of that is cool, but where is the problem

the problem that when you don't allocate the memory, the memory still reserved, which means that you eat some of system memory which will cause slow down the whole system by time,

to avoid that you may be aware of your pointers and allocated memory, or you can use the smart pointers, it got a very cool solution for this problem,

the second one is memory fragmentation, this one is amazing :D this one may make you kill your self :D because it's so hard to find, the only way to find it is to check up the design or your code, memory fragmentation came by allocating and deallcating memory in high frequently way, I mean allocating 500 bytes 30000 times, this may cause memory fragmentation, the only way to solve this problem is to allocate enough piece of memory and keep working on it.

Heap over flow, this bug is simple one and you can avoid it by check your heap space and data you want to put, from it's name you can guess that it happen when you try to put a lot of data in not enough space, this will cause application crash,

Buffer over flow, this one may turn your application into a virus host, HOW COULD IT BE? simple the buffer you allocate in stack , when you exceed it you will be able to put SHELL CODE and run it as much as u want, this topic is little complicated ( you have to know the STACK object and heap object ), also you should know the dynamics of stack and stack pointer.

stack over flow, this happen by recursive calls, you should solve it by doing some algorithm with good design that a void this problem,

the virtual memeory, it's cool but how to use it, I think you should use it when your data exceed the limit of 1 or 2 mega, and do some algorithm that synchronous the data between the virtual memory and the heap,

I think the final word is before you start doing C++ code, write a good class for memeory management to have some peace while you writing the rest of the application.

thanks for reading this

thanks for trusting me ;)




at last :D

Please can you kick it? :Dkick it on DotNetKicks.com

No comments: