Question:
C strings, using arrays instead of pointers, to handle memory allocation?
Reveal my Identity?
2009-03-23 11:32:37 UTC
If I use this syntax

char myString[13];

instead of

char *myString;

I know none of my strings are longer then 13 characters (including the null terminator)

Will memory allocation and de-allocation be handled for me?

I want to avoid pointers where not absolutely necessary.
I don't want any memory issues, due to either a failure to allocate memory, or a failure to release memory I am finished with.
I would rather just have extra space automatically allocated, then have to deal with this myself.

If I do use
char *myString;

Do I have to allocate my own memory, and then release it?
Three answers:
llaffer
2009-03-23 11:49:23 UTC
If you use the character array, that creates the memory when the function is run, so you won't have to allocate or deallocate the memory on your own.



If you use character arrays, you'll either need to allocate your memory your self, or have the pointer assigned to memory space that's already been assigned by something else. Example of that:



char *ptr;

char myString[10];



strcpy(myString,"12345");

ptr = myString



printf("%s", ptr + 2);



that would output: 345



The pointer is used, and used the memory allocated from the character array.



Does this help?
2009-03-23 16:23:25 UTC
This will work, but it likely will not work as you expected it to.



Remember that arrays are simply pointers. If you pass myString to an object that uses it a member, and the original myString goes out of scope, then the object's copy of myString will be invalid, since it pointed to data that no longer exists. An array is passed as a pointer (they are equivalent), so you would need to ensure that the original data is valid, or do a deep copy of the string(strcpy).



In this way, allocation and deallocation are handled for you, but the array will not necessarily be deallocated when you want it to be. If you can guarantee that the operations that myString are involved in will all occur within the scope that it was declared, then you can use the array. Otherwise, you will need to allocate it with malloc.



char *myString will have to be allocated and released in your own code. Do not use something like char *myString = "foo", as that is a deprecated language feature, and is equivalent to using an array. It should be used like char *myString = (char*)malloc( ... ).
2009-03-23 11:49:08 UTC
"Will memory allocation and de-allocation be handled for me?"



No. This is C, not Java, not BASIC, not Python. You have to do everything yourself.



"I want to avoid pointers where not absolutely necessary.

I don't want any memory issues, due to either a failure to allocate memory, or a failure to release memory I am finished with. I would rather just have extra space automatically allocated, then have to deal with this myself."



Then switch to another language.



"If I do use

char *myString;



Do I have to allocate my own memory, and then release it?"



Yes you do.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...