Using Double Pointers after memory allocated within function

C is pass by value. The value assigned to table is lost on returning from InitStringTable(). Also when allocating pointers to char ask for room for pointers to char. So this: … = (char**)malloc(sizeof(char)*10); shall at least be (assuming C): … = malloc(sizeof(char*)*10); A possible approach to this would be: #include <stdlib.h> #include <string.h> #include … Read more

Should pointer comparisons be signed or unsigned in 64-bit x86?

TL:DR: intptr_t might be best in some cases because the signed-overflow boundary is in the middle of the “non-canonical hole”. Treating a value as negative instead of huge may be better if wrapping from zero to 0xFF…FF or vice versa is possible, but pointer+size for any valid size can’t wrap a value from INT64_MAX to … Read more

Issue with UnsafePointer in SQLite project in Swift

This should work: let address = sqlite3_column_text(statement, 0) let string = String.fromCString(UnsafePointer<CChar>(address)) Update for Swift 3 (Xcode 8), compare Swift 3: convert a null-terminated UnsafePointer<UInt8> to a string: let string = String(cString: sqlite3_column_text(statement, 0)) Update: sqlite3_column_text() returns an implicitly unwrapped optional, and that is why you can pass it to String(cString:) directly. But according to … Read more

How to pass a 2d array from Python to C?

Listing [SciPy.Docs]: C-Types Foreign Function Interface (numpy.ctypeslib) (and [Python.Docs]: ctypes – A foreign function library for Python just in case). This is a exactly like [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati’s answer) (a duplicate), it just happens to also involve NumPy. In other words, you have Undefined Behavior, as … Read more

Swap nodes in a singly-linked list

Why Infinite loop? Infinite loop is because of self loop in your list after calling swap() function. In swap() code following statement is buggy. (*b)->next = (temp1)->next; Why?: Because after the assignment statement in swap() function temp1‘s next starts pointing to b node. And node[b]‘s next point to itself in a loop. And the self … Read more

Is pointer comparison undefined or unspecified behavior in C++?

Note that pointer subtraction and pointer comparison are different operations with different rules. C++14 5.6/6, on subtracting pointers: Unless both pointers point to elements of the same array object or one past the last element of the array object, the behavior is undefined. C++14 5.9/3-4: Comparing pointers to objects is defined as follows: If two … Read more