The problem here is with
char Tmp[]="";
here, Tmp is not an dynamic array, it's an array with exactly one element (initialized via the initializer). When you use this as destination for strcpy(), you're overrunning the buffer. Basically, your code tries to access out of bound memory, which invokes undefined behavior.
Related, quoting C11, chapter §7.24.2.3, strcpy(), emphasis mine
The
strcpyfunction copies the string pointed to bys2(including the terminating null character) into the array pointed to bys1. [...]
and then, for "String function conventions", §7.24.1,
[....] If an array is accessed beyond the end of an object, the behavior is undefined.
Solution: You need to ensure that Tmp has enough memory to hold the content to be copied into it, including the null terminator.
That said, for a hosted environment, int main() should at least be int main(void) to be conforming to the standard.
The problem here is with
char Tmp[]="";
here, Tmp is not an dynamic array, it's an array with exactly one element (initialized via the initializer). When you use this as destination for strcpy(), you're overrunning the buffer. Basically, your code tries to access out of bound memory, which invokes undefined behavior.
Related, quoting C11, chapter §7.24.2.3, strcpy(), emphasis mine
The
strcpyfunction copies the string pointed to bys2(including the terminating null character) into the array pointed to bys1. [...]
and then, for "String function conventions", §7.24.1,
[....] If an array is accessed beyond the end of an object, the behavior is undefined.
Solution: You need to ensure that Tmp has enough memory to hold the content to be copied into it, including the null terminator.
That said, for a hosted environment, int main() should at least be int main(void) to be conforming to the standard.
Allocate memory to Tmp before copying the string to Tmp.
Tmp is an character array of size 1.
Allocate memory dynamically as shown below. (If you are trying to do dynamic memory allocatio)
char *Tmp = malloc(strlen(sNext1) + 1);
Then try doing strcpy()
c++ - Use Strcpy_s to Copy Dynamically Allocated Char Array - Stack Overflow
c - Strcpy a static char array into a dynamically allocated char array to save memory - Stack Overflow
Using strcpy with a string array in C - Stack Overflow
c++ - strcpy function with dynamic allocated char array - Stack Overflow
Videos
Praetorian hit it on the head. "The important rule you're missing is use std::string". Old C functions like strcpy_s are notoriously incredibly unreliable and that's the whole point of not doing it anymore. So don't do it. Use std::string.
The above code snippet result in "Debug Assertion failed" run time error.
strcpy_s(p, len, temp); //Expression:(L"Buffer is too small" &&0)
So the answer is strcpy_s(p, len+1, temp); will work fine in your case.
That's not a character array; that's an array of character pointers. Remove the * to make it a character array:
char c[20];
Then you can use strcpy:
strcpy(c, "undefined");
If you really did want an array of character arrays, you'll have to do what you said you tried:
// array that holds 20 arrays that can hold up to 70 chars each
char c[20][70];
// copy "undefined" into the third element
strcpy(c[2], "undefined");
The problem could have been you're missing the closing ", I don't know if that was a paste error though. Or, the problem could have been that you're using k without defining it, we can't know without seeing the error message you get.
If you want to set them all to that string, then just loop over them:
char c[20][70];
int i;
for (i = 0; i < 20; ++i)
strcpy(c[i], "undefined");
If what you want is to have 20 strings of 70 chars each then your second option should work:
char c[20][70];
for (int k = 0; k < 20; k++)
strcpy(c[k], "undefined");
The char *c[20] definition is incorrect because you are just defining an array of 20 pointers, and the pointers are not initialized. You could make it work in this way:
char *c[20];
for (int k = 0; k < 20; k++) {
c[k] = malloc(70);
strcpy(c[k], "undefined");
}
// then when you are done with these strings
for (int k = 0; k < 20; k++)
free(c[k]);
You haven't actually allocated memory for the pointer in data[current] leading to undefined behavior (and one of the possible outcomes of UB is that sometimes it seems to work fine).
Either allocate memory and assign it to the pointer, or use std::string instead (which is my recommendation).
In modern C++ there's really little reason to use pointers at all these days, least of all for strings. Using pointers just creates problems like the one you have, and more. And using std::string also means you won't have the risk of a buffer overflow if the user inputs a string longer than 19 characters.
You have allocated array of char pointers. You need to allocate memory for each of your strings, something like this:
char * dynamicTemp = new char [ 20 ];
std::cin >> dynamicTemp;
data[ current++ ] = new char [ 20 ];
strcpy( data[ current ], dynamicTemp );
There are a host of generally innocuous warnings that valgrind will throw from stdlib functions since they "cheat" a little. But this is not the case here:
char* c = new char; // this is bad
only allocates a char - not a buffer of chars, try:
char* c = new char[s->size()+1];
and then change the delete to:
delete [] c;
char* c = new char; The size of c is 1 and to copy even 1 character string you need two characters long buffer (2nd character to hold the null terminator)
If you ever find yourself attempting to use sizeof() for anything that is sized dynamically, you're doing it wrong. Keep that in mind. sizeof() is used repeatedly in this code incorrectly. Dynamic size counts must be managed by you; the allocation size requirements in bytes can be managed using those counts in conjunction with a sizeof() of the fundamental type for the item being stored.
Given that:
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i = 0;
int arrayIndexes = 2;
char ** myArray = malloc(arrayIndexes * sizeof(*myArray));
//Allocate memory for each [x]
for (i = 0; i < arrayIndexes; i++)
{
myArray[i] = malloc(254 * sizeof(char));
sprintf(myArray[i], "string %d", i+1);
}
//Print out array values
printf("Before: \n");
for (i = 0; i < arrayIndexes; i++)
printf("Array[%d]: %s\n", i, myArray[i]);
// TODO: Fix this to check the result before orphaning the old
// value of myArray if an allocation failure ensues.
myArray = realloc(myArray, (arrayIndexes+1) * sizeof(*myArray));
++arrayIndexes;
//Allocate memory for the new string item in the array
myArray[arrayIndexes-1] = malloc(254 * sizeof(char*));
//Populate a new value in the array
strcpy(myArray[arrayIndexes-1], "string 3"); //
//Print out array values
printf("After: \n");
for (i = 0; i < arrayIndexes; i++)
printf("Array[%d]: %s\n",i, myArray[i]);
// TODO: write proper cleanup code just for good habits.
return 0;
}
Output
Before:
Array[0]: string 1
Array[1]: string 2
After:
Array[0]: string 1
Array[1]: string 2
Array[2]: string 3
You shall replace this line:
arrayIndexs = sizeof(*myArray)/sizeof(char*);
by something like this:
arrayIndexs += 1;
Reason: sizeof is a compile-time operator to determine the size of a datatype or datatype of an object, while here you shall manually maintain the array size.
EDTIED: Also when initializing the variable, you shall use
int arrayIndexs = 2;
instead of
int arrayIndexs = sizeof(*myArray) / sizeof(char*);