0

I have been on this fow quite some time now and i dont seem to figure it out.

I have this code:

    unsigned char *src;
    int length = (parameterArray[i].sizeInBits/8) + 1; // check how long array should be
    unsigned char tmp[length]; // declare array

    memcpy(tmp, (char*)&parameterArray[i].valueU8, length); // in this case copy char to array
    src = realloc(src, strlen(src) + strlen(tmp)); // reallocate space for total string
    strncat(src, tmp, strlen(tmp)); // merge 

every time the code crashes on the reallocating part.

I have tried almost everything and nothing works. Please help

5
  • 1
    You don't say what your problem is but note that you should add 1 for a null terminator to the size you realloc. You'd also have problems if parameterArray[i].valueU8 isn't guaranteed to be null terminated. Commented Apr 10, 2013 at 9:11
  • What does not work? How is parameterArray defined? Commented Apr 10, 2013 at 9:11
  • sorry i edited my question Commented Apr 10, 2013 at 9:16
  • Show definition and any use of src prior to the call to realloc(). Commented Apr 10, 2013 at 9:20
  • i only declared it, unsigned char *src; Commented Apr 10, 2013 at 9:22

3 Answers 3

4

src is an unitialized pointer, and will hold a random memory address. The preconditions for realloc() state. from the linked reference page:

Reallocates the given area of memory. It must be previously allocated by malloc(), calloc() or realloc() and not yet freed with free(), otherwise, the results are undefined.

When using realloc() store the result to a temporary variable to avoid a memory leak in the event of failure.

Additionally, calling strlen() on src will also result in undefined behaviour. As first pointed out by mani tmp must be null terminated in order for strlen() and strcpy() to work correctly. The space calculated in the realloc() must be increased by one to allocate an additional char for the terminating null character.

Example code fix:

unsigned char tmp[length + 1];
memcpy(tmp, parameterArray[i].valueU8, length);
tmp[length] = 0;

unsigned char* src = NULL;
unsigned char* src_tmp = realloc(src, (src ? strlen(src) : 0) + strlen(tmp) + 1);
if (src_tmp)
{
    if (!src) *src_tmp = 0; /* Ensure null character present before strcat(). */
    src = src_tmp;
    strcat(src, tmp);
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1, Though the argument to realloc() may also be NULL, in which case realloc() acts like malloc().
Ok i know what you mean. If i want to use the chars for other purposes than printing it on the screen would i still be obligated to add a terminating null character?
@flexzican, almost all c string functions depend upon null terminatination.
2

As per your code of this line memcpy(tmp, (char*)&parameterArray[i].valueU8, length); you are trying to copy valueU8 which must be assigned with Null terminator. Otherwise it will crash in this line src = realloc(src, strlen(src) + strlen(tmp));

Comments

0

From man pages of realloc

Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().

and your src is an uninitialized pointer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.