Character space allocation

Discussion in 'C Programming' started by David Buck, Jan 12, 2006.

  1. David Buck

    David Buck Guest

    If I have a structure as follows;

    typedef struct {
    double x;
    double y;
    int size;
    char text[256];
    } text;

    I have allocated a space of 256 bytes for some text entered. If the text is
    less than 256 bytes, what happens to the rest of them.

    I am interested as I want to append other objects after the text in memory,
    and if the user only enters a few bytes, I only want to use that amount of
    data (rounded to the next word);

    if C automatically either writes the remaining bytes to a specific value, or
    currupts them, I will have to do this another way.

    --
    David Buck
    2D CAD for RISC OS at www.risccad.freeuk.com
     
    David Buck, Jan 12, 2006
    #1
    1. Advertising

  2. David Buck

    Chris Dollin Guest

    David Buck wrote:

    > If I have a structure as follows;
    >
    > typedef struct {
    > double x;
    > double y;
    > int size;
    > char text[256];
    > } text;
    >
    > I have allocated a space of 256 bytes for some text entered. If the text
    > is less than 256 bytes, what happens to the rest of them.


    Nothing. You asked for 256 characters; that's what you got. If you don't
    care to use them, well, that's fine.

    > I am interested as I want to append other objects after the text in
    > memory, and if the user only enters a few bytes, I only want to use that
    > amount of data (rounded to the next word);


    C doesn't let you randomly append objects to the end of data structures.

    > if C automatically either writes the remaining bytes to a specific value,


    No (not unless you allocate a static text).

    > or currupts them,


    No (but it won't stop /you/ corrupting them).

    > I will have to do this another way.


    Yes, if you only want to use "enough" space: point into larger character
    buffers or use mallocated space.

    --
    Chris "hey, dude, where's my train?" Dollin
     
    Chris Dollin, Jan 12, 2006
    #2
    1. Advertising

  3. David Buck

    David Buck Guest

    Chris Dollin wrote:
    > David Buck wrote:
    >
    >> If I have a structure as follows;
    >>
    >> typedef struct {
    >> double x;
    >> double y;
    >> int size;
    >> char text[256];
    >> } text;
    >>
    >> I have allocated a space of 256 bytes for some text entered. If the
    >> text is less than 256 bytes, what happens to the rest of them.

    >
    > Nothing. You asked for 256 characters; that's what you got. If you
    > don't care to use them, well, that's fine.
    >
    >> I am interested as I want to append other objects after the text in
    >> memory, and if the user only enters a few bytes, I only want to use
    >> that amount of data (rounded to the next word);

    >
    > C doesn't let you randomly append objects to the end of data
    > structures.
    >


    I didn't say random, nor to the end of data structures, all I said was after
    this object in memory. It would be another structure, but I will point it to
    the next word aligned address after the text - hence the question.

    >> if C automatically either writes the remaining bytes to a specific
    >> value,

    >
    > No (not unless you allocate a static text).
    >
    >> or currupts them,

    >
    > No (but it won't stop /you/ corrupting them).
    >
    >> I will have to do this another way.

    >
    > Yes, if you only want to use "enough" space: point into larger
    > character buffers or use mallocated space.


    Sounds like it will work OK.

    Thanks for the info - much appreciated.

    --
    David Buck
    2D CAD for RISC OS at www.risccad.freeuk.com
     
    David Buck, Jan 12, 2006
    #3
  4. David Buck

    Mike Smith Guest

    "David Buck" <> wrote in message
    news:dq5kqr$iv8$-infra.bt.com...
    > Sounds like it will work OK.
    >
    > Thanks for the info - much appreciated.
    >
    > --
    > David Buck
    > 2D CAD for RISC OS at www.risccad.freeuk.com
    >
    >


    You might be interested in article 2.6 in the FAQ

    http://c-faq.com/struct/structhack.html

    --
    Mike
     
    Mike Smith, Jan 12, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,909
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,668
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,020
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    729
    Michael Hudson
    Oct 24, 2003
  5. Replies:
    7
    Views:
    3,591
Loading...

Share This Page