Memory reallocation problem

Hello guys,

ive just wrote some lines to reallocate a string for expansion, but i got nothin but an overflow.

My codes below



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char *val = "This is my string 1";

char *_value;

_value = (char *) malloc(sizeof val / sizeof *val);

_value = val;

char *app = ", hello world";

_value = (char *) realloc(_value, strlen(_value) + strlen(app); // failes


strcat(_value, app);



thanks in advance,

Luke
Last edited on
line 7 be breaking stuff. it leaks the memory you just got, and worse: you can't realloc a const char* (line 1).

unless you need your strings to be exactly sized to content (say, you have very little system memory on your wristwatch?) you should use char arrays of a fixed size (say 200 or whatever characters, consider a typedef).
Last edited on
ok, ive just changed line 7 to strcpy(_value, val), but im still failin
now, check the actual # of bytes from line 5.
I think sizeof isnt doing what you think its doing.
you want strlen(val)+ something, and do not forget to leave space for the terminal zero.

what, exactly, is your goal?
Last edited on
In your example sizeof(val) is the size of a char* pointer, not the string length!

If you want to allocate a buffer large enough to hold a string, use strlen() to determine the length:
1
2
3
4
5
6
7
8
9
char *buffer = (char*) malloc(strlen(str) + 1);
if (buffer != NULL)
{
    strcpy(buffer, str);
}
else
{
    /* failed to allocate memory! */
}


Note: Make sure to allocate one extra slot for the NULL terminator that strlen() doesn't count!

Also note that malloc() can fail, so always check result of malloc() for NULL value!

_____________


...or simply use the strdup() function, which copies string into newly allocated buffer:
1
2
3
4
5
6
7
8
9
char *buffer = strdup(str);
if (buffer != NULL)
{
    /* success */
}
else
{
    /* failed to copy string! */
}
Last edited on
kigar64551 wrote:
...use the strdup() function

I've never heard of that function before, most likely because it is POSIX (non-Windows).

At one time it was part of the "dynamic memory TR", and so required some testing to see if the implementation had it.
https://en.cppreference.com/w/c/experimental/dynamic/strdup

Now it is part of C23 (Visual Studio only goes to C17 at this time).

To work with VS use _strdup, even in C legacy mode (the VS default).
'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _strdup.


BTW, that's one cool C string function. :)
As an aside:

Not specified in Standard C or POSIX, but available on Linux (glibc) as well as *BSD:

The functions asprintf() and vasprintf() are analogs of sprintf() and vsprintf(), except that they allocate a string large enough to hold the output including the terminating null byte, and return a pointer to it via the first argument.
1
2
int asprintf(char **strp, const char *fmt, ...);
int vasprintf(char **strp, const char *fmt, va_list ap);


There is an implementation for MSVC available too:
https://pastebin.com/FfcGfrL5
Last edited on
I think I'll stick with C/C++ functions that cppreference has documentation on, thankyouverymuch. :D

Personally I'll work with C++ std::string first and foremost. And when I need some C string mangling I'll make do with the ol' stand-by stuff of strcpy, etc. :)
Topic archived. No new replies allowed.