Test code:
void modify_it(char * mystuff)
{
//last element is null i presume for c style strings here.
char test[7] = "123456";
//when i do this i thought i should be able to gain access to this
//bit of memory when the function is destroyed but that does not
//seem to be the case.
//static char test[] = "123123";
//this is also creating memory on stack and not the heap i reckon
//and gets destroyed once the function is done with.
//char * test = new char[7];
//this does the job as long as memory for mystuff has been
//allocated outside the function.
strcpy_s(mystuff,7,test);
//this does not work. I know with c style strings you can't just do
//string assignments they have to be actually copied. in this case
//I was using this in conjunction with static char test thinking
//by having it as static the memory would not get destroyed and i can
//then simply point mystuff to test and be done with it. i would later
//have address the memory cleanup in the main function.
//but anyway this never worked.
mystuff = test;
}
int main(void)
{
//allocate memory on heap where the pointer will point
char * mystuff = new char [7];
modify_it(mystuff);
std::string test_case(mystuff);
//this is the only way i know how to use cout by making it into a c++ string.
std::cout<<test_case.c_str();
delete [] mystuff;
return 0;
}
- In the case of a static array in the function why would it not work?
- In the case when I allocated memory using new in the function does it get created on the 开发者_Go百科stack or heap?
- In the case when I have a string which needs to be copied into a
char *
form. everything I see usually requiresconst char*
instead of justchar*
.
I know I could use a reference to take care of this easily. Or char **
to send in the pointer and do it that way. But I just wanted to know if I could do it with just char *
. Anyway your thoughts and comments plus any examples would be very helpful.
char * mystuff = new char [7]; delete mystuff;
delete mystuff
is causing undefined behavior. You must delete[]
what you new[]
.
The line mystuff = test;
causes the variable mystuff
to contain the address of the test
array. However, this assignment is local to the function. The caller never sees the modified value of mystuff
. This is generally true for C/C++: function parameters are passed by value, and local modifications to that value are invisible outside of the function. The only exception to this is if you use the &
operator in the parameter list in C++, which causes the parameter to be passed by reference. Like so:
void modify_it(char* &str) { /* ... */ }
However, if you do this, your program still won't work correctly, and will probably crash. That's because the address of test
is stack memory, and that memory will be overwritten when modify_it
returns. You'll be giving the caller the address of invalid stack memory, which can only lead to bad things. The correct thing to do is one of the following:
/* function allocates, caller frees */
void modify_it(char* &str) {
str = new char[7]; // allocate enough memory for string
memcpy(str, 7, test);
}
Or this:
/* caller allocates and frees */
void modify_it(char* str, size_t str_len) {
if (str_len < 7) { /* report an error. caller didn't allocate enough space. */ }
memcpy(str, 7, test);
}
精彩评论