Need information about dynamic allocation in C++

Status
Not open for further replies.

fatma1000

Banned
Joined
Apr 30, 2006
Messages
147
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
eygpt
Activity points
0
help C++

please i need to understand step by step all about dynamic allocation in c++

beceuse i use "malloc" but my program has many problems and i thinh if i use dynamic allocation it will be good but i do not know any thing about it so please help me


thanks
 

help C++

What is your background.

You don't *use* malloc in C++. Use new
 

Re: help C++

please givr mr example .how can i use new?
thanks
 

help C++

char dynamicString[] = new char[stringSize];
 

Re: help C++

and then u should free the memory after using.

char* myString;
myString =(char*)new[numberOfCharecter];
if(!myString){
//report error!
}
//after use
delete[] myString;
 

help C++

There is a reason why I typed the line... so he could search the rest out from Google...

But since code was posted, let me say a few things :

1. new is NOT like malloc().

You MUST have a try-catch handler while newing up memory.

The lines :

Code:
if(!myString){ 
//report error! 
}

*may* work in your compiler, but it's deprecated.

2. char* myString and char myString[] are two different things.

4. after delete, it good practise to null the pointer :

Code:
//after use 
delete[] myString;
myString = NULL;

And I still don't understand why people don't Google before asking here at EDABoard ?
 

Re: help C++

dynamic allocation in C++ can be achieved with new and deallocation of memory with delete.
allocate memeory to a variable,
int *ip;
ip=new int;
allocate memeory to 1D array
int *ip;
ip=new int[10];
allocate memory to 2d array
int *ip;
ip=new int[][10];

for deallocation
delete ip;
delete []ip;
 

Re: help C++

hi all

i want to advise you

at first you must read an book
there are many of them
after that you can handle with the least information
but if you want to be prof
you must learn on net
c++ awide wide info
so ,if you tired you will get what you need

and i try this way .. and i succes

try and say hi ....
:idea:
 

help C++

the "malloc" can still use as the "new" operater but it's old and can't handle much memory.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…