dynamic memory allocation

char *abc= new char

in the syntax above, what is actually happening? i mean why i have to specify the type "char " twice. also, i have tried to store a string using the above statement and correctly produced the output using pointer "abc".

logically, according to me, the compiler must allocate the memory at run time as per the user input, i must only specify the type of data being entered and not the size. The compiler must store my data in continuous memory and return the starting address to pointer "abc".

tell me where i am wrong, and explain the syntax above?
You specify the type twice because:
1) The first designates the type of abc, which is actually char* and not char.
2)The second tells new the number of bytes it should be allocating (which depends on the type).

When you say you "stored" a string, did you do something like:
 
abc = "abc";

or like
1
2
for(int i(0); i < 3; ++i)
   *(abc + i) = i + 'a';


Both ways have flaws:
1)The first leads to a memory leak. You changed the location the pointer points to, but that allocated space has been left untouchable by the program (meaning it cannot be deallocated).
2)The second leads to out-of-bounds access. You allocated space for a single char, so accessing anything beyond index 0 may corrupt data. The fact that a terminating character just happened to lay right next to 'c' and that your assignment just happened not to mess anything up this time is not a guarantee that the program is working correctly.

To allocate space for multiple values:
 
char* abc = new char[n];
Last edited on
If you do not like to write two times char then you can use the following construction

auto abc = new char;

Or even the following way

auto abc = new auto( 'A' );

:)

However in the last case you write two times auto...:)
Last edited on
@Dealth

the program i wrote is:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();

char* abc=new char;
cout<<"enter string\t";
gets(abc);
cout<<"\nstring entered is\t";
puts(abc);
cout<<&abc<<"\t"<<&abc+1<<"\t"<<&abc+2;
getch();
}

in this program, i entered a string and then displayed it. Now if the "char" on the right hand side of "char* abc=new char" tells the number of bytes it should be allocating, how could i store a string of undefined size (at compile time) into memory?

also the memory address displayed were: 0x8fb8ff4, 0x8fb8ff6, 0x8fb8ff8

as i said before,the compiler should start allocating memory(like an array) at the run time when the input is given, and then the base address of the array must be allocated to pointer?
Last edited on
I'd recommend you look at the STL classes std::string (for character arrays that you want to treat as text strings) and std::vector (for other arrays). They'll give you what you need, and will handle the memory management for you automatically.

The STL container classes aren't some strange, esoteric thing. They're a fundamental part of C++ programming these days, and you'd do well to get into the habit of using them as soon as possible.
Topic archived. No new replies allowed.