-
August 15th, 2009, 03:26 PM
#1
memory allocation
Dear All,
If I write following statement, where will memory be allocated?
If it is on heap, who will free the allocated memory?
char* ptr = "hello";
Thanks
-
August 15th, 2009, 03:43 PM
#2
Re: memory allocation
It's hard-coded into your program, stored with the executable code. You cannot free that memory from within your program, because it was allocated by the operating system when your program was loaded into memory. Every string literal exists within the code segment of your program in the computer's memory. If you want to be able to free the allocated memory, do this:
Code:
char* ptr = malloc(strlen("hello")+1);
strcpy(ptr,"hello");
free(ptr);
Or, using new and delete:
Code:
char* ptr = new char[strlen("hello")+1];
strcpy(ptr,"hello");
delete ptr;
-
August 15th, 2009, 03:45 PM
#3
Re: memory allocation
Or using proper C++ techniques:
Code:
std::string str("hello");
-
August 15th, 2009, 05:42 PM
#4
Re: memory allocation
Originally Posted by postiwala
If I write following statement, where will memory be allocated?
If it is on heap, who will free the allocated memory?
char* ptr = "hello";
You should treat a String literal as you would a primitive literal.
When you see,
int i = 42;
you don't wonder how and where 42 is allocated and deallocated.
In the same fashion you shouldn't bother about "hello". That's the idea.
Last edited by nuzzle; August 15th, 2009 at 06:36 PM.
-
August 16th, 2009, 07:24 AM
#5
Re: memory allocation
Given below are my views:
a) memory is not allocated dynamically, the memory would be deallocated once ptr goes out of scope.
b) since the memory is not allocated, you should not delete memory.
Code from one of your other posts, shows that moment the pointer mystring goes out of scope, the memory of the char array is deallocated.
Code:
#include <iostream>
using std :: cout;
using std :: endl;
char* getValue()
{
char* mystring = "hello";
return (mystring);
}
int main()
{
char* ptr = getValue();
ptr[2] = 'n';
cout << "ptr = " << ptr << endl;
return 0;
}
-
August 16th, 2009, 08:15 AM
#6
Re: memory allocation
In the above code, the line
is incorrect because it attempts to modify a string literal. This will probably cause a crash.
For legacy reasons, string literals have type char*; however, in all modern code, you should treat them as if they have type const char*. Don't try to modify them.
-
August 16th, 2009, 08:32 AM
#7
Re: memory allocation
I don't think that program was trying to modify a string literal (pls correct me if i am wrong)
It was trying to modify the char memory location which had been deallocated.
given below is the modified version of the program which would work:
Code:
#include <iostream>
using std :: cout;
using std :: endl;
char* getValue()
{
char* mystring = new char[6];
mystring[0] = 'h';
mystring[1] = 'e';
mystring[2] = 'l';
mystring[3] = 'l';
mystring[4] = 'o';
mystring[5] = '\0';
return (mystring);
}
int main()
{
char* ptr = getValue();
ptr[2] = 'n';
cout << "ptr = " << ptr << endl;
delete [] ptr;
return 0;
}
-
August 16th, 2009, 08:35 AM
#8
Re: memory allocation
Originally Posted by Muthuveerappan
I don't think that program was trying to modify a string literal (pls correct me if i am wrong)
It was trying to modify the char memory location which had been deallocated.
That would be the case had the expression been:
Code:
char mystring[] = "hello";
which would have created an array on the stack and copied the string literal into it.
However, since it was
Code:
char *mystring = "hello";
the returned pointer was actually directed at the string literal in static, read-only memory.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|