Converting from signed char to unsigned char and back again?

I'm working with JNI and have an array of type jbyte, where jbyte is represented as an signed char ie ranging from -128 to 127. The jbytes represent image pixels. For image processing, we usually want pixel components to range from 0 to 255. I therefore want to convert the jbyte value to the range 0 to 255 (ie the same range as unsigned char), do some calculations on the value and then store

从signed char转换为unsigned char并返回?

我正在使用JNI,并有一个jbyte类型的数组,其中jbyte表示为一个有符号的字符,即从-128到127.Jbytes表示图像像素。 对于图像处理,我们通常希望像素分量的范围从0到255.因此,我想将jbyte值转换为0到255的范围(即与无符号字符的范围相同),对值进行一些计算,然后存储再次结果为jbyte。 我怎样才能安全地进行这些转换? 我设法让这个代码正常工作,其中一个像素值增加30,但被固定为值255,但我不明白它是安全的还是便携

x high DesignResolution and iOS simulator display bug

I'm currently building a game with cocos2dx 3.0 alpha0 in c++. At the moment I'm building for iOS. I tried to set the "DesignResolution" at 1280 by 1920 (Which is double the iPhone 4 resolution) so I can have the graphics look good on iPad retina. Everything works fine on the devices but when I try to run it on the iPhone and iPad simulator I get three quarters of the screen

x高DesignResolution和iOS模拟器显示错误

我目前正在用c ++编写cocos2dx 3.0 alpha0游戏。 目前我正在为iOS构建。 我试图将1920年的“设计分辨率”设置为1280(这是iPhone 4分辨率的两倍),所以我可以在iPad视网膜上看到图形。 一切工作正常的设备,但当我试图在iPhone和iPad模拟器上运行它,我得到四分之三的屏幕呈现黑色。 无论是iPhone,iPad还是iPad视网膜模拟器都会发生这种情况。 当我试图通过960将DesignResolution降低到640时,它在模拟器上显示得很好。 有

C++ new operator scope

雅博体育锁定账户 So I was writing a piece of code where I used new operator to create an object inside a local scope function and returned the reference as a pointer. A* operator+(A that){ int sumA = a + that.getA(); int sumB = b + that.getB(); return new A(sumA, sumB); } In my thought It would not work because the object was created inside the local scope and should be temporary, but it compiled an

C ++新的运营商范围

所以我写了一段代码,我使用new运算符在本地作用域函数内创建一个对象,并将引用作为指针返回。 A* operator+(A that){ int sumA = a + that.getA(); int sumB = b + that.getB(); return new A(sumA, sumB); } 在我看来,这是行不通的,因为该对象是在本地作用域内创建的,应该是临时的,但它会被编译并运行。 任何人都可以向我解释这个吗? 我相信还有其他的东西有能力打破范围和东西。如果可能的话,你能给我

Detecting stack or heap allocation

I have a class I'd like to be able to set a flag in that says if it is heap allocated so it can properly clean up after itself and not try to delete itself if it's on the stack. The problem is...I can't seem to override both new and the constructors at the same time. So it goes from my new overload that sets the isHeapAllocated flag and then into my constructor which resets the flag

检测堆栈或堆分配

我有一个班,我希望能够设置一个标志,说如果它是堆分配,所以它可以适当清理后,而不是尝试删除自己,如果它在堆栈上。 问题是......我似乎无法同时重写new和构造函数。 所以它从我的new重载设置isHeapAllocated标志,然后到我的构造函数重置标志。 void* String8::operator new(size_t size) { String8* string = (String8*)malloc(size); if(string == null) Exception("allocation fail : no free memory"

Managing Scope and Object Lifetime Within STL Vectors

Coming from a C# world, I'm struggling to make sure I don't introduce memory leaks and errors in a C++ project I've been assigned to. I'm writing code that uses structs to parse information from a buffer of data. Because the number of data structures that appear in the buffer can vary at runtime, an stl vector is used to store the processed data. I came across the following blo

管理STL向量中的范围和对象生命周期

来自C#世界,我努力确保我不会在我分配的C ++项目中引入内存泄漏和错误。 我正在编写使用结构来解析来自数据缓冲区的信息的代码。 由于缓冲区中出现的数据结构数量在运行时会有所不同,因此使用stl向量来存储处理后的数据。 我在现有软件中遇到了以下代码块,并且很难理解它为什么起作用: MyVectorOfObjects.clear(); for (unsigned __int8 i = 0; i < NumberOfObjects; i++) { MyParserObject parserObject;

Why is the copy constructor being called before deleting the object?

I have the following class, which includes a copy constructor: Header File: Fred.h namespace foo { class Fred { private: int _x; int _y; public: Fred(); // Default constructor Fred(Fred const &other); // Copy constructor Fred(int x, int y); // Regular parameters ~Fred(); // Destrcutor }; } Imp

为什么在删除对象之前调用拷贝构造函数?

我有以下类,其中包括一个复制构造函数: 头文件:Fred.h namespace foo { class Fred { private: int _x; int _y; public: Fred(); // Default constructor Fred(Fred const &other); // Copy constructor Fred(int x, int y); // Regular parameters ~Fred(); // Destrcutor }; } 实施文件:Fred.cpp #include "Fr

C++ and when to use delete

I am re-reading some code from a while ago on C++ (I am learning Java in school right now), and I am a little confused as to when I must use delete. For example: When declaring two objects: Fraction* f1; Fraction* f2; And create f1 and f2 like this: f1 = new Fraction(user_input1, user_input2); f2 = new Fraction(user_input3, user_input4); The next time I want to use new operator to create a

C ++和何时使用删除

我刚刚重新阅读了一些C ++代码(我现在在学校学习Java),并且对于何时必须使用delete有点困惑。 例如:声明两个对象时: Fraction* f1; Fraction* f2; 并像这样创建f1和f2: f1 = new Fraction(user_input1, user_input2); f2 = new Fraction(user_input3, user_input4); 下一次我想使用new操作符创建一个新对象时,是否必须先删除? 我很困惑,因为我习惯于让java中的垃圾回收器负责处理对象并删除它们。 我需要删除才

What is safe? returning a structure or the pointer from a function

#include struct person_t{ int age; }; person_t get_person1(){ person_t person; person.age = 10; return person; } person_t * get_person2(){ person_t *person = new person_t; person->age = 20; return person; } int main(){ person_t person1 = get_person1(); person_t *person2 = get_person2(); std::cou

什么是安全的? 从函数返回一个结构或指针

#include struct person_t{ int age; }; person_t get_person1(){ person_t person; person.age = 10; return person; } person_t * get_person2(){ person_t *person = new person_t; person->age = 20; return person; } int main(){ person_t person1 = get_person1(); person_t *person2 = get_person2(); std::cou

Difference of variable initialization in C++

This question already has an answer here: When should I use the new keyword in C++? 11 answers "Is there any difference between these 2 ways of storing an integer?" Yes, there is a significant difference. int X = 100; Initializes a variable X on the stack with the value 100 , while int *pX = new int(100); allocates memory for an int on the heap, kept in pointer pX , and init

C ++中变量初始化的区别

这个问题在这里已经有了答案: 我应该什么时候在C ++中使用新的关键字? 11个答案 “这两种存储整数的方式有什么区别吗?” 是的,存在显着差异。 int X = 100; 使用值100初始化堆栈上的变量X ,while int *pX = new int(100); 为堆中的int分配内存,保存在指针pX ,并将该值初始化为100 。 对于后者,您应该注意到,有必要在不再需要时释放堆内存: delete pX; 第一个是在栈上创建一个变量,而第二个是在堆上创建

What is the best way to manage the lifetime of an object variable?

Possible Duplicate: When should I use the new keyword in C++? I'm not a professional programmer and I only have experience working with small projects, so I'm having a little trouble understanding what's going on here. I usually create objects using class_name var_name . But now I'm 'learning' Objective-C, where almost everything is a pointer and you have more contr

管理对象变量的生命周期的最佳方法是什么?

可能重复: 我应该什么时候在C ++中使用新的关键字? 我不是一个专业的程序员,我只有与小型项目合作的经验,所以我在理解这里发生的事情时有点麻烦。 我通常使用class_name var_name创建对象。 但是现在我正在'学习'Objective-C,几乎所有的东西都是一个指针,并且你对内存使用有更多的控制。 现在我正在创建一个包含无限循环的应用程序。 我的问题是,哪个选项是管理内存使用情况(导致内存使用量减少)的