GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Jul-2009, 10:43
fairytales247 fairytales247 is offline
New Member
 
Join Date: Jul 2009
Posts: 15
fairytales247 has a little shameless behaviour in the past
Smile

Advantage of malloc() over array in C


Well 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  
Old 08-Jul-2009, 18:05
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: advantage of malloc() over array in C


Quote:
Originally Posted by fairytales247
Well 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.


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  
Old 08-Jul-2009, 18:18
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: advantage of malloc() over array in C


Quote:
Originally Posted by Mexican Bob
Unless you are using some non-standard compiler extensions, the size of an array must be declared at compile time.
Unless it's a C99 standard conforming compiler.
  #4  
Old 08-Jul-2009, 21:45
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: advantage of malloc() over array in C


Quote:
Originally Posted by dlp
Unless it's a C99 standard conforming compiler.

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  
Old 09-Jul-2009, 08:41
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Advantage of malloc() over array in C


For 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:
void foo (size_t n) {
   char * buff = 0;
   buff = calloc (n, sizeof (char));
   if (! buff) {
      /* handle allocation error */
   }
   
   /* ... process as usual ... */

   free buff;
   buff = 0;
}
Is less preferable to the VLA version
CPP / C++ / C Code:
void foo (size_t n) {
   char buff[n];
   memset (buff, 0, n);

   /* ... process as usual ... */
}
Again, each has their place, but the latter is much nicer in terms of ensuring proper memory handling.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 00:43.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.