C/C++ pointers and strings

Da_iMac_Daddy

Not-so-Neo-DumbA$$
Can someone please use human language to tell me how to use pointers to make a string. Maybe even write a short little line of code that shows me how to recieve input for a string. All the books I'm reading seem pretty cryptic to me when it comes to pointers being used for strings.

Thanks in advance
 
There are 3 things to remember when using pointers with a string. The main thing is that your pointer needs to have a block of memory assigned to it - otherwise, your string will be stored at a random memory location and could easily damage some other data.

The second thing is that this block of memory needs to be big enough to hold the string PLUS an extra byte for a NULL terminator (you might need a couple of extra bytes if you're using Unicode or some other multi-byte character string). So - what happens if you don't know how big the string will be? Just allocate a big enough block to hold the maximum size you think it will probably be and hope for the best....!

The final thing to remember is that every block of allocated memory must be released once you've finished with it.

In C, memory is allocated using 'malloc()' and released using 'free()'. In C++ it is allocated using the 'new' operator and released using 'delete[]'. Note the square brackets. Here's an example in C assuming a simple character string......

Code:
#define MAX_STRING_SIZE 1024;
char* pStringBuffer = NULL;

pStringBuffer = (char *)malloc(MAX_STRING_SIZE);
/* 'malloc()' returns a pointer to void, so cast it as a pointer to char */

if (pStringBuffer)
{
   strcpy (pStringBuffer, "Hello World");

   /* Now do something with the string */

   /* and finally... */
   free (pStringBuffer);
}
And the same thing in C++......

Code:
#define MAX_STRING_SIZE 1024;
char* pStringBuffer = NULL;

pStringBuffer = new char[MAX_STRING_SIZE]; // Note the square brackets

if (pStringBuffer)
{
   strcpy (pStringBuffer, "Hello World");

   // Now do something with the string

   // and finally...
   delete[] pStringBuffer; // Note the square brackets again
}
Strictly speaking, the 'if' test is dangerous under C++ because unlike malloc(), 'new' is not guaranteed to return NULL if it fails. However, I've never come across a compiler where it didn't.

Incidentally, for most purposes it's easier to hold strings on the program stack. This is especially true if they're small and only being used temporarily - e.g.

Code:
char stringBuffer[] = "Hello World";
This automatically allocates the right amount of space and the memory doesn't need to be released afterwards. The drawback is that you need to make sure that you don't accidentally overwrite stringBuffer with a bigger string.

Hope that helps.
 
OK now is there anyway to get a compiled program to run in the background but still recieve input from the keyboard? Like is there anyway that I can pull data from the ps2 port (on a pc) without anyone noticing?
 
Buy a keyboard snooping dongle and put it between the keyboard and the box. In software the answer is generally no but depending on the Os and level of "not noticing" required you could do something...
 
There's a thing called an OS level hook that will do that on Windows(win32) but I haven't ever used it. It's not an easy thing to do, unless you've done a lot of win32 programming, but it's possible. There are many key logging programs for Windoze.

BTW, I say windoze because you say you want to log from a PS2 port, so I assume you want to run a program on the windoze computer.
 
That is true but I figured that that approach violated the "not noticing" part of the message. Before those APIs are availale to a program it has to regester itself with the windowing system and then it will show up in the list of running applications. It may be a background task but it will be there. (A similar thing is possible under OSX)

To avoid that situation you would need to most likely go down to the level of a device driver and I am not sure if they can do things like request arbitrary event notifications. I think not, so you basically have to put your code into the driver for the keyboard which is really hard.

One other problem with the OS hooks modle is that you are working at the level of UI events and are relying on the OS to deliver them to you. Now if some cases even if you have regestered to recieve all events the OS will not deliver them to you. In the case of X Windows a given window can request that it have a sevure connection with the server in which case those events are not published to just anyone who wants to listen. (As an example of this an xterm will let you request a secure keyboard connection.) I assume such a system would fail to snoop on the very keystrokes you were wanting to get. Or was this for honest purposes ;-)

-Eric
 
Back
Top