First, the code:
Code:
main(int argc, char * const argv[]) {
char *ident_a=0; // Initialize ident_a as a null pointer so we can easily check for that later
ident_a=malloc(sizeof(the_argument)); // dynamic memory allocation for ident_a
ident_a=strcpy(ident_a, the_argument); // should be safe since enough memory was allotted
testident(ident_a);
}
testident(char *ident_a) {
/*
* Makes a string with a random number in it
*/
if(ident_a != 0) { // As long as ident_a isn't a null pointer, test it
char *rstring=malloc(sizeof(int)+sizeof("dark_ok__stupid")); // Allocates enough memory for the string we're making (string + enough for random number)
sprintf(rstring, "dark_ok_%d_stupid", rand()); // Fills our array with a string with a random number in it
// Immediately after this sprintf, in testing, is where ident_a changes and gets filled with some of what sprintf() is writing
/* rstring is freed */
}
}
This is obviously very stripped own, but I didn't want to confuse people with too much irrelevant code.
The ident_a argument is received well, I've checked and no matter how large it stays in its place.
The problem is, when I do the sprintf() later in the program, it somehow writes to ident_a. As in, if I put abcdefghijklmnopqrstuvwxy0123456789 it would cut off after the "p" and fill the rest of ident_a with "dark_ok_RANDOMNUMBER_stupid" (obviously with a real random number in place of RANDOMNUMBER).
Why is my sprintf writing to ident_a? Didn't I give it enough memory?
Any help would be appreciated, thanks.