Character space allocation

D

David Buck

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.
 
C

Chris Dollin

David said:
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.
 
D

David Buck

Chris said:
David said:
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.
No (not unless you allocate a static text).


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


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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top