with respect to the memory

Discussion in 'C Programming' started by jesjak, May 23, 2007.

  1. jesjak

    jesjak Guest

    thanks a lot Lew Pitcher.

    one more doubt i have it is: we are declaring a structure and the
    memory for that structure will be allocated only when i creat some
    objects to that one right?
    so where the momory will be allocated for structure signature and for

    i will be thankfull if you or someone clarify this one too

    jesjak, May 23, 2007
    1. Advertisements

  2. That's going to depend upon the compiler.

    No space needs to be allocated for the "structure signature"
    (whatever that is exactly): the offsets of structure members
    are constants, so all of the code to access them can be compiled
    right into the executable.

    Objects are allocated in whatever kind of storage you declared
    for them -- static, auto, dynamic. The exact (or even relative)
    location of these types of memory in an executable is compiler dependant.
    Walter Roberson, May 23, 2007
    1. Advertisements

  3. jesjak

    Lew Pitcher Guest

    Hash: SHA1

    You're welcome.
    You have the concept correct.
    You have asked two questions here. Lets separate them so that the answers will
    be clear.

    Q1: Where will the memory be allocated for the structure definition?
    Q2: Where will the memory be allocated for a specific declaration of an object
    of a particular structure?

    Question 1 is fairly simple: there is no memory allocated to a structure
    definition. Period.

    Question 2 is a little more complicated, and can be answered in several ways.
    The pedants here might take exception to my answer, and they will provide
    better, more accurate responses.

    a) It depends. More specifically, it depends on whether you declare the object
    as a static, an auto, or an extern, and it depends on whether or not you
    provide an initializer for the contents of the object.

    b) C makes no distinction between BSS and DATA. In fact, those terms have
    nothing to do with C as a language. They /do/ have to do with the
    implementation of your local C compiler on your local system, though. The best
    we can tell you is that if you declare the object as 'static', the language
    guarantees that the object will reside in the space set aside for 'static'
    objects, and if you declare the object as 'auto', then the object will reside
    in the space set aside for 'auto' objects. These two spaces /may/ be the same
    physical space, or may be different physical spaces - that's beyond the
    definition of the language.

    - --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    - ---------- Slackware - Because I know what I'm doing. ------

    Version: GnuPG v1.4.7 (GNU/Linux)
    Comment: Armoured with GnuPG

    -----END PGP SIGNATURE-----
    Lew Pitcher, May 23, 2007
  4. I'm going to quibble about the phrase "the space set aside". There
    needn't be any space specifically set aside for either static or auto

    All we can really say is that static objects exist during the entire
    execution of the program, and auto objects exist only during the
    execution of the enclosing compound statement.

    (A compiler may vary this as long as any such variation has no visible
    effect; for example, a declared object might not be stored anywhere if
    the program never access it, or an auto object may be stored as if it
    were static if the compiler can prove that the enclosing function is
    never called recursively. The latter, as far as I know, is not a
    likely thing for a compiler to do, since it doesn't really save time
    or space.)
    Keith Thompson, May 23, 2007
  5. jesjak

    CBFalconer Guest

    See the following sig. Without proper quoting and linking to
    previous messages this is totally meaningless. At any rate, memory
    disposition etc. is not a C language matter, and is totally
    off-topic here.

    If you want to post a followup via groups.google.com, ensure
    you quote enough for the article to make sense. Google is only
    an interface to Usenet; it's not Usenet itself. Don't assume
    your readers can, or ever will, see any previous articles.
    More details at: <http://cfaj.freeshell.org/google/>
    CBFalconer, May 24, 2007
  6. Actually, I have worked with a compiler which did just that.
    I must admit that the compiler was not fully conforming (because
    functions could not be used recursively without some extra decoration),
    but the compiler would give all objects with automatic storage duration
    a fixed address.
    To conserve memory, would two objects that could never be alive at the
    same time be allocated to overlapping memory areas.

    This was a compiler for a small embedded platform (addressable memory
    was 64kB)

    Bart v Ingen Schenau
    Bart van Ingen Schenau, May 29, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.