![]() |
|
#1
|
|||
|
|||
Advantage of malloc() over array in CWell the author of the book prefers to use malloc() in a program to find prime numbers.
Since we don't know initially how many prime numbers the user wants, the author wants to allocate memory using malloc() But the same thing could be done using an array. I am just a beginner so don't know the real advantage. Thanks for the help. |
|||
|
#2
|
||||
|
||||
Re: advantage of malloc() over array in CQuote:
Unless you are using some non-standard compiler extensions, the size of an array must be declared at compile time. Therefore, since we don't know how many prime numbers the user wants, we can not "do the same thing" using an array, because we won't know what size to make it. We could easily size it way too small or even just 1 smaller than necessary and we would have serious problems. There are some other benefits to using "heap" allocations versus "stack" allocations, but that is topic of another discussion. The basic "test" for allocation is allocate only as much storage as is required. If you do not know how much will be required, dynamic allocation is probably your better choice. If you know that the user has only a surname, given name and perhaps a middle name, then allocation an array size of 3 elements deep appears to be a good option. But, how long is each name in characters (adding one extra for the terminator)? What of those who have four or five names? What happens to our array then? What if we decide that nobody has more than 15 letters in any one of their names and then we encounter someone with 20? This is a fairly typical problem with C and C++ programming. C++ handles this with much greater ease to the programmer through its standard containers and "string" class, but there isn't as much simplicity in C. There are times when we know exactly how large something will be (or at least its maximum allowable size) and we can statically allocate for that maximum. MxB |
|
#3
|
|||
|
|||
Re: advantage of malloc() over array in CQuote:
|
|
#4
|
||||
|
||||
Re: advantage of malloc() over array in CQuote:
To suggest that a VLA can replace a fixed size array is yet another over-simplification. While it is possible to declare VLAs in a C99-conformant compiler at a function prototype scope, some VLA declarations are simply not allowed. Simplified and portable is that arrays must be sized at compile time. MxB |
|
#5
|
|||
|
|||
Re: Advantage of malloc() over array in CFor the problem originally posted here, the idea of using VLAs is misguided; it would take a rather non-intuitive approach to accomplish the goal. However, the use of VLAs can be a huge benefit when dealing with the generation of strings and such on the fly where many *alloc/free would open the door to leaks.
For instance: CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
My personal site: Utilities for text processing, debugging, testing and plotting |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| where is the problem and can you fix it (php) | oggie | MySQL / PHP Forum | 8 | 14-Apr-2008 16:08 |
| Getting a line error in register | oggie | MySQL / PHP Forum | 5 | 13-Apr-2008 17:16 |
| What is an array? | Howard_L | C Programming Language | 3 | 05-Oct-2007 06:11 |
| How to sort in C++ alphabetically | wilen | C++ Forum | 5 | 20-Apr-2007 15:43 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The