•
•
•
•
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
![]() |
char *name; cout<<name;
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
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]
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]
Ist Code
2nd Code
Both The Codes Works Fine.....Now What??Why Memory Allocation
#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;
}•
•
Join Date: Jul 2005
Location: London
Posts: 164
Reputation:
Rep Power: 4
Solved Threads: 5
Because in code 1 the string "sunny" is readonly. In code 2 the string "sunny" is writeable to.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,691
Reputation:
Rep Power: 36
Solved Threads: 877
•
•
•
•
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);![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Problem with memory allocation =( (C)
- regarding dynamic memory allocation (C)
- memory allocation ptr to array? how? (C)
- Why use Dynamic and Static Memory Allocation... (C)
- memory allocation (C)
- Dynamic memory allocation homework (C++)
- How to use alloc's memory allocation functions? (C)
Other Threads in the C Forum
- Previous Thread: Boolean algebra
- Next Thread: scope problem again



Linear Mode