Thread: problem in using popen calloc

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    gandhinagar
    Posts
    7

    Unhappy problem in using popen calloc

    FIRST CODE
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    int main()
    {
            int n=1;
            char *cmd="pwd";
            char *buffer=(char *)calloc(sizeof(char),256);
            FILE *f=(FILE *)popen(cmd,"r");
            printf("\nOutput\n");
            while(fgets(buffer,sizeof buffer,f))
            {
                    puts(buffer);
            }
            fclose(f);
            return 0;
    }
    Output
    /ho
    me/
    smi
    t/t
    emp

    TRY 2 :
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    int main()
    {
            int n=1;
            char *cmd="pwd";
            char buffer[256];
            FILE *f=(FILE *)popen(cmd,"r");
            printf("\nOutput\n");
            while(fgets(buffer,sizeof buffer,f))
            {
                    puts(buffer);
            }
            fclose(f);
            return 0;
    }
    Output

    /home/smit/temp

    What is the diff between buffer[256] and *buffer=(char *)calloc(sizeof(char),256); ?
    and why new line in output ??
    Last edited by Salem; 02-20-2011 at 01:49 PM. Reason: Added [code][/code] tags - learn to use them YOURSELF

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's a shame you couldn't have figured out the code tags at the same time you figured out the colour and size tags to such awful effect.

    Edit:
    Another half-job.
    << !! Posting Code? Read this First !! >>
    The code is still unreadable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For starts you're using calloc incorrectly...
    It's calloc( NumberOfElements , SizeOfEachElement );
    And there's no reason to typecast it's returned pointer.

    The difference between char buffer[256] and calloc(sizeof(char),256) is that one will work and the other won't.
    Last edited by CommonTater; 02-20-2011 at 10:52 AM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Actually, I do not believe that's correct, because calloc allocates an array of n elements of x bytes each -- and an array is guaranteed to be contiguous in memory -- reversing the order of the arguments does not affect the return value of the calloc call. Not that I'm saying one should ignore providing the arguments in the proper order, mind you; just that it will actually work.

    And the differences between char buffer[256] and calloc(sizeof(char),256):

    char buffer[256] allocates 256 contiguous bytes in stack memory local to the block in which it's created, which will "disappear" when execution leaves the block. It is also can contain any data whatsoever after it's been created; it is not initialized to any particular state by definition (although I'm pretty sure a global array created in the matter will be initialized to all 0's; I always initialize my arrays so as to remove any doubt).

    calloc(sizeof(char),256), or more properly calloc(256, 1) -- the size of a char is by definition is always 1 -- allocates 256 contiguous bytes in heap memory, sets all bytes to 0s and returns a pointer to the first element of this array of bytes. This memory remains available until the pointer returned is free'd when it is no longer needed, which must be done in order to avoid a memory leak.

    /cue the language lawyers
    Last edited by rags_to_riches; 02-20-2011 at 01:26 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It is somehow ironic that the only assignment which could do with a cast, doesn't have a cast.

    As for the difference, work out what sizeof on a pointer and on an array mean.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Location
    gandhinagar
    Posts
    7
    include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    char *cmd="pwd";
    char *buffer=calloc(256,1);
    FILE *f=(FILE *)popen(cmd,"r");
    printf("\nOutput\n");
    while(fgets(buffer,sizeof buffer,f))
    {
    puts(buffer);
    }
    fclose(f);
    free(buffer);
    return 0;
    }


    I made some changes but the output remains same.

    Output
    /ho
    me/
    smi
    t/t
    emp

  7. #7
    Registered User
    Join Date
    Feb 2011
    Location
    gandhinagar
    Posts
    7
    calloc function returns void pointer and buffer is character pointer therefore I have put (char *) in line

    char *buffer=(char *)calloc(256,1);

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by smitpatel24 View Post
    calloc function returns void pointer and buffer is character pointer therefore I have put (char *) in line

    char *buffer=(char *)calloc(256,1);
    Unnecessary... a void pointer can be assigned to anything.

    Just out of curiosity can you post the source file (pwd) you're using?

    Code:
    while(fgets(buffer,sizeof buffer,f))
    buffer is a pointer... on 32 bit systems that's usually 4 bytes... sizeof buffer is going to return 4, the size of the pointer, not the 256 bytes you've allocated.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define BUFFSIZE 256
    
    int main()
    {
       char *cmd = "pwd";
       char *buffer = calloc( BUFFSIZE, 1 );
       FILE *f = popen( cmd, "r" );
       printf( "\nOutput\n" );
       while( fgets( buffer, BUFFSIZE, f ) )
       { 
           puts( buffer );
          }
       fclose( f );
       free( buffer );
       return 0;
    }
    And PLEASE use code tags when posting ... click the octothorpe on the editor's toolbar and paste your code between the resulting bbscode tags.
    Last edited by CommonTater; 02-25-2011 at 01:36 PM.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by CommonTater View Post
    Just out of curiosity can you post the source file (pwd) you're using?
    What source file? It's a command. popen() starts a process, not read a file. pwd happens to print the current working directory.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    while(fgets(buffer,sizeof buffer,f))
    You don't know difference between array and pointer.
    sizeof cannot tell you the sizeof the memory block that pointer is pointing.
    It just tells you sizeof (buffer) = sizeof(char*) = 4(for your case)
    That's why you are getting the result.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Best practice is probably to take CommonTater's code, but instead of using your own BUFFERSIZE variable, use the BUFSIZ that's defined for you in stdio.h:

    Code:
    #define BUFSIZ  1024        /* size of buffer used by setbuf */
    For more info, see here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 12:55 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

Tags for this Thread