Could memory be trashed if string "hello" is assigned during declaration?

I know memory could be trashed with this:

char *str1;

str1 = "hello";

... but could anyone confirm whether the following may also cause memory to be trashed or, is the following okay, as it's assigned during declaration?

char *str1 = "hello";

Thanks!
bugsbunny1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
The first statement may or may not cause an error. Actually, its not good programming practice to do so. You must alocate memory for a pointer before you make it point somewhere. Before the str1 = "hello" ; statement, the compiler does not know how much memory to be allocated. But I guess it will run on most platforms. As for the second statement, it is definitely correct and it won't cause any errors because during the declaration itself, you are also specifying how much space to be allocated (length of "hello" + 1 for the null character) and then assigning it to point there.

Mayank.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oferhCommented:
both cases may cause memory corruption since
the memory allocated for the string is allocated
on the program stack.
that means that when you will exit the function in
which it was allocated this memory is "free".
0
Mayank SAssociate Director - Product EngineeringCommented:
Ya, if you put this in a function and attempt to return it, it will lead to memory corruption because the activation record of the function will be deallocated and the memory location pointed to by str1 will now be free for allocation. I'd given an answer for a simple case, like when you have a small main program containing the entire code which was posted in the question.

Cheers :-)

Mayank.
0
CleanupPingCommented:
bugsbunny1:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
Mayank SAssociate Director - Product EngineeringCommented:
Recommendation: Split points between mayankeagle and oferh.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.