NO

Author Topic: resizing a string  (Read 4381 times)

bergin

  • Guest
resizing a string
« on: November 07, 2009, 11:53:25 PM »
hi forum

am trying to create a log as a big string and then send it to a file; so appending a small string but dont know how large it will eventually be, so doubling the size whilst the program is running.

the code below gives this: 

Operands of '=' have incompatible types 'char
  • ' and 'void *'.


how should I alter?

thanks in advance,

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *resize_array(char *a, size_t new_size) {

char *save;
save = realloc(a, new_size);
if (save == NULL) {
fprintf(stderr, "Memory exhausted\n");
exit(EXIT_FAILURE);
}

return save;
}

int main(void) {

char size=10;
char a[size];
int i = 0;
strcat("hello ", a);

/* Double the size of the array */
size *= 2;
a =    realloc (a, size * sizeof (*a));
strcat("goodbye!", a);
printf("%s", a);

/* Destroy the array */
free(a);

return 0;
}

bergin

  • Guest
Re: resizing a string
« Reply #1 on: November 08, 2009, 12:54:48 AM »
have since found this:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void foo(char **string, char size)
{
*string = realloc(*string, size * sizeof **string);
if (*string == NULL) {
fprintf(stderr, "Memory exhausted\n");
exit(EXIT_FAILURE);
}

}


int main(void)
{
char size = 6;
char *string1;
string1 = malloc(size);
sprintf(string1, "Hello");

size *=2;
foo(&string1, size);
strcat(string1, " World");
printf("%s", string1);
}

« Last Edit: November 08, 2009, 01:03:01 AM by bergin »

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: resizing a string
« Reply #2 on: November 10, 2009, 10:43:53 AM »
hi forum

Operands of '=' have incompatible types 'char
  • ' and 'void *'.


how should I alter?

thanks in advance,

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *resize_array(char *a, size_t new_size) {

char *save;
save = realloc(a, new_size);
if (save == NULL) {
fprintf(stderr, "Memory exhausted\n");
exit(EXIT_FAILURE);
}

return save;
}

int main(void) {

char size=10;
char a[size];       // you have seen, you need dynamic memory "char *a = (char*)malloc(strlen("hello") + 1);"
int i = 0;
strcat("hello ", a);  // the content of a is undefined, so strcat is no good idea (sprintf as you use now is better) :)

/* Double the size of the array */
size *= 2;                              // I would calculate "size += strlen("goodbye!");"  
a =    realloc (a, size * sizeof (*a)); // you can not reallocate static memory
strcat("goodbye!", a);
printf("%s", a);

/* Destroy the array */
free(a);

return 0;
}

« Last Edit: November 16, 2009, 03:53:24 PM by AlexN »
best regards
 Alex ;)

bergin

  • Guest
Re: resizing a string
« Reply #3 on: November 16, 2009, 01:29:35 PM »
Yes Alex? I cant see your reply!

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: resizing a string
« Reply #4 on: November 16, 2009, 03:56:03 PM »
Yes Alex? I cant see your reply!
I can't see it also at the first view. :(

But I have added some comments to your source code. I have changed my last answer so you perhaps can see it. ;)
best regards
 Alex ;)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: resizing a string
« Reply #5 on: November 16, 2009, 09:18:33 PM »
Syntax:
char * strcat(char * restrict dst, const char * restrict src);
May the source be with you

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: resizing a string
« Reply #6 on: November 17, 2009, 07:14:52 AM »
Syntax:
char * strcat(char * restrict dst, const char * restrict src);

I want not to say the function is undefined, i want to say this usage is no good idea.
Code: [Select]
strcat("hello ", a);
There are two problems:
1.) "hello" is a constant string and you are not able to add something.
2.) a is pointer to a memory, but the content of this memory is undefined. So if you have the strcat-function with the correct argument order ( strcat(a, "hello") ) you also don't know what you get (Pelles C (or the malloc-function from the OS) seems to initialize the content with zeros, but there also can be a long serie of f.e. 0x2D - a mistake in one of my programs which was open up when I changed from Windows NT to Windows 2000). The function strcpy(a, "hello") or sprintf(a, "hello") as bergin used in his second example will be much better. ;)
best regards
 Alex ;)

Offline Robert

  • Member
  • *
  • Posts: 247
Re: resizing a string
« Reply #7 on: November 17, 2009, 12:05:20 PM »
 I have found that writing directly to disk is much faster than concatenating a string and then writing that string to disk. The hard drive cache is very efficient. YMMV.

Robert Wishlaw