Sign in to follow this  
DirkDaring

A Simple Char Buffer Problem

Recommended Posts

Hello once again! I'm writing a simple chat program using Windows GDI to test a server program I'm working on for my game. Turns out after months of working on the game I keep coming across basic stuff I never needed use before but should have learned in the first place. Since it's a Windows 32 application, I figured I'd use the WM_CHAR message to determine when and what character the user is typing, then put the character in the appropriate element in a char buffer, and display it immediatly to the screen so that the user see's what he's typing in real time. (Pretty much like a console) This can and does work just fine, but I sometimes still get a little confused about character array's. HERE'S THE PROBLEM: I declared my character array, then started inputing ascii codes into whichever element of the array I wanted (just to test how all this works). Problem is, all the other elements are filled with garbage if you don't initialize the array somehow, and of course I don't WANT to initialize the array because I want it to be a blank array so that I can fill it as I go along. I think I found a solution; I used the function "memset" to fill the array with zeros. Here's a simple sample (example) code: char buffer [10]; memset(buffer,0,sizeof(buffer)); //Clear all the garbage buffer [0] = 110; ///Just to experiment with putting stuff in the array buffer [1] = 105; ///These could be ascii codes returned from WM_CHAR cout << buffer << endl; This SEEMS to work, but I got this wacky feeling there is a more normal way to do what I'm trying to accomplishing. Any ideas? Thanks a million in advance as always! (By the way, the code sample is a simple console app, not my Win32 version, obviously)

Share this post


Link to post
Share on other sites
Why do you initialize the array? When you add a character at index, why not add the '\0' at index+1? That way, you know where the valid characters end and where the garbage begins. It'll also result in a valid string you can do stuff with later if you need.

Share this post


Link to post
Share on other sites
of course it's filled with garbage. when you allocate memory you just get a pointer to the head of the memory block. The memory block will contain whatever data the computer had there last time the memory was used.

couple options:

1) just maintain a separate integer that points to the last valid index in the array.

2) use a dynamic container, like std::vector or std::list. these grow as you insert more elements into the container.

-me

Share this post


Link to post
Share on other sites
Hey, be easy on me! I wasn't surprised by the garbage, just forgotton how to deal with it. Pew Wee, your talking about the NULL character, up to now (and still) I havn't completely understood it's significance. Now I know that it (at least) helps "cout" identify the end of the string and I'm guessing almost all other functions will need that null character too (right?). I inserted the null in my example program and it worked fine, so thanks a million for you! As far as Fizban is concerned, I like your ideas and I want to try them, any chance of a quick and easy sample code for each? Please? Pretty please? Thanks again!

Share this post


Link to post
Share on other sites
Filling the array with 0 using memset is a sane and normal thing to do (In C anyway).
cout will print characters from your array until it finds a zero.
The NULL/zero character is used to mark the end of an ascii string. By convention I guess.
Thats also why a char array must be 1 byte bigger than the ascii letters contained in it.
If you didnt fill the array with zeros and you didnt add a '\0' character at the end (index 2) and there did not happen to be a zero among the garbage, cout would continue to advance past the end of the array, and your program would eventually crash.
Possibly with a "Address violation" error.

Palidine's suggestion number 2 is worth an example

#include <iostream>
#include <string> // using string since its all about char
using namespace std;
...
string buffer;
buffer += 110;
buffer += 105;
cout << buffer << endl;


Here is one using vector

#include <vector>
#include <iostream>
using namespace std;

int main()
{
vector<char> buffer;
buffer.resize(10);

buffer[0] = 105;
buffer[1] = 110;

// cout dont know how to print vectors so im
// gonna print one char at a time using a loop
for(int i=0; i<10 && buffer[i] != '\0'; i++)
cout << buffer[i];
cout << endl;

return 0;
}




No garbage and no need to initalize the buffer.

Im sure there is more clever ways to print the contents of a vector but you get the point. The vector also support iterators to iterate through the elements, but thats not important here. This vector has a size of 10 from the beginning and it never grow or shrink. It also allow us random access to its elements through indexing. This example pretty much assemble what you already had.
The fact that this works tells me that the elements of the vector is initialized to zero by default. If C++ guarantee this behavior or not I dont know.

[Edited by - pulpfist on November 28, 2006 12:05:19 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this