Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (anonymous):

Need Help: Why Dynamic memory allocation by using pointers provides an efficient way of utilizing computer memory but such memory allocation may be problematic if not handled properly in program.

OpenStudy (anonymous):

anyone help

OpenStudy (rsmith6559):

There are several ways to misuse memory. First, if you're allocating for say a 20 character sting, you need to allocate 21 bytes, 42 for wide characters, to allow for a the terminating 0. If your program tries to write 25 characters to that memory, you'll overrun into some other data's memory. When you're done with the memory, if it's not freed, you can run out of available memory.

OpenStudy (anonymous):

what do mean by 42 for wide characters to allow for a the termination 0.

OpenStudy (rsmith6559):

"Strings" (which are actually arrays of characters) need a terminating value. Zero has been adopted for this, so a string is actually one byte longer than what you see. Computers started very American-centric. ASCII is an acronym for American Standard Code for Information Interchange. Originally, there were 127 characters defined, ASCII-7. After adding more characters (mostly symbols), it became ASCII-8. ASCII-8 basically became UTF-8. I've been told that the Chinese alphabet has over 29,000 characters, so that would require 16 bits, two bytes to represent. This is UTF-16. C refers to 16 bit character representation as wide characters. The 20 character "string" in the example would be 20 x 2 bytes long plus a 16-bit terminating zero, 42 bytes.

OpenStudy (anonymous):

Amazing, i love this information... but, would you tell me... What is the advantage and disadvantages of dynamic memory allocation... Is this safe for small programming like microwave oven and and other little things. and is it good for large systems.. then why we use int and char instead of dynamic memory allocation...

OpenStudy (e.mccormick):

What if your program is able to use more memory whever it needs and you make a very small mistake in there? So it just keeps taking more and more and eventually runs the computer out of memory. This is called a memory leak.

OpenStudy (e.mccormick):

Now lets say you want to free memory to prevent a leak. So you go out and free things using pointers and so on.... and you free up some memory that was already garbage collected! It has been reassigned and you free memory that is being used by another program! Oops.

OpenStudy (e.mccormick):

The advantage is that you do not have to statically track down and map out every last bit of memory needed. You can take in large chunks of data easily and improve the flow of a program by being dynamic. This improved performance has allowed computers to do far more than when they were static and limited to very small address ranges.

OpenStudy (e.mccormick):

Old saying: With great power comes great responsibility. Dynamic memory is one of these cases. It is powerful and useful and if you mess up it will crash the machine.

OpenStudy (e.mccormick):

Oh, and that is in no way a comprehensive list of things. While dynamic memory can make things more powerful, it can quickly cause them to be more problematic in other ways.

OpenStudy (anonymous):

amazing dear, much interesting information. You are the bank of information.. but, many operating system using dynamic memory allocation one example in old days of computer early 1960's + s many people using that time dynamic memory and sometime after 4 hour or morethen computer hang but, now windows is also using dynamic memory allocation .. any comment... so, please give me adivce i am new in programning can i use dynamic memory i

OpenStudy (anonymous):

so, what's your last comment are you in support or contradict of dynamic memory allocation ?

OpenStudy (e.mccormick):

It is a tool. Like any tool, it is great if it is used properly. And also, can be problematic when used improperly. I am not going to claim to be for or against it. I am just trying to point out why it is a complex issue that must be learned properly before being used.

OpenStudy (anonymous):

dear you point out only one issue which is memory leakage... any other side effects if i use this ... is computer will burn if i use to much and not free the allocated memory...

OpenStudy (rsmith6559):

int and char are data types. They are a known size, so the compiler can lay out memory at compile time. When you request memory dynamically, say for 100 chars, the compiler can translate that into a request for 100 bytes. Not impressive for chars, but ints, short ints and long ints vary in size obviously by definition, but also by hardware architecture ( 16, 32 or 64 bit ).

OpenStudy (anonymous):

Dear Friends i am confuse in dynamic memory data type let say if i declare int x; then compile time memory allocated for x if i use int *iptr; iptr = new int; then can int *iptr is taking compile time memory of iptr assign memory in run time.. i mean int x; has 4 bytes compile time and also run time is int *iptr; have 4 bytes allocated in compile time....

OpenStudy (rsmith6559):

int whatever // will have sizeof( int ) bytes allocated to it int* whatever // will have the size of a pointer that can address the memory space that the program is running in. It COULD actually use more memory than int would, if int was 32 bits, but memory went beyond 4GB, which would be 64 bit, 8 bytes. The value of a pointer is the address of the memory that it points to. If you want the value of the memory that the pointer points to, you have to dereference the pointer: int foo = 3; int* bar = ∫ *bar; // equals the 3 that foo contains. bar would be the address of foo.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!