0

I have:

char *data = malloc(file_size+1);

I also have strings such as char *string = "string1". I want to be able to realloc memory to data whenever I'm about to add another string to data. What would be the best way to do this in C, I've seen other ways such as using file_size * 2, however, this would be a waste of memory, what is the best way to do this when adding the length of a string to the memory. This is for a simple desktop application.

Nassim
  • 301
  • 3
  • 11
  • yes, I know that, but whats the best way to implement it using realloc. – Nassim Apr 24 '14 at 13:11
  • You have multiple solutions, like reallocating just what you need or, if you have to concatenate a lot and performances matter, you can double the allocated size everytime, or realloc a multiple of some numbers, like 128 for instance. – Chnossos Apr 24 '14 at 13:13
  • What's your notion of "best"? Smallest foot print? You know the answer to that. Less overall heap fragmentation? Less system requests for more pages? – Duck Apr 24 '14 at 13:14
  • Isn't there a way to add only enough memory as the string I am about to add? that would be the most efficient? – Nassim Apr 24 '14 at 13:14
  • Being 'the best' depends on the context. Do you want to optimize memory usage or speed, where is the code supposed to run etc. – Ingo Leonhardt Apr 24 '14 at 13:14
  • I forgot to take into consideration speed vs memory. For a simple application would it be best to just double the memory when ever more memory is needed? – Nassim Apr 24 '14 at 13:17

2 Answers2

2

It's very much up to what you want to optimize for, and that's hard to answer here since you don't say much about what "best" means for you, i.e. what the constraints are.

For a general-purpose program running on a desktop or server machine, I'd over-allocate a lot from the beginning, to avoid having to call realloc(), and typically double the allocation each time it overflows. The memory will probably not be used (physically) until needed anyway, so why try to "save" it?

If the allocation is going to be long-lived (days or weeks) it might make sense to try to be more conservative. If the number of concatenations is small, it might not. And so on.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • So your saying for a simple desktop application, its better to allocate more memory than have to realloc multiple times? – Nassim Apr 24 '14 at 13:18
  • @Nassim I would think so, yes. Of course I have no idea what "more memory" means. If it's over-allocating hundreds of megabytes, that might be an issue, but that would mean it has quite a large working space anyway. – unwind Apr 24 '14 at 13:21
  • 1
    @Nassim It depends on your requirements! What's important for your program, is it execution speed, RAM consumption, program size, readability/maintainability? You can't just say "what's best". – Lundin Apr 24 '14 at 13:45
0

You can use realloc to allocate more memory to an existing dynamically allocated buffer. Also, your pointers to string literals should be const qualified because they are read-only.

const char *string = "string1"
char *data = malloc(strlen(string) + 1); // +1 for the null byte

strcpy(data, string)

const char *string2 = "hello";
char *temp = NULL;

// +1 for the terminating null byte
int newsize = strlen(data) + strlen(string2) + 1;

// realloc returns NULL in case it fails to reallocate memory.
// save the value of data in temp before calling reallocate
temp = data;
data = realloc(data, newsize);

// check for failure of realloc
if(data == NULL) {
    printf("realloc failed\n");
    data = temp;
    // handle it
}
ajay
  • 9,402
  • 8
  • 44
  • 71