LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] C string concat question revisited

[ILUG] C string concat question revisited

Dave Neary dneary at eircom.net
Tue Jul 3 21:11:29 IST 2001


On Tue, Jul 03, 2001 at 10:51:16AM +0100, Dave Neary wrote:
> Conor Daly wrote:
> > Therin lies the rub.  I'm trying to be general here so I don't know the
> > length of the destination char *array* 's'.  I can find out the length of the
> > string *within* 's' using strlen() but I don't know of a function that will
> > return the amount of memory *allocated* to 's'.
> 
> If you haven't explicitly allocated memory to s then it's not going to
> have any memory allocated to it. If you're sure that there is some
> memory allocated to s then realloc() can be used to change the size to
> something else (assuming it succeeds). If it's uninitialised then
> there's no issue - just allocate whatever you need to it and away you
> go.

Having thought about what you wanted to do, I reckon this is the
best way if you don't mind calling malloc/realloc a lot - if you
do, just keep a track of how big a buffer is allocated for my_buf
as you go, and when you get near the end of it, realloc another
big chunk.

Cheers,
Dave.

char *my_buf = NULL;
char *next_string;

/* This is just to set up next_string to get some strings, and
 * append them to my_buf, which automatically expands to fit */
next_string = malloc(80);
while (fgets(stdin, next_string, 80))
{
  if (my_buf)
  {
    char *tmp = realloc(my_buf, strlen(my_buf) + 
                        strlen(next_string) + 1);
    if(tmp == NULL)
    {
      fprintf(stderr, "realloc failed - not modifying my_buf\n");
    }
    else
      tmp = my_buf;
   
    strncat(my_buf, next_string, strlen(next_string)+1);
  }
  else
  {
    my_buf = malloc(strlen(next_string)+1);
    if(my_buf == NULL)
    {
      fprintf(stderr, "Failed to allocate space for my_buf");
      return -1;
      /* Consider setting errno too */
    }
   
    strncpy(my_buf, next_string,
    strlen(next_string)+1);
  }
  printf("%s", my_buf); /* Check what's in there */
}

free(next_string);
free(my_buf);
-- 
  .------------------------------.
 /          David Neary,          \
|     E-Mail dneary at eircom.net     | 
 \     Phone +353-1-872-0654      /
  `------------------------------'




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell