pointers

Does NSValue free its value when freed?

╄→尐↘猪︶ㄣ 提交于 2021-01-28 06:33:15
问题 Have a look at this pseudocode: void* magicpointer = malloc(OVER_NINE_THOUSAND); NSValue* v = [NSValue valueWithPointer:magicpointer]; [v release]; When v is released, does magicpointer get freed as well, or do I have to do that manually? I am using manual reference counting. 回答1: It doesn't get freed - NSValue is just a wrapper so you can treat arbitrary values as objects. It doesn't do anything with the wrapped pointer. 回答2: No, NSValue won't. Sounds like you might want to use NSData , it

Force C++ to assign new addresses to arguments

≡放荡痞女 提交于 2021-01-28 06:10:43
问题 It seems that when I pass different integers directly to a function, C++ assigns them the same address as opposed to assigning different addresses to different values. Is this by design, or an optimization that can be turned off? See the code below for an illustration. #include <iostream> const int *funct(const int &x) { return &x; } int main() { int a = 3, b = 4; // different addresses std::cout << funct(a) << std::endl; std::cout << funct(b) << std::endl; // same address std::cout << funct

pointer not updating the value it is pointing to inside void function

烂漫一生 提交于 2021-01-28 05:26:41
问题 I've made a simple function that simple does the addition and absolute difference between the parameters using pointers, but when I try to update the pointer, the pointer still has the old value. why is this so, or what am I doing wrong: #include <stdio.h> #include <cstdlib> void update(int *a,int *b) { int temp = *a; int temp2 = *b; int temp3 =0; temp3 = temp + temp2; printf("%d",temp3); *b = abs(*a - *b); a = &temp3; // it is not updating } int main() { int a, b; int *pa = &a, *pb = &b;

How exactly does constexpr double Point::* coords[3] work? [duplicate]

ⅰ亾dé卋堺 提交于 2021-01-28 04:03:10
问题 This question already has answers here : Pointer to class data member “::*” (15 answers) Closed 6 months ago . So I've been looking at some stuff and found this thread Aliasing struct and array the C++ way And this is the answer to the question #include <math.h> struct Point { double x; double y; double z; }; double dist(struct Point *p1, struct Point *p2) { constexpr double Point::* coords[3] = {&Point::x, &Point::y, &Point::z}; double d2 = 0; for (int i=0; i<3; i++) { double d = p1->*coords

range of values a c pointer can take?

房东的猫 提交于 2021-01-28 03:17:50
问题 In "Computer System: A Programmer's Perspective", section 2.1 (page 31), it says: The value of a pointer in C is the virtual address of the first byte of some block of storage. To me it sounds like the C pointer's value can take values from 0 to [size of virtual memory - 1]. Is that the case? If yes, I wonder if there is any mechanism that checks if all pointers in a program are assigned with legal values -- values at least 0 and at most [size of virtual memory - 1], and where such mechanism

c - casting uint8_t* to uint32_t* behaviour

断了今生、忘了曾经 提交于 2021-01-28 02:09:56
问题 I have read this question: How does casting uint8* to uint32* work? but I am unsure of the answer given. I'm newbie embedded C programmer working on an project that uses GCC and I've been refactoring chunks of code to reduce the memory usage. An example is where I changed the data types of some variables from uint32_t to smaller sized types: uint8_t colour; uint16_t count; uint16_t pixel; func((uint32_t*)&colour); func((uint32_t*)&count); func((uint32_t*)&pixel); Where func(uint32_t* ptr)

Multiplying matrices using pointers

半腔热情 提交于 2021-01-28 00:41:46
问题 Hey guys I am trying practice learning pointers in c++. So I am trying to multiply these arrays and am getting all 0's in my resulting matrix. if anybody could just hint to me what to look at or some advice on what is causing this that would be amazing. Here is the code: #include <stdio.h> #include<conio.h> #include <stdlib.h> #include <iostream> /* Routines called. */ int loadMatrixFromFile(char *filename, int *data); void showMatrix(int *data, int len); int makeIdent(int matrixB[5][5], int

Is dereferencing a NULL pointer considered unspecified or undefined behaviour?

假如想象 提交于 2021-01-27 20:41:27
问题 The consensus of stackoverflow questions say that it is undefined behaviour. However, I recently saw a 2016 talk by Charles Bay titled: Instruction Reordering Everywhere: The C++ 'As-If" Rule and the Role of Sequence. At 37:53 he shows the following: C++ Terms Undefined Behaviour: Lack of Constraints (order of globals initialization) Unspecified Behaviour: Constraint Violation (dereferencing NULL pointer) Now I have conflicting information. Was this a typo? Has anything changed? 回答1: The

Store all QImage's pixels with scanLine() method in C++

懵懂的女人 提交于 2021-01-27 19:15:38
问题 I am trying to modify an image using Qt with the scanLine() method. This methods return a pointer to the data of a given row. I founded how to read rows here. Right now, I am able to read the value of all pixels like this: QRgb ** pixels; pixels = (QRgb **) (malloc(sizeof (QRgb*) * img->width() * img->height())); #pragma omp parallel for for (int y = 0; y < img->height(); ++y) { pixels[y] = (QRgb*) img->scanLine(y); } for (int x = 0; x < img->width(); ++x) { for (int y = 0; y < img->height();

Why can't you create a linked lists without creating nodes as pointers?

倖福魔咒の 提交于 2021-01-27 19:14:19
问题 There is an answer in Creating a linked list without declaring node as a pointer. But I wanted to know if there were any other reasons due to which you can't create nodes as pointers, just so, to be clear. One of the reasons is that the scope of the new nodes will die outside the function-Is there no way you can solve this problem?, and Is there any other reason? 回答1: I've been using a lot of linked lists (and even more complex structures) in which no node was allocated separately on the heap