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

I see, so I wasn't wrong, that's what I thought, that I wouldn't need a destructor, and thanks for the reply

If the code allocate memory with new, there needs to be a corresponding delete. So yes, if your constructor uses new, there should probably be a destructor that uses delete, and a copy constructor that appropriately does both.

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

As a matter of fact the output of my quicksort program is correct.

Unfortunately, in C "working" is not a guarantee of correctness. The swap process is undefined. Take a[hi]=a[pivot]+a[hi]-(a[pivot]=a[hi]); as an example. In C you can't assign to a variable and then use it again for anything except to determine the new value between sequence points. First the expression assigns a[hi] to a[pivot] . That's fine. Then it tries to use the value of a[pivot] in a way that doesn't determine a[pivot] 's new value.

It's better to write boring code than to be clever. The usual three variable swap is probably going to be faster anyway because of compiler optimizations.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 10
#define SWAP(a, b) do { int temp = a; a = b; b = temp; } while (0)

void quicksort(int *a, int start, int end);

int main()
{
    srand((unsigned)time(NULL));
    
    int a[N];
    
    for (int i = 0; i < N; i++)
    {
        a[i] = rand();
    }
    
    quicksort(a, 0, N - 1);
    
    for (int i = 0; i < N; i++)
    {
        printf("%d\n", a[i]);
    }
}

void quicksort(int *a, int start, int end)
{
    if (start >= end)
    {
        return;
    }
    
    int pivot = (start + end) / 2;
    int hi = start;
    
    SWAP(a[pivot], a[end]);
    pivot = end;
    
    for (int i = start; i < end; i++)
    {
        if (a[i] <= a[pivot])
        {
            SWAP(a[hi], a[i]);
            hi++;
        }
    }
    
    SWAP(a[hi], a[pivot]);
    pivot = hi++;
    
    quicksort(a, start, pivot - 1);
    quicksort(a, …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I mean of these, just for preparing to crack interviews.

But.

main()
{
char *p="dcis";
while(*p++!='\0')
printf("%s\n",p);
}

what about this..?

That's fine and will print "cisiss". Now the printf() is limited by the loop and overrun doesn't happen.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why not use String.Replace()?

string NewDate = Date.Replace(".", string.Empty);
Philippe.Lahaie commented: agreed, and made me laugh! +2
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The = operator is for assignment in C++. You want the == operator to compare equality:

for (x = 0; x <= y; x++)
{
    if (a == x || b == x || c == x)
    {
        printf("Value = %d", x);
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It totally depends on how the compiler organizes memory. In gcc the string literal "%s" is stored after the string literal "dcis" in memory, so overrunning the string prints "%s". You can test it like this:

#include <stdio.h>

int main(void)
{
    const char *before = "before";
    const char *p = "dcis";
    const char *after = "after";
    
    while (*p++ != '\0');
        
    printf("%s", p);
    
    return 0;
}

In gcc it'll print "after", but another compiler might print "before", or even garbage. Overrunning a string like that is undefined behavior.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The assembler would be written in another language first, possibly a different assembly language or even a higher level language like C. Then it's not uncommon after that for the assembler to compile/bootstrap itself as a way of shaking out bugs and proving viability of the tool.

And since it's a common question, the first compiler/assembler ever must have been written directly in machine code.

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());