#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    England, UK
    Posts
    35
    Rep Power
    13

    size of executable


    Hi, I've just written my first few apps and they seem to be quite big. One for instance is 105kb, when all I'm including is <string>
    and <fstream> (the source file itself is only 40 or so lines). Is this just how it is or can the compiler somehow remove unneeded functions? I'm using dev-c++.

    While I'm here, I want to change a files extension so I've done this:
    Code:
        while(file_name[a] != '.'){
            out_file[a] = file_name[a];
            a++;
        }
        out_file[a] = '.';
        a++;
        out_file[a] = 't';
        a++;
        out_file[a] = 'p';
        a++;
        out_file[a] = 'l';
        a++;
        out_file[a] = '\0';
        a++;
    which seems a little odd, is there a tidier way. BTW I come from PHP and pointers scare me so I've no idea how I even managed that, so go easy.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    USA
    Posts
    286
    Rep Power
    13
    Are you building debug binaries (bigger) or release binaries (smaller)?
    Jon Sagara

    "Me fail English? That's unpossible!"
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    England, UK
    Posts
    35
    Rep Power
    13
    normal, it comes out at 280 with debug on.

    I also have the optimize options on ( -g3 -fexpensive-optimizations)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    include string and fstream will bloat the code considerably. These include files typically contain most of the code necessary to implement the class.

    You can also simplify the code, by using C functions instead of C++ classes:

    Code:
    #include <string.h>
    #include <stdlib.h>
    
    char* change_extension(char* filename) {
      
      char* outfile;
      char* extension;
    
      outfile = (char *)calloc(1, strlen(filename) + 3);
      strcpy(outfile, filename);
      extension = strchr(outfile, '.');
      if (extension)
        strcpy(extension, ".tpl");
      else
        strcat(outfile, ".tpl");
    
      return outfile;
    
    }
    The standard C libraries have a great set of functions for basic string manipulation and navigation. I usually find them to be easier to use than the C++ equivalents, with the exception of the search and replace capabilities of C++ strings.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    England, UK
    Posts
    35
    Rep Power
    13
    Thanks, I think I just about get that piece of code you posted after some thought.

    This line:
    Code:
    outfile = (char *)calloc(1, strlen(filename) + 3);
    I understand to be allocating some space for the filename and adding some incase there's no extension (would that not need 4 then?)

    As to my first question, I did think about maybe trying C headers instead although I will need to read up on them (I tried changing to string to string.h and my compiler was not happy). All I'm doing with string is holding the line from the in file, checking it and then sending it out so maybe I can do something else and use C file I/O instead and see if that comes out smaller. Cheers then I'll give that a go.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    You are right about the size: 4 bytes, not 3.

    The calloc function, as you indicate, allocates memory. It also initializes all members to 0, which can save problems. The parameters to calloc are the number of records and the size of those records.

    Elsewhere in your program (but not in this function), you'll want to free() the memory after you're done with it, to prevent memory leaks.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo