Results 1 to 4 of 4

Thread: malloc anyone?

  1. #1
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,126
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question malloc anyone?

    Hi all,
    Im trying to get a hnadle on malloc() but having some brian cramps over it.
    I want to stat a file, and assign the file size in a char type variable
    My program runs ok, and compiles without error but doesn tell me what in assigned in the pointer ptr. I named this program 2args
    Code:
    $ ./2args helloWelt.c 
    File size:                77 bytes
     allocated:
    Source:
    Code:
           #include <sys/types.h>
           #include <sys/stat.h>    //Allows for linux system Calls
           #include <stdlib.h>
           #include <fcntl.h>
           #include <stdio.h>
           #include <time.h>
    
    //####StatFucntion taken from man 2 page
    int main(int argc, char ** argv){
             struct stat sb;
            if (argc > 2 || argc == 1) {
                perror("Please include 1 and only 1 filename in the format:\n ./2args <filename> \n");
    	        exit(EXIT_FAILURE);
    	   }
    
               if (stat(argv[1], &sb) == -1) {
                   perror("stat");
                   exit(EXIT_FAILURE);
               }
    //Show the file size:
               printf("File size:                %lld bytes\n",
                       (long long) sb.st_size);
    		  
    
    //assign memory with malloc
    /* Allocate space for an array with elements of type
       char where char is assumed to be a byte. */
    char *ptr = (char *) malloc(sb.st_size * sizeof (char));
    if (ptr == NULL) {
    	printf("memory could not be allocated:\n");
        /* Memory could not be allocated, the program should
           handle the error here as appropriate. */
    } else {
        /* Allocation succeeded.  Do something.  */
        printf("%s allocated:\n", ptr);
        free(ptr);  /* We are done with the int objects, and
                       free the associated pointer. */
        ptr = NULL; /* The pointed-to-data  must not be used again,
                       unless re-assigned by using malloc
                       again. */
    }
    
      //Open the file next:
    
    FILE *fp; //File is a pointer to a file.
         if (( fp = fopen(argv[1],"r")) == NULL){
        	perror("Please Enter a valid filename");
            fp = fopen(argv[1], "a");
            //fprintf(fp, "%s\n ", "Hello World, Where there is will, there is a way.");
       exit(1);
    
      }
    
      fclose(fp) ; //put toys away
    
    return 0;
    }

    What am I doing wrong? I just want to verify the blocks assigned to ptr via malloc()
    ~Conradin~

  2. #2
    Join Date
    May 2007
    Location
    East Yorkshire, England
    Beans
    Hidden!

    Re: malloc anyone?

    Your printf is wrong. It's %p to print out the address of a pointer.
    Website | Blog | The Arch Hurd Project

    If you want to ask about something I posted, send a PM, as I don't watch many threads

  3. #3
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: malloc anyone?

    Also please watch your indentation. Your code is a pain to read.
    「明後日の夕方には帰ってるからね。」


  4. #4
    Join Date
    Feb 2009
    Beans
    1,469

    Re: malloc anyone?

    I'll add:
    - Don't cast the return value of malloc()
    - Use EXIT_FAILURE and EXIT_SUCCESS consistently
    - Be careful opening files. Just because fopen() failed doesn't mean the file doesn't exist; it could also be a permissions issue or a dozen other things. In any case, you certainly shouldn't assume you can write to a file if you can't read it.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •