deceptikon 1,790 Code Sniper Team Colleague Featured Poster

ok the only reason is it may cause problem when it is used in any other compiler or version

There are two big reasons:

  1. The new operator calls constructors and the delete operator calls destructors. malloc() and free() only work with raw memory. So if you use malloc() to allocate objects with constructors the constructors will not be called; this will probably crash your program. Likewise if you use new to allocate objects with destructors and free() to deallocate them, the destructors won't be called; this will probably cause resource leaks.
  2. The data structure used to store and manage allocated memory might be different between malloc()/free() and new/delete. If you mix them, the pointer might be valid with one but not the other and your program will probably crash.

    For example, on a Windows compiler the heap might be created with HeapCreate() instead of GetProcessHeap() and malloc()/free() could use a different heap object than new/delete like this:

    #include <Windows.h>
    
    class memory_manager
    {
    public:
        memory_manager(): _heap(HeapCreate(0, 0, 0)) { }
        ~memory_manager() { HeapDestroy(_heap); }
    
        void *alloc(size_t bytes) { return HeapAlloc(_heap, 0, bytes); }
        void dealloc(void* p) { HeapFree(_heap, 0, p); }
    private:
        HANDLE _heap;
    };
    
    int main(void)
    {
        memory_manager new_manager;
        memory_manager malloc_manager;
    
        void* p = new_manager.alloc(1);
    
        malloc_manager.dealloc(p); // Boom!
    }
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Any workaround this?

Exclude any fields that don't correspond to a database column. If it were me, I'd store the columns in an array or object and just iterate over them and store anything in $_POST that matches:

$columns = array('column1', 'column2', 'column3', 'column4');
$fields = array();

foreach ($_POST as $key => $value)
{
    // Only use fields that are known to the database
    if (isset($columns[$key]))
    {
        $fields[$key] = "'" . mysql_real_escape_string($value) . "'";
    }
}

mysql_query('INSERT INTO tablename (' . implode(',', array_keys($fields)) . ') VALUES (' . implode(',', $fields) . ')');
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
double **matrix = new double*[rows*sizeof(double*)];

This line declares a new matrix variable that hides the one defined in your class. After the constructor runs, the matrix variable defined in your class is still uninitialized.

for(int i=0, j=0; (i < rows, j < columns); i++, j++)

I don't think this does what you think it does. The i < rows part will be ignored completely and doesn't participate in stopping the loop. That means unless the matrix has the same number of rows and columns every time, you'll try to use an invalid index.

The usual method for traversing a 2D array is this:

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columns; j++)
    {
        // Do something at index [i][j]
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can build the query string dynamically. It all depends on how the POST data is organized, but assuming everything in $_POST corresponds directly to table column names and values, here's an example:

$columns = array();

foreach ($_POST as $key => $value)
{
    $columns[$key] = "'" . mysql_real_escape_string($value) . "'";
}

mysql_query('INSERT INTO tablename (' . implode(',', array_keys($columns)) . ') VALUES (' . implode(',', $columns) . ')');
asprin commented: Looks fine. +1
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What i asked is, how to write the the code, which don't give a undefined behavior still giving the correct output for this exact question.

The undefined behavior comes from modifying the same variable more than once in the same expression. To get rid of undefined behavior, first figure out what output you want, then break up the expression to do it. In the first post on this thread, the first code snippet has undefined behavior and the second code snippet is the right way to fix it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If the constructor depends on a pointer then it should make a copy in all cases or in no cases. That way the class is guaranteed to always own the pointer and deleting it is safe, or the pointer is always assumed to be owned by an external entity and deleting it is not safe:

// Pointer always owned
class Node
{
public:
    Node(Node const* parent)
    {
        if (parent == nullptr)
        {
            throw runtime_error("Invalid pointer");
        }

        _parent = new Node(*parent);
    }

    ~Node()
    {
        delete _parent;
    }
private:
    Node* _parent;
};

When the class owns the pointer it's safer to use one of C++'s smart pointers instead of pairing up new and delete.

// Pointer always owned
class Node
{
public:
    Node(Node const* parent)
    {
        if (parent == nullptr)
        {
            throw runtime_error("Invalid pointer");
        }

        _parent.reset(new Node(*parent));
    }
private:
    unique_ptr<Node> _parent;
};

Using an aliased pointer isn't as clean cut as owning the pointer, but it works as long as the class never assumes the pointer is valid or can be deleted.

Am I wrong about this ?

I think so, but I'm making an assumption about your class. A node class usually just needs to store the pointer because the memory is managed by whatever data structure class is creating the node objects. It could be as simple as this, for a binary tree:

struct Node
{
    Node(Node* left, Node* right, Node* parent = nullptr): _left(left), _right(right), _parent(parent)
    {}

    Node* _left;
    Node* _right;
    Node* _parent;
};

A …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Strings are immutable in C#. If you want something more like an array, use a StringBuilder object:

var x = new StringBuilder("Daniweb");

x[3] = 'k';

Console.WriteLine(x.ToString());