why memory allocation

Reply

Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

why memory allocation

 
0
  #1
Sep 19th, 2005
  1. char *name;
  2. cout<<name;
and
  1. char*name;
  2. name=new char[20];
  3. cout<<name;

both of these work....then why do i need memory allocation....where does the first code fails
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 76
Reputation: CrazyDieter is an unknown quantity at this point 
Solved Threads: 3
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: why memory allocation

 
0
  #2
Sep 19th, 2005
in your first example, the 'name' variable stores the address of an area of the memory that you do not know. this can be any value. When you try to print it, using 'cout<<', This should always crash, reporting a 'segmentation fault', as this operation will try to read the content of the memory at a random address.

Your second example is a bit better, because you allocate the memory (20 bytes) before using it.

The problem is that character strings are stored with a terminating '\0' character (a byte set to 0) that delimits the end of a character string.
As the content of your array is not defined yet, you may also have a problem, because the 'cout<<' operation will read the content of the array UNTIL it finds a '\0' character. This may happen inside your array, or not ! In the latter case, You will also try to read memory that you do not own.

So in general :

always be sure that you are working with memory that you own
always be sure of the content of this memory;

The correct code (for writing an empty string (!?)) may be :
[php]
char *name=new char[20];
name[0]='\0';
cout<<name<<endl;
delete name; //always release allocated memory
[/php]
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: why memory allocation

 
0
  #3
Sep 20th, 2005
Ist Code

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.  
  6. char *name;
  7. name="Sunny";
  8. cout<<name;
  9. return 0;
  10. }

2nd Code
  1.  
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.  
  7. char *name;
  8. name=new char[20];
  9. name="Sunny";
  10. cout<<name;
  11. return 0;
  12. }
Both The Codes Works Fine.....Now What??Why Memory Allocation
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: why memory allocation

 
0
  #4
Sep 20th, 2005
Because in code 1 the string "sunny" is readonly. In code 2 the string "sunny" is writeable to.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: why memory allocation

 
0
  #5
Sep 20th, 2005
Originally Posted by sunnypalsingh
Both The Codes Works Fine.....Now What??Why Memory Allocation
There are many many cases where you do not know the text that the character array contains. For example, suppose I ask you for your name. The program has to put the answer some place, if the pointer doesn't point to some value memory locations then your program will crash big time.
  1. // allocate space to put your name, up to 79 characters
  2. char *name = malloc(80);
  3. // display the prompt
  4. printf("Enter your name");
  5. // get name from keyboard
  6. fgets(name,80,stdin);


the above could also be done like this
  1. // allocate space to put your name, up to 79 characters
  2. char name[80];
  3. // display the prompt
  4. printf("Enter your name");
  5. // get name from keyboard
  6. fgets(name,80,stdin);
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: why memory allocation

 
0
  #6
Sep 20th, 2005
Originally Posted by Stoned_coder
Because in code 1 the string "sunny" is readonly. In code 2 the string "sunny" is writeable to.
Thanx....i got it...its only read only
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC