Thread: Possible to create char string at compile time?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Possible to create char string at compile time?

    I would like to have a program with a 5 mb string embedded in it kind of like this:
    Code:
    const char *c;
    c = (const char*)"BEGIN_STRING_HERE, (5 MB of arbitrary values here)";
    Is it possible to generate this string at compile time? Maybe with a macro? I'm trying to avoid a source file with a 5 mb string in it.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In general, no, it is not possible with out making use of features specific to your compiler, linker, or executable file format.

    Firstly, data embedded in an executable is normally directly reflected, in some way, in the source code (it has to come from somewhere). Secondly, programs execute in an environment with constrained resources (e.g. stack), and 5MB of such data would exceed those resources.

    Why do you want a 5MB string embedded in an executable? What are you trying to achieve by doing that?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by grumpy View Post
    Why do you want a 5MB string embedded in an executable? What are you trying to achieve by doing that?
    I am using the string to hold a file image. I am moving the image into the program after it has been compiled.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Which toolchain are you using?
    There's usually a way of adding "resources" to program images in one form or another.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-12-2013, 10:11 AM
  2. Replies: 6
    Last Post: 04-26-2012, 03:23 PM
  3. how to create and compile c programs
    By David_Baratheon in forum C Programming
    Replies: 13
    Last Post: 02-02-2012, 03:21 PM
  4. Length of string at compile time?
    By TriKri in forum C Programming
    Replies: 14
    Last Post: 05-23-2010, 11:16 AM