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
Define some features of register storage class in c program? The feature of variable defined to be of register type all as follows: Storage - CPU registers Default initia


Objectives: The objective of this assignment is to use C++ to queue students into an array-based queue for BCS registration. Students have the option of taking five different cours

C Program for MULTIPLICATION TABLE   main() {           int a, b=1, c, d;           char ch;           clrscr();           while(ch!='n')           {

i have an array of structs, and am trying to make a pointer that can point to a certain struct within the array

Question 1 . Write a brief note on Aggregation Question 2 . Discuss briefly on constructors Question 3 . What are the important advantages of Inheritance? Question 4 .

Program is to store all ASCII char into a file: void main()     {   ofstream  fout("ascii.txt");   int i,n=256;   for(i=1;i     {     fout     }

Addition of array elements: #define rows 3 #define cols 3 void main() {                 int i=0,j=0,sum=0;                 int arr[rows][cols];

Aim: To implement a program to add two polar coordinates using operator overloading. Code:                       #define PI 3.14 class polar {             double

Problem: (a) Describe a linked list. (b) Explain the three different types of linked list with the help of diagrams. (c) Give two advantages and two disadvantages o