[comp.lang.c] Q: Realloc of function parameter problem.

SLORES@umiami.miami.edu (Stanislaw L. Olejniczak) (11/27/89)

I am trying to write a function which would, if neccessary, change the
size of the string pointed to by one of the arguments, and return the
pointer to the "new" string.  It is part of a larger function.  I seem
to have a problem with realloc...  I tried finding an answer in just about
all the C books I have, but I simply cannot figure out WHY is it giving me
problems.  If you could tell me HOW to do this correctly, I would be most
grateful.  Any assistance would be welcome.

code:
======================================================================
char *substr( char *, int, int, char **);

main()
{
	char *string="This is a test",
		 *subs="";
	int start = 3,
		numchar = 5;

	subs =  substr (string, start, numchar, &subs);
}

char *substr (char *string, int start, int numchar, char **substr)
{
   /*I get a variety of errors on the following statement, depending on a
     compiler/linker.  The code bombs here,  I guess because memory
     management is being damaged*/

    *substr = (char *) realloc ( (unsigned) (numchar + 1), *substr);
}


----
Stan Olejniczak               Internet:         slores@umiami.miami.edu
University of Miami, FL USA   UUCP: (temp void) gould!umbio!solejni
SLORES@UMIAMI.BITNET          UUCP: (?)         umigw!gables!slores
Voice: (305) 547-6571         FAX: (305) 548-4612  
My opinions cannot possibly represent the views of anyone else!

cpcahil@virtech.uucp (Conor P. Cahill) (11/27/89)

In article <3752@umiami.miami.edu>, SLORES@umiami.miami.edu (Stanislaw L. Olejniczak) writes:
>     *substr = (char *) realloc ( (unsigned) (numchar + 1), *substr);

First, you must RTFM.  The order of arguments to realloc is:

		realloc(ptr,size);
		char * ptr;
		unsigned size;

Second, although not explicitly stated in the manual, the pointer passed
to realloc must be a ptr that was returned by a previous malloc.  Your
example was passing a pointer to an automatic variable.


-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

chittamu@umvlsi.ecs.umass.edu (Satish Kumar .C) (11/27/89)

In article <3752@umiami.miami.edu> SLORES@umiami.miami.edu (Stanislaw L. Olejniczak) writes:
>I am trying to write a function which would, if neccessary, change the
>size of the string pointed to by one of the arguments, and return the
>pointer to the "new" string.  It is part of a larger function.  I seem
>to have a problem with realloc...  I tried finding an answer in just about
>all the C books I have, but I simply cannot figure out WHY is it giving me
>problems.  If you could tell me HOW to do this correctly, I would be most
>grateful.  Any assistance would be welcome.
>

What compiler are you using? I tried it under gcc and the Ultrix cc. It
worked fine. Ofcourse I made one small change. Your last parameter to the
function substr was also named substr. I changed the parameter to a
different name. Could it have something to do with that?

===Code===
char *substr( char *, int, int, char **);

main()
{
	char *string="This is a test",
		 *subs="";
	int start = 3,
		numchar = 5;

	subs =  substr (string, start, numchar, &subs);
}

char *substr (char *string, int start, int numchar, char **subs)
{
   /* The following statement compiled fine under gcc and pcc */

    *subs = ( (char *) realloc ( *subs,(unsigned) (numchar + 1)));
}


=========
-- 
	-Satish.
	chittamu@cs.umass.edu
--
The Theory of Objectivity: E = MC++

brianh@hpcvia.CV.HP.COM (brian_helterline) (11/28/89)

>I am trying to write a function which would, if neccessary, change the
>size of the string pointed to by one of the arguments, and return the
>pointer to the "new" string.  It is part of a larger function.  I seem
>to have a problem with realloc...  I tried finding an answer in just about
>all the C books I have, but I simply cannot figure out WHY is it giving me
>problems.  If you could tell me HOW to do this correctly, I would be most
>grateful.  Any assistance would be welcome.

>code:
>======================================================================
>char *substr( char *, int, int, char **);

>main()
>{
>	char *string="This is a test",
>		 *subs="";
>	int start = 3,
>		numchar = 5;
>
>	subs =  substr (string, start, numchar, &subs);
>}
>
>char *substr (char *string, int start, int numchar, char **substr)
>{
>   /*I get a variety of errors on the following statement, depending on a
>     compiler/linker.  The code bombs here,  I guess because memory
>     management is being damaged*/
>
>    *substr = (char *) realloc ( (unsigned) (numchar + 1), *substr);
                                   ^^^^^^^^^^^^^^^^^^^^^^^   ^^^^^^
				   I believe you need to switch these.
				   At least for MSC 5.1, you specify
				   the buffer first, followed by the size.
>}
>
>
>----
>Stan Olejniczak               Internet:         slores@umiami.miami.edu
>University of Miami, FL USA   UUCP: (temp void) gould!umbio!solejni
>SLORES@UMIAMI.BITNET          UUCP: (?)         umigw!gables!slores
>Voice: (305) 547-6571         FAX: (305) 548-4612  
>My opinions cannot possibly represent the views of anyone else!
>----------

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/28/89)

In article <3752@umiami.miami.edu> SLORES@umiami.miami.edu (Stanislaw L. Olejniczak) writes:
>I seem to have a problem with realloc...

You must realloc() only storage previously obtained via malloc(), calloc(),
or realloc().  String literals are not among these.