image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old October 3rd, 2003, 10:37 AM
Da_iMac_Daddy's Avatar
Not-so-Neo-DumbA$$
 
Join Date: Sep 2002
Location: Dover, Delaware USA
Posts: 423
Thanks: 0
Thanked 0 Times in 0 Posts
Da_iMac_Daddy is on a distinguished road
C/C++ pointers and strings

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
__________________
Nathan Coffield
Apple iBook;
AMD 3400 3GB of RAM, 250GB SATA HD, 19" Wide Screen Monitor, Windows XP Home, VMWare running Slackware 11 for development purposes, Apache 2.2, PHP 5;

Home Page
Reply With Quote
  #2  
Old October 3rd, 2003, 01:01 PM
Registered User
 
Join Date: Sep 2003
Location: Manchester, England
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
JohnE is on a distinguished road
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.
Reply With Quote
  #3  
Old October 7th, 2003, 09:23 AM
Da_iMac_Daddy's Avatar
Not-so-Neo-DumbA$$
 
Join Date: Sep 2002
Location: Dover, Delaware USA
Posts: 423
Thanks: 0
Thanked 0 Times in 0 Posts
Da_iMac_Daddy is on a distinguished road
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?
__________________
Nathan Coffield
Apple iBook;
AMD 3400 3GB of RAM, 250GB SATA HD, 19" Wide Screen Monitor, Windows XP Home, VMWare running Slackware 11 for development purposes, Apache 2.2, PHP 5;

Home Page
Reply With Quote
  #4  
Old October 7th, 2003, 09:44 AM
lurk's Avatar
Mitä?
 
Join Date: Mar 2002
Location: Land o' skeeterz
Posts: 2,076
Thanks: 0
Thanked 0 Times in 0 Posts
lurk is on a distinguished road
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...
__________________
Wenn ist das Nunstruck git und Slotermeyer? Ja!...
Beiherhund das Oder die Flipperwaldt gersput!
Reply With Quote
  #5  
Old October 8th, 2003, 07:28 PM
Captain Code's Avatar
Moderator
 
Join Date: Aug 2001
Location: Ontario, Canada
Posts: 3,108
Thanks: 0
Thanked 1 Time in 1 Post
Captain Code will become famous soon enough
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.
__________________
MacBook Pro 2.16GHz Core2Duo 3GB RAM, G4 1.4GHz OSX Tiger 1.25GB RAM, Dual 2GHz G5 OSX Tiger 2GB RAM (freakin shweet)
Athlon 64 Windoze XP for school work (programming) 1GB RAM
dferns@macosx.com
Reply With Quote
  #6  
Old October 9th, 2003, 10:28 AM
lurk's Avatar
Mitä?
 
Join Date: Mar 2002
Location: Land o' skeeterz
Posts: 2,076
Thanks: 0
Thanked 0 Times in 0 Posts
lurk is on a distinguished road
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
__________________
Wenn ist das Nunstruck git und Slotermeyer? Ja!...
Beiherhund das Oder die Flipperwaldt gersput!
Reply With Quote
  #7  
Old October 9th, 2003, 10:29 AM
lurk's Avatar
Mitä?
 
Join Date: Mar 2002
Location: Land o' skeeterz
Posts: 2,076
Thanks: 0
Thanked 0 Times in 0 Posts
lurk is on a distinguished road
Wow typo city today.

Sorry.
__________________
Wenn ist das Nunstruck git und Slotermeyer? Ja!...
Beiherhund das Oder die Flipperwaldt gersput!
Reply With Quote
  #8  
Old October 9th, 2003, 10:50 PM
wiz wiz is offline
Registered User
 
Join Date: Jul 2002
Location: Sol
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
wiz is on a distinguished road
*edited*

Last edited by wiz; October 10th, 2003 at 11:34 AM.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -5. The time now is 11:29 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.