#include <bits/stdc++.h>
using namespace std;
int main(int argc,char* argv[]){
string str = atoi[1];
cout<< str;
}
I get an error in this, is there any solution, so that when I get input from the command line argument and store that in str, and use for the operation
Related
I am trying to pass a (large) integer from python to an extension module, but I am unable to parse pythons arbitrary precision integers to 256-bit unsigned integers uint256. Here is the C callee:
#include <Python.h>
typedef unsigned _ExtInt(256) uint256;
static PyObject* test(PyObject* self, PyObject* args)
{
uint256 x;
if(!PyArg_ParseTuple(args, "O", &x)) {
puts("Could not parse the python arg");
return NULL;
}
// simple addition
x += (uint256) 1;
return Py_BuildValue("O", x);
}
// ... initalize extension module here ...
In python I run something like
import extension_module
extension_module.test(1)
And I get the error:
Bus error: 10
Or
Segmentation fault: 11
However, if I remove the simple addition x += (uint256) 1; it will atleast not throw any error and return the argument.
How do I parse extended-integer types in my C extension module?
I am migrating some code from using wchar_t to char32_t, and when compiling with the -Werror=pointer-sign flag set, I am getting the following issue:
// main.c
#include <uchar.h>
#include <wchar.h>
int main(void) {
wprintf(U"some data\n");
}
Compiling: gcc -std=c11 -Werror=pointer-sign main.c
Output:
main.c: In function ‘main’:
main.c:5:10: error: pointer targets in passing argument 1 of ‘wprintf’ differ in signedness [-Werror=pointer-sign]
wprintf(U"some data\n");
^~~~~~~~~~~~~~
In file included from main.c:2:
/usr/include/wchar.h:587:12: note: expected ‘const wchar_t * restrict’ {aka ‘const int * restrict’} but argument is of type ‘unsigned int *’
extern int wprintf (const wchar_t *__restrict __format, ...)
^~~~~~~
To remedy this, I can do:
wprintf((const int *)U"some data\n");
//or
printf("%ls\n", U"some data");
Although this is quite a pain. Is there a nice and easy way to do this? What is the real difference between const unsigned int* vs const signed int*, aside from the data type it points to? Is this possibly dangerous, or should I just disable the flag altogether?
char32_t is an unsigned type.
wchar_t is either signed or unsigned, depending on implementation. In your case, it is signed.
You can't pass a pointer-to-unsigned where a pointer-to-signed is expected. So yes, you need a type-cast, however you should be casting to const wchar_t *, since that is what wprintf() actually expects (wchar_t just happens to be implemented as an int on your compiler, but don't cast to that directly):
wprintf((const wchar_t *)U"some data\n");
It doesn't get much cleaner than that, unless you wrap it in your own function, eg:
int wprintf32(const char32_t *str, ...)
{
va_list args;
va_start(args, str);
int result = vwprintf((const wchar_t *)str, args);
va_end(args);
return result;
}
wprintf32(U"some data\n");
Note that this code will not work properly at all on platforms where sizeof(wchar_t) < sizeof(char32_t), such as Windows. On those platforms, where sizeof(wchar_t) is 2, you will have to actually convert your string data from UTF-32 to UTF-16 instead, eg:
int wprintf32(const char32_t *str, ...)
{
va_list args;
int result;
va_start(args, str);
if (sizeof(wchar_t) < sizeof(char32_t))
{
wchar_t *str = convert_to_utf16(str); // <-- for you to implement
result = vwprintf(str, args);
free(str);
}
else
result = vwprintf((const wchar_t *)str, args);
va_end(args);
return result;
}
wprintf32(U"some data\n");
When I only include my "Int.h", friend declaration invokes "C++ requires a type specifier for all declarations". And it runs well when I just write forward declaration "class Int;" and include "Int.h" in cpp file.
#ifndef _MY_ARRAY_H_
#define _MY_ARRAY_H_
// class Int;
#include "Int.h"
class MyArray {
friend Int; //error: C++ requires a type specifier for all declarations
public:
MyArray(int dim, int *size);
Int operator[](int n);
private:
int mDim;
int *mSize;
int *mData;
};
#endif
In my opinion, it doesn't need "forward declaration", because there is a "Int" class declaration in the "Int.h" file. But it does. Why?
I've tried to use the Freeglut library in a Swift 4 Project. When the
void glutInit(int *argcp, char **argv);
function is shifted to Swift, its declaration is
func glutInit(_ pargc: UnsafeMutablePointer<Int32>!, _ argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!)
Since I don't need the real arguments from the command line I want to make up the two arguments. I tried to define **argv in the Bridging-Header.h file
#include <OpenGL/gl.h>
#include <GL/glut.h>
char ** argv[1] = {"t"};
and use them in main.swift
func main() {
var argcp: Int32 = 1
glutInit(&argcp, argv!) // EXC_BAD_ACCESS
glutInitDisplayMode(UInt32(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH));
glutCreateWindow("my project")
glutDisplayFunc(display)
initOpenGL()
glutMainLoop()
but with that I get Thread 1: EXC_BAD_ACCESS (code=1, address=0x74) at the line with glutInit().
How can I initialize glut properly? How can I get an UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>! so that it works?
The reason the right code in C char * argv[1] = {"t"}; does not work is because Swift imports fixed size C-array as a tuple, not a pointer to the first element.
But your char ** argv[1] = {"t"}; is completely wrong. Each Element of argv needs to be char **, but you assign char * ("t"). Xcode must have shown you a warning at first build:
warning: incompatible pointer types initializing 'char **' with an expression of type 'char [2]'
You should better take incompatible pointer types warning as error, unless you know what you are doing completely.
Generally, you should better not write some codes generating actual code/data like char * argv[1] = {"t"}; in a header file.
You can try it with Swift code.
As you know, when you want to pass a pointer to single element T, you declare a var of type T and pass &varName to the function you call.
As argcp in your code.
As well, when you want to pass a pointer to multiple element T, you declare a var of type [T] (Array<T>) and pass &arrName to the function you call.
(Ignoring immutable case to simplify.)
The parameter argv matches this case, where T == UnsafeMutablePointer<Int8>?.
So declare a var of type [UnsafeMutablePointer<Int8>?].
func main() {
var argc: Int32 = 1
var argv: [UnsafeMutablePointer<Int8>?] = [
strdup("t")
]
defer { argv.forEach{free($0)} }
glutInit(&argc, &argv)
//...
}
But I wonder if you really want to pass something to glutInit().
You can try something like this:
func main() {
var argc: Int32 = 0 //<- 0
glutInit(&argc, nil)
//...
}
I'm not sure if freeglut accept this, but you can find some articles on the web saying that this works in some implementation of Glut.
I am working with a C API that defines a structure with const char* and a function that returns a char* and am trying to find the best way to do the assignment.
Is there a way to do this without using unsafeBitCast? If I don't put the cast then I get this error:
Cannot assign value of type 'UnsafeMutablePointer<pchar>'
(aka 'UnsafeMutablePointer<UInt8>')
to type 'UnsafePointer<pchar>!'
(aka 'ImplicitlyUnwrappedOptional<UnsafePointer<UInt8>>')
Also, will the initialization of pairPtr below using pair() allocate a pair struct on the stack to initialize the allocated pair on the heap because this seems inefficient for the case where the structure just has to be zero'd out.
Here is sample code:
C library header (minimized to demonstrate the problem):
#ifndef __PAIR_INCLUDE__
#define __PAIR_INCLUDE__
typedef unsigned char pchar;
pchar*
pstrdup(const pchar* str);
typedef struct _pair {
const pchar* left;
const pchar* right;
} pair;
#endif // __PAIR_INCLUDE__
My Swift code:
import pair
let leftVal = pstrdup("left")
let rightVal = pstrdup("right")
let pairPtr = UnsafeMutablePointer<pair>.allocate(capacity: 1)
pairPtr.initialize(to: pair())
// Seems like there should be a better way to handle this:
pairPtr.pointee.left = unsafeBitCast(leftVal, to: UnsafePointer<pchar>.self)
pairPtr.pointee.right = unsafeBitCast(rightVal, to: UnsafePointer<pchar>.self)
The C code:
#include "pair.h"
#include <string.h>
pchar*
pstrdup(const pchar* str) {
return strdup(str);
}
The module definition:
module pair [extern_c] {
header "pair.h"
export *
}
You can create an UnsafePointer<T> from an UnsafeMutablePtr<T>
simply with
let ptr = UnsafePointer(mptr)
using the
/// Creates an immutable typed pointer referencing the same memory as the
/// given mutable pointer.
///
/// - Parameter other: The pointer to convert.
public init(_ other: UnsafeMutablePointer<Pointee>)
initializer of UnsafePointer. In your case that would for example be
p.left = UnsafePointer(leftVal)