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
A: If you employ auto_ptr objects you would not need to be concerned along with heap objects not being deleted even if the exception is thrown.

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

pls give me answer 01110-00110 in 2''s comlpiment in detail

#questThere is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doin

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Write a program to process bank accounts. Create a base class named Account and two derived classes named Savings and Checking. In the base class, use an int AccountNum and a dou

C language lets us do this in a structure definition by putting: bit length after the variable that is. struct packed_struct { unsigned int f1:1; unsigned int f2:1; unsigned

I need Cryengine Sandbox Speedometer and Tachometer We want to get working Tachometer and Speedometer for a car in latest Cryengine Sandbox. You will find how to make it usin

Inheritance is the process of forming new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class,

(a) Pairs may be represented using a lambda. Using only lambdas, create a procedure (triple x y z) that constructs a triplet. You may NOT use car, cons or cdr in the triplet proced