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
Create a C language code, only one program, to implement the following task. Task: Write C code for a program that implements a digital clock (HH:MM:SS). The clock inclu

Inline functions, C++ provides inline functions to help reduce function_call overhead especially for small functions. The qualifier inline before function's return type in the f

A: N delete p is a two-step procedure: it calls the destructor, and then releases the memory. The code developed for delete p is functionally similar to this (supposing p is of typ

C program to demonstrate call by reference: void disp(int *,int*,int*,int*); void main() {                 int a=2,b=3, sum,mul;                 printf("Enter 1st n

What are all of the implicit member functions of the class? Or what are all of the functions that compiler implements for us if we don't describe one? A: 1.      copy ctor

Write a simple program in C++ to investigate the safety of its enumeration types. Include at least 10 different operations on enumeration types that are incorrect/unsafe things to

What does throw; (with no exception object after the throw keyword) mean? Where would I employ it? A: You should see code that looks something like this: class MyException

P o i n t er s to Members: In C a pointer is created  to locate  the address of another variable  for faster access. Consider the following example. int x, *p; p =

enter height of the shape: 1 3 5 3 1 3 5 7 5 3 5 7 9 7 5 3 5 7 5 3 1 3 5 3 1

Default Arguments A default argument is a value that is automatically assigned to a formal variable, if the actual argument from the function call is omitted. e.g. void