[NTLUG:Discuss] C pointer questions

Rusty Haddock rusty at fe2o3.lonestar.org
Wed Nov 27 17:33:46 CST 2002


Paul Ingendorf wrote:
    >It does work I've used it many a time.  If you have a better way of
    >doing it 

Sorry Paul, but it worked by luck.  You obviously had more data area
after the "This is" string otherwise the strcat would have written right
off the end of the already allocated string that 'b' is pointing to.
This isn't Java or BASIC or some other language that automatically
extends your strings when you add something to the end.  What saved your
butt may not have been your own doing but part of the linked in runtime.
Put your code on an embedded micro, where every byte counts, and I can
guarantee it will not produce the "expected" results especially of the
strings that a and b point to are put into ROM.

The main point being is that your code snippet has not allocated the
space (strlen(a) + strlen(b) + 1 /* nul */) inwhich to put the result
of strcat(b,a).  Again, just because you assign the resulting pointer
from strcat() to 'b' doesn't mean you have the space for it.  See the man
page for strcat() or look at the source code to the string(3) functions
to see how they work.

    >please share.  Otherwise your comment about it being bad technique
    >is out of line.

Tis not out of line whether I share how it should be done or not.
The fact is, it's incorrect code.  You don't know what was located
after the string "This is" and could have blown away some previously
calculated results or lucky enough to overwrite memory that has yet
to be used.  Nonetheless, here's kinda what you should be doing:

	char *a = " a test.",
	     *b = "This is",
	     *c;
	
	c = malloc(strlen(a) + strlen(b) + 1);
	/* Test for error result from malloc(); */

	/* If we got the space... */
	strcpy(c, b);
	strcat(c, a);

	printf(c);	/* May need a newline, but tis just a detail. */

	free(c);	/* Return memory back to system when you're done. */


Now, I don't care for malloc() too much -- too much hassle.  If I can
get away with it, a pre-allocated character array will work if I can
guarantee the result will not overflow it.  Consequently, an array
declaration like this:

	char c[BUFSIZ];	/* Usually defined in stdin.h but value varies w/ system. */

removes the need for the malloc().  Then you just call the strcpy()
and strcat() as above, possibly with a cast before the first arguments,
depending on if your compiler is picky or not.

    >line.  If you think it won't work then try it in a gcc compiler
    >before telling how it won't.

Just because it works on some (or even all compilers) doesn't mean
what you've coded is correct.  It may work... but you'll probably be
lucky.  Compilers for workstations and larger tend to have a large
overhead in their runtimes and thus kept your code from crashing.

	-Rusty-

    >Quoting Rusty Haddock <rusty at fe2o3.lonestar.org>:
    >
    >> Paul Ingendorf wrote:
    >>     >>From what I read in the question you are looking for the strings
    >> functions
    >>     >
    >>     >#include <string.h>
    >>     >
    >>     >char *a = " a test.", *b = "This is";
    >>     >b = strcat( b, a );
    >>     >printf(b);
    >>     >
    >>     >Should print:
    >>     >This is a test
    >> 
    >> It's very doubtful this would work.  You're causing strcat() to exceed
    >> the boundaries of what "b" is pointing to unless there's just happens
    >> to be some user data area after the "This is" string.  This should
    >> prevent the exception signal in many environments.  In others, the
    >> string constants above could be linked to read-only memory and then
    >> you're really up string creek. :-)
    >> 
    >> strcat() does not allocate new memory for its result. The assignment
    >> of the return value of strcat() is rather superfulous.  The man page
    >> for strcat(3), which isn't more than a large screen-full, explicitly
    >> states this.  Nonetheless, this is bad technique.
    >> 
    >> 	-Rusty-
    >> -- 
    >>    _____        Rusty Haddock  =  KD4WLZ  =  rusty at fe2o3.lonestar.org
    >> |\/   o \   o
    >> |   (  -<  O o   The day Microsoft makes something that doesn't suck
    >> |/\__V__/           is the day they start making vacuum cleaners.
    >> 
    >> _______________________________________________
    >> https://ntlug.org/mailman/listinfo/discuss
    >> 
    >
    >
    >
    >-- 
    >-->> mailto:pauldy at wantek.net
    >-->> http://www.wantek.net/
    >Running ....... Cos anything else would be a waste...
    >`:::\'                  .......  ......
    > :::  *                  `::.    ::\'
    > ::: .::  .:.::.  .:: .::  `::. :\'
    > :::  ::   ::  ::  ::  ::    :::.
    > ::: .::. .::  ::.  `::::. .:\'  ::.
    >:::.....................::\'   .::::..
    >
    >_______________________________________________
    >https://ntlug.org/mailman/listinfo/discuss

-- 
   _____        Rusty Haddock  =  KD4WLZ  =  rusty at fe2o3.lonestar.org
|\/   o \   o
|   (  -<  O o   The day Microsoft makes something that doesn't suck
|/\__V__/           is the day they start making vacuum cleaners.




More information about the Discuss mailing list