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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

752 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