So here's what I am trying to do; connect two arrays of characters together through using memcpy. Part of this is because I am using u_chars but for this example, we will just use characters and use strlen to get the length of a string for simplification's sake.
Essentially, the steps that steps I take are:
- Create a new pointer that will represent my combined strings (char* name)
- Pass the address of the pointer into a function (so a reference to a pointer) along with the two pointers that I want to add together
- Set a dereferenced uninitialised pointer to be the memory address of a new memory allocation of the size I want (this works fine)
- Copy the first set of data into this new location (this also works fine)
- Copy the second set of data into this new location, plus a few places that represent the lengthof the first set's data (this also works fine)
- Set the last character of this pointer's data to be a null-terminating character ('\0)
...and this is where everything dies. I get an access violation error when trying to write to location 0x00000000. However, the pointer definitely exists. Below is the code (written in Visual C++ using VS2013)
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * name;
char * firstpart;
char * lastpart;
void addstrings(char ** result, char * body, char * prefix)
{
int x = strlen(prefix);
int y = strlen(body);
*result = (char *)malloc((x + y + 1) * sizeof(char));
memcpy(*result, prefix, x);
memcpy(*result + x, body, y);
*result[x + y] = '\0'; // Exception is thrown writing to memory address 0x00000000
}
int _tmain(int argc, _TCHAR* argv[])
{
firstpart = "stack";
lastpart = "overflow";
addstrings(&name, firstpart, lastpart);
return 0;
}
The following replacement allows the program to continue, which on inspection shows me that the string exists
result[x + y] = '\0';
But this will not set any value; which is probably correct as "result" in this instance is still only the reference to the pointer and not the pointer itself (I want to modify the contents at the address of this pointer, which itself is a pointer, but I need to de-reference it by one level first).
To be clear; I know that there are ways around this (strcpy if I am using string.h) but this wouldn't work if I am using u_char types or something like this. Additionally, I could modify the above as follows, which works fine:
char * addstrings(char * result, char * body, char * prefix)
{
int x = strlen(prefix);
int y = strlen(body);
result = (char *)malloc((x + y + 1) * sizeof(char));
memcpy(result, prefix, x);
memcpy(result + x, body, y);
result[x + y] = '\0';
return result;
}
- Return type is now char *
- Takes a pointer to the output string rather than a reference to the pointer
- Removed all dereferencing in the function
- Returns the pointer
The function call now looks like:
name = addstrings(name, firstpart, lastpart);
And this will work fine. But I am just curious as to what the issue really is and why I could not assign a value to an element in the array that the pointer is pointing to (yet I can in the second example).
Any comments or thoughts are greatly appreciated,
Thanks!
char const *
instead ofchar *
whenever it is pointing to something that cannot, or is not going to be, modified. So in this code,body
,prefix
, and anything that points to a string literal. Also,size_t
is the proper type for the return ofstrlen