User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 401,687 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,676 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1065 | Replies: 5
Reply
Join Date: Aug 2005
Posts: 595
Reputation: SpS is on a distinguished road 
Rep Power: 5
Solved Threads: 30
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

why memory allocation

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

both of these work....then why do i need memory allocation....where does the first code fails
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 75
Reputation: CrazyDieter is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: why memory allocation

  #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  
Join Date: Aug 2005
Posts: 595
Reputation: SpS is on a distinguished road 
Rep Power: 5
Solved Threads: 30
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: why memory allocation

  #3  
Sep 20th, 2005
Ist Code

#include<iostream>
using namespace std;
int main()
{

char *name;
name="Sunny";
cout<<name;
return 0;
}

2nd Code
#include<iostream>
using namespace std;
int main()
{

char *name;
name=new char[20];
name="Sunny";
cout<<name;
return 0;
}
Both The Codes Works Fine.....Now What??Why Memory Allocation
Reply With Quote  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: why memory allocation

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,691
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 877
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: why memory allocation

  #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.
// allocate space to put your name, up to 79 characters
char *name = malloc(80);
// display the prompt
printf("Enter your name");
// get name from keyboard
fgets(name,80,stdin);


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

Re: why memory allocation

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 7:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC