Calloc v's Malloc 
Author Message
 Calloc v's Malloc

Hello.

I wrote the following program as an exercise in Kernighan and
Ritchie's book, but I was wondering whether someone could tell me the
simplest way of replacing the two malloc statements with one calloc
statement.

Thanks,

David King

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* compare two files */

main(int argc, char *argv[])
{
char *line = (char *) malloc(100*sizeof(char));
char *line1 = (char *) malloc(100*sizeof(char));
FILE *fp, *fp1;
int n = 100;

        if (argc != 3)
                printf("usage: 7.6 file1 file2\n");
        else    {
                if ((fp = fopen(argv[1], "r")) == NULL)
                        {
                        fprintf(stderr, "can't open %s\n", argv[1]);
                        exit(1);
                        }
                else
                        fp1 = fopen(argv[2], "r");
                        while (fgets(line, n, fp) != NULL)
                                {
                                fgets(line1, n, fp1);
                                        if (strcmp(line1, line) == 0)
                                        continue;
                                        else
                                                {
                                                printf("Files differ at the following line of the second file: %s\n",
line1);
                                                exit(1);
                                                }
                                }      
                }
fclose(fp);
fclose(fp1);

Quote:
}



Mon, 13 Nov 2000 03:00:00 GMT  
 Calloc v's Malloc

Quote:

> Hello.

> I wrote the following program as an exercise in Kernighan and
> Ritchie's book, but I was wondering whether someone could tell me the
> simplest way of replacing the two malloc statements with one calloc
> statement.

<snip>

Quote:
> char *line = (char *) malloc(100*sizeof(char));
> char *line1 = (char *) malloc(100*sizeof(char));

I don't see how these could be replaced with one calloc unless you merge
the two lines into one variable.  calloc() is practically equivalent to
malloc(), except that it zeros the bits of the allocated memory.  I
prefer calloc() because it automatically initializes some types to zero,
and there's also a better chance that the memory you get from calloc()
will actually have been allocated; some implementations of malloc()
don't actually find the memory until you access it the first time, which
may cause it to tell the program it allocated more than it really could.

Also, you should remove the casts from the calls to malloc() since they
might hide the error of not prototyping it, and since sizeof(char) is 1
multiplying by it is unnecessary.

--

I believe we can change anything.
I believe in my dream.
    - Joe Satriani



Wed, 15 Nov 2000 03:00:00 GMT  
 Calloc v's Malloc

:Hello.
:
:I wrote the following program as an exercise in Kernighan and
:Ritchie's book, but I was wondering whether someone could tell me the
:simplest way of replacing the two malloc statements with one calloc
:statement.
============
No, you can't replace two malloc calls with one calloc call.  However:

:main(int argc, char *argv[])
:{
:char *line = (char *) malloc(100*sizeof(char));
:char *line1 = (char *) malloc(100*sizeof(char));

is easily rewritten to the better
int main(int argc, char *argv[])
{
char *line = malloc(100);
char *line1 = malloc(100);

But for several reasons, including the fact that your
code foolishly assumes malloc succeeded, this is even
better rewritten as
int main(int argc, char *argv[])
char line[100], line1[100];

======================
:
:Thanks,
:
:David King

:
:#include <stdio.h>
:#include <stdlib.h>
:#include <string.h>
:
:/* compare two files */
:
:main(int argc, char *argv[])
:{
:char *line = (char *) malloc(100*sizeof(char));
:char *line1 = (char *) malloc(100*sizeof(char));
:FILE *fp, *fp1;
:int n = 100;
:
: if (argc != 3)
: printf("usage: 7.6 file1 file2\n");
: else {
: if ((fp = fopen(argv[1], "r")) == NULL)
: {
: fprintf(stderr, "can't open %s\n", argv[1]);
: exit(1);
: }
: else
: fp1 = fopen(argv[2], "r");
: while (fgets(line, n, fp) != NULL)
: {
: fgets(line1, n, fp1);
: if (strcmp(line1, line) == 0)
: continue;
: else
: {
: printf("Files differ at the following line of the second file: %s\n",
:line1);
: exit(1);
: }
: }
: }
:fclose(fp);
:fclose(fp1);
:}
:
:



Thu, 16 Nov 2000 03:00:00 GMT  
 Calloc v's Malloc

:
:But for several reasons, including the fact that your
:code foolishly assumes malloc succeeded, this is even
:better rewritten as
:int main(int argc, char *argv[])

Aargh! I left out the opening '{'.  I wonder what kind of
klewness newbie this "Martin Ambuhl" is!!

:char line[100], line1[100];



Thu, 16 Nov 2000 03:00:00 GMT  
 Calloc v's Malloc

Quote:

>Hello.

>I wrote the following program as an exercise in Kernighan and
>Ritchie's book, but I was wondering whether someone could tell me the
>simplest way of replacing the two malloc statements with one calloc
>statement.

What do you want to achieve using that? malloc and calloc are very similar,
the only real difference between them is that calloc set the allocated memory
to all-bits-zero. True, calloc takes two arguments but that doesn't increase
its functionality, you can allocate the same space by calling malloc with
the product of the arguments passed to calloc.

Quote:
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>

>/* compare two files */

>main(int argc, char *argv[])
>{
>char *line = (char *) malloc(100*sizeof(char));
>char *line1 = (char *) malloc(100*sizeof(char));

You could convert these into one allocation like so:

 char *line = malloc(2*100);
 char *line1 = line+100;

If you insist on unsing calloc you might try:

 char *line = calloc(2, 100);
 char *line1 = line+100;

Note that this technique won't work in general if the objects you are
allocating have different types, you need to ensure that alignment is
correct for the second object. That isn't a problem in this case however.

Quote:
>FILE *fp, *fp1;
>int n = 100;

>        if (argc != 3)
>                printf("usage: 7.6 file1 file2\n");
>        else    {
>                if ((fp = fopen(argv[1], "r")) == NULL)
>                        {
>                        fprintf(stderr, "can't open %s\n", argv[1]);
>                        exit(1);

1 doesn't necessarily indicate failure. The portable way to indicate that
is to use:

                         exit(EXIT_FAILURE);

Quote:
>                        }
>                else
>                        fp1 = fopen(argv[2], "r");
>                        while (fgets(line, n, fp) != NULL)
>                                {
>                                fgets(line1, n, fp1);
>                                        if (strcmp(line1, line) == 0)
>                                        continue;
>                                        else
>                                                {
>                                                printf("Files differ at the
> following line of the second file: %s\n",
>line1);
>                                                exit(1);
>                                                }
>                                }      
>                }

You really should tidy up allocated memory where possible:

 free(line);

Quote:
>fclose(fp);
>fclose(fp1);
>}

--
-----------------------------------------


-----------------------------------------


Thu, 16 Nov 2000 03:00:00 GMT  
 Calloc v's Malloc

Hi,

Maybe something like:
I don't like lots of magik numbers so also a #define.

#define LINE_SIZE 100

char *line = (char *) calloc(2*LINE_SIZE, sizeof(char));
char *line1 = line+LINE_SIZE;
int n = LINE_SIZE;

Quote:

> Hello.

> I wrote the following program as an exercise in Kernighan and
> Ritchie's book, but I was wondering whether someone could tell me the
> simplest way of replacing the two malloc statements with one calloc
> statement.

> Thanks,

> David King

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> /* compare two files */

> main(int argc, char *argv[])
> {
> char *line = (char *) malloc(100*sizeof(char));
> char *line1 = (char *) malloc(100*sizeof(char));
> FILE *fp, *fp1;
> int n = 100;

>         if (argc != 3)
>                 printf("usage: 7.6 file1 file2\n");
>         else    {
>                 if ((fp = fopen(argv[1], "r")) == NULL)
>                         {
>                         fprintf(stderr, "can't open %s\n", argv[1]);
>                         exit(1);
>                         }
>                 else
>                         fp1 = fopen(argv[2], "r");
>                         while (fgets(line, n, fp) != NULL)
>                                 {
>                                 fgets(line1, n, fp1);
>                                         if (strcmp(line1, line) == 0)
>                                         continue;
>                                         else
>                                                 {
>                                                 printf("Files differ at the following line of the second file: %s\n",
> line1);
>                                                 exit(1);
>                                                 }
>                                 }
>                 }
> fclose(fp);
> fclose(fp1);
> }

--
Regards,

IFB Systems Administrator


Fri, 17 Nov 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. What's different between malloc() and calloc?

2. malloc vs 'no-malloc' - help

3. Override malloc,calloc,realloc and free?

4. Problems changing from Malloc to Calloc

5. calloc VS malloc ??

6. problem with malloc/calloc

7. malloc & memset versus calloc

8. difference between malloc and calloc

9. malloc/calloc problems

10. malloc,calloc undefined behaviour ??

11. allocating memory via malloc/calloc

12. malloc failed but calloc worked, why?

 

 
Powered by phpBB® Forum Software