How do i allocate multidimensional arrays by new? , C/C++ Programming

Can I free() pointers allocated  along with new? Can I delete pointers allocated along with malloc()?

A: No.

It is completely legal, moral, and wholesome to employ malloc() and delete in the same program, or to employ new and free() in the same program. However it is illegal, immoral, & despicable to call free() along with a pointer allocated though new, or to call delete on a pointer allocated through malloc().

Occasionally people tell that it works OK for them on machine X and compiler Y. Just because they don't notice bad symptoms in simple test case doesn't mean this won't crash in the field. Even if they know this won't crash on their specific compiler doesn't mean this will work carefully on another compiler, platform, or even another version of the simialr compiler.

Occasionally people say, "But I'm just working with an array of char." Though do not mix malloc() and delete on the similar pointer, or new & free() on the similar pointer! If you allocated through p = new char[n], you have to use delete[] p; you do not have to use free(p). Or if you allocated via p = malloc(n), you have to use free(p); you should not employ delete[] p or delete p! Mixing these up could cause a catastrophic failure at runtime if code was ported to a new compiler, a new machine or even a new version of the similar compiler

 

Posted Date: 3/19/2013 8:27:24 AM | Location : United States







Related Discussions:- How do i allocate multidimensional arrays by new? , Assignment Help, Ask Question on How do i allocate multidimensional arrays by new? , Get Answer, Expert's Help, How do i allocate multidimensional arrays by new? Discussions

Write discussion on How do i allocate multidimensional arrays by new?
Your posts are moderated
Related Questions
Description A long time ago in a galaxy far, far away, the country Mafghanistan had n cities and m old roads, where each road connected a pair of cities. Due to the treacherous mo

Write a program to calculate the timetable for numbers 1 -> 10 and display them as follows. Your solution should use for do loops      #include stdio.h void main() {    char p

what is the purpose of pointer ? what is the syntax? how it works?

A: Mostly can be overloaded. The only C operators which can't be are. and?: (and sizeof, that is technically an operator). C++ adds a few of its own operators, mostly which can be

i havea a small C++ i need help with. should not be anything special for an experienced programmer

I want Skype Recorder Application + Setup + Sourcecode + NICE UI Project Description: i want an application built which will allow user to record skype audio or video calls

solve mesh and nodal analysis through c/c++?

http://www.expertsmind.com/questions/decodethecode-30110560.aspx

should i put define constant and memory constant in my flowchart? the other one, how to draw flowchart for break and continue statement?

PROGRAM TO FIND WHETHER A NUMBER IS ODD OR EVEN int main() { int number ; printf("Enter a whole number\n"); scanf("%d",&number); if(number % 2 == 0) printf("n