| | |
why memory allocation
![]() |
C Syntax (Toggle Plain Text)
char *name; cout<<name;
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main() { char *name; name="Sunny"; cout<<name; return 0; }
2nd Code
C Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main() { char *name; name=new char[20]; name="Sunny"; cout<<name; return 0; }
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
Because in code 1 the string "sunny" is readonly. In code 2 the string "sunny" is writeable to.
•
•
•
•
Originally Posted by sunnypalsingh
Both The Codes Works Fine.....Now What??Why Memory Allocation
C Syntax (Toggle Plain Text)
// 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
C Syntax (Toggle Plain Text)
// 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);
![]() |
Similar Threads
- 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
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






