Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2003-03-17
Medium Priority
Last Modified: 2010-05-19
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";

Question by:bugsbunny1
  • 3
LVL 30

Accepted Solution

Mayank S earned 80 total points
ID: 8150952
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.


Expert Comment

ID: 8152071
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".
LVL 30

Expert Comment

by:Mayank S
ID: 8152169
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 :-)


Expert Comment

ID: 9447079
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 
Post your closing recommendations!  No comment means you don't care.
LVL 30

Expert Comment

by:Mayank S
ID: 9472010
Recommendation: Split points between mayankeagle and oferh.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question