When to use Malloc?

Hi!
I hope this is the correct forum to post the question even if I'm a newbie...
I am a C-newbie (and really on the edge to be a C-addict :wink: ) and have a question.

When should I use malloc?
To state it differently, when should I NOT use malloc?

For instance, if I have an array of chararray/string pointers . Should I allocate space for both the array AND the strings?

Thank you for reading my question. :slight_smile: This really has been bugging me lately... :confused:
Tonje

The decision of whether to use malloc or not really depends on you.

If you want to have a program that does not allocate space that it does not need, then you should use malloc to allocate space when you know how much you will need. Also, if you are expecting to allocate a very large array, say of 100000 bytes or something, but it is possible that some cases may have only 500 of those bytes actually used, then you are again better off using malloc to allocate just those many bytes.

Of course, if you are going to use malloc - especially in a loop, remember to use free, or you risk crashing your program.

Thank you for replying so quickly! :slight_smile:

When I do not know the maximum size of the strings, it just seems like a waste of time to first count the letters then to malloc sufficiant space for the string.
But then again, I guess one gets better contol over the program?

What actually happens if I don't malloc space for the strings, (in the situation as mentioned in my previous post), and the strings that the array points to change all the time. What about the space the old strings occupy, will that automaticly be free space, or will it be occupied until the program terminates?

Thanks a million for your answer! :slight_smile:

Tonje

I really haven't got your question, but will try to answer just the same,
If you declare a character array for strings like char a[50], then you can have the string that occupies it change frequently over the course of your program, but as long as it does not go over 50*sizeo(char) bytes, it wont matter. The space that the old string occupies will be overwritten by the new one.
Note that the question of freeing space does not come up, as you have statically allocated memory. It will be freed only when you exit the program.

--NOTE--
The use of the word 'statically' does not mean that you are declaring any of the variables as static.

static int a;

is different from

 int a;

--/NOTE--

Thank you very much for your reply. It is "clearer" for me now. :o)

The advantages of malloc

  1. unix limits stack space - where normal C variables are stored.
    malloc uses heap memory which is usually not as limited as stack memory.
  2. after you call malloc, you can re-size storage for your buffer with realloc.
    you can make it larger or smaller. So you do not have to worry about
    sizing your variables to gigantic proportions beforehand.

Reading a whole file example:

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>

#define ck(x) if((x)==NULL)\
{perror("Error"); exit(EXIT_FAILURE);}
/* read nbyte from a file  - can read whole file */
ssize_t readall(int fd, void *buf, size_t nbyte){
     ssize_t nread = 0,
                   n=0;

     do {
         if ((n = read(fd, &((char *)buf)[nread], nbyte - nread)) == -1) {
             if (errno == EINTR)
                 continue;
             else
                 return (-1);
         }
         if (n == 0)
             return nread;
         nread += n;
     } while (nread < nbyte);
     return nread;
}

 /* get file size */
size_t file_size(FILE *in)
{
	 struct stat st;
	 if(fstat(fileno(in), &st) == (-1))
	 {
	 	perror("stat error");
	 	exit(EXIT_FAILURE);
	 }	
     return st.st_size;
}

int main(int argc, char *argv[1])
{
	char *buf=NULL;
	FILE *in=fopen(argv[1],"r");    /* open afile for read */
	size_t filebytes=0;

	ck(in);                       /* check file errors */
	filebytes=file_size(in);      /* get size of buf we need */
	if(filebytes)                 /* do we have a file with data in it? */
	{
		ck(buf=malloc(filebytes+1) );	/* create storage */
		memset(buf,0x0, filebytes+1);   /* init storage */
		if( readall(fileno(in),buf,filebytes)>0 ) /* read entire file */
		{
			ck(fprintf(stdout,"%s",buf)); /* print whole file */
		}
		else                         /* complain about errors reading file */
		{
			ck(fprintf(stderr,"file read error\n") );
			exit(EXIT_FAILURE);
	    }
	    free(buf);                 /* release the buffer */
	}
	if(! fclose(in)	)              /* close file with error check */
    {
    	return 0;                  /* normal return */
    }
	ck(fprintf(stderr, "filesystem error\n") );
	return EXIT_FAILURE;           /* file close error - return */
}