Skip to main content
gamedev.net
🔒 Locked

char array question (newbie)

Started by Pfhorseti Oct 25, 2002 at 9:15 PM 5 replies 650+ views
Original Post
Pfhorseti
Pfhorseti
Hi, I have some basic questions about a char array that I use as a buffer. When I create the buffer: char buffer[512]; And later on I use the buffer to write data into it, How do I know if buffer[0] for example, contains data or some garbage? I need to know this so I know if the buffer is empty or contains some data. Thanks.
-forseti
Matt Calabrese
Matt Calabrese
That''s something you have to keep track of yourself, by your own means.
RuneLancer
RuneLancer
You can''t really tell if a variable contains junk or not. The only case where such a thing would be possible is if a few certain values are possible only. For instance, if you have 56 in an int that should only have values from 200 to 400, then you can be sure something went wrong somewhere.

You should always initialize variables before using them. Filling your char array with blank data would allow you to make sure it wouldn''t be corrupted unless your code has a bug which does corrupt it (such as a boundary violation or messing around with pointers and writing to the wrong memory location).

The easy way to clear your array would be...

memset(ArrayName, 0, sizeof(ArrayName));

This will fill whatever "ArrayName" points to (in your case, an array of 512 bytes) with "0" (0x00 as opposed to ''0'', the alphanumeric value). You also need to specify how large the section of memory you''re going to fill with 0''s is (hence the sizeof).

This way, when your program starts, your array will be clean and corruption-free. Unless the program has a bug which would leak stuff into your array without you wanting it to.
CodeJunkie
CodeJunkie
Well here''s an idea.

Store a speical set of characters at the beginning of the buffer that shows it is emtpy.

The easiest would be a ''\0'' end of line character to show that nothing is in the buffer. However, if you put info in the buffer that starts with that speical set of characters then the code will think the buffer is empty. So, let the user know not to use the speical set of characters at the start of the info put into the buffer.
Pfhorseti
Pfhorseti
Ah, thanks for the detailed explanation.

So if I want to check my array if it is empty or not, would this do the trick:

// zero the array with memset().
.
.
// read data into buffer.

if(buffer[0] == NULL)
// its empty.
else
// buffer[0] contains data.



Thanks.
-forseti
x-bishop
x-bishop
heres the low level explaination...im doin this just to see if i still remember my compiler construction

if anyone is familiar with compiler construction theyd know that when any variable is is declared, the compiler must determine the amount of space to reserve

without getting into details, i believe C reserves memory array space based on data alignment, whether or not its a static/dynamic/local/global or external or internal visible variables

anywhoo ..before array space is reserved, it has to be aligned, depending on the memory architecture of the platform then uses the RMB (reserve memory bytes i think) directive ..but this is true for globally defined variables,

for local variables, the space will be offsetted from the stack, and RMB wont work since it doesnt know where the stack will be to reserve memory there

in this case the EQU directive is used as well as the space being initialized to hold the expected values...DC.W maybe

so the moral is..space is reserved for it..but probably not initialized..especially in C...what you can do is test it out yourself...declare a variable in your mainline or in a function/class method and try and access it..using cout or something

if ive made any mistakes, someone please correct me



naher
naher
hi

all depend on how you use the buffer.

but the most of the time you will need more variables to indicate which position is the next available. (or you can use buffer[0] to do it, using it like an integer)

or you can use one invalid value to know it.

if you need to remove the char you read you need two vars, one indicating the first position occupied (int o) and another the last occupied (int u).
at the beginning o = 0 and u = 0. when you need add you will add in buffer[u] and later you must do u=(u+1)%512. if you need read you do it in buffer[o] and remove it doing o=(o+1)%512
if ( o==(u+1)%512 ) the buffer is full, and if(o==u) the buffer is empty, you must check it before add/read respectively. this work like a queue.
in this way you would be leaving one place of the buffer always empty, i dont remember exactly how to make complete use of the buffer, maybe using another counter that indicate how many elements there are in the buffer and the full/empty condition would be if( (o==u)&&(another_counter==512) ) buffer_full
if( (o==u)&&(another_counter==0) ) buffer_empty

i hope you understand my english, and this can help you.

// naher

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.