| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Auto string-length deter |
G'Day Paul,
-=> Quoting Paul Wankadia to Frank Adam <=-
PW> What would I have to do? Use strdup() to duplicate the string locally
PW> and then sizeof() that duplicate string perhaps???
Shivers. Alloc(), strcpy(),sizeof(), and free() just to find the byte
length ?
I'm not sure if it'll work anyway, i think it just copies strlen(string).
Best way is to just send the old fashioned maxsize to the function, the
string may not always be allocated just for the input anyway.
Eg. What happens if..
char* GetString(char *s)
{
...
size = GetSize(s);
/* beuty, we can get (size - 1) chars if the user is willing. */
/* ... */
return s; /* let's say user entered 92 chars.*/
};
int main()
{
char mystr [100]; /* frank's line */
GetString(mystr);
strcat(mystr," - said Frank."); /* oh-oh 92+15 = buckle_up */
return 0;
}
Ok, then i would have to make sure in the calling function that the
returned string is big enough to manipulate further.
int main()
{
char mystr [100]; /* frank's line */
GetString(mystr);
if(GetSize(mystr)) > (100 - 16) ) /* did he rumble on again ? */
printf("out of string length");
else
strcat(mystr," - said Frank.");
return 0;
}
Now i've saved a sizeof(mystr) to send, but lost a sizeof call and an
if else statement on return, not to mention the extra work the getstr()
function had to do.
I know what you're trying to do, i worked on the same thing few months
back, that's exactly what made me do that function with heapwalk().
It's good fun, but i haven't used that code since.
PW> BTW how can I make a pointer to point to a set place in memory (i.e.
PW> segment and offset) ???
Take a look at MK_FP(), and also FP_OFF(),FP_SEG().
char far *x;
x = (char far*) MK_FP( (unsigned) seg, (unsigned) off );
if you know the actual address.
or..
x = (char far*) MK_FP( FP_SEG(var) , FP_OFF(var) );
C++ may also want a cast to unsigned on FP_SEG/OFF.
Btw MK_FP(), is not ISO, but there is a portable replacement in SNIPPETS.
Or you can always swipe VidMgr's #define for it, it looks close.:-)
FA> Perhaps if the string is global it may also work, but i didn't try or
FA> even said that !.:)
PW> What's wrong with global variables? :)
Nothing when used in moderation, that was just a preemptive strike in
case i was wrong..:)
FA> PS: if portability is not an issue look at heapwalk in Borland, or go
PW> What does heapwalk() do? (Forgive all these questions -- I only got
Heapwalk goes through the internal allocation table, and report the number
of bytes allocated to each variable and the state of the memory.
With a bit of coding it can return the REAL size of any variable.
I've posted a piece of code here few months back on that, but i can't
seem to find it just now.
Anyway it doesn't matter 'cause it was deemed to be highly non portable.
FA> alloced block size somewhere, it only seems to work with the Borland
FA> RTL..
PW> What's RTL? BTW I'd rather keep the code as portable as possible
PW> (between PC compilers anyway), so that's not an option really...
Run Time Library.
WRT to the other mssg, an M3 is a BMW, a car that is :-)
L8r Frank (fadam{at}ozemail.com.au).
___ Blue Wave/DOS v2.21
--- Gash
* Origin: The Software Parlour (3:635/544)SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/409 410 413 430 SEEN-BY: 711/808 809 932 934 712/515 713/888 714/906 800/1 @PATH: 635/544 50/99 711/808 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.