pointers

Stupid question about `delete` and pointers

Say we have a piece of code: //... class A //... A* myA = new A(); A* myPointerToMyA = myA; delete myA; delete myPointerToMyA; // this is wrong, no? //... The last line does the exact same thing as the one above it, correct? So I would now be deleteing an invalid/NULL pointer? I understand this may be a stupid question, but still, I ...

Swapping objects with Objective C

I want to swap where player is pointing to here. I'm doing it wrong for sure, but can't figure out why. I've also tried using (Player **)player in the signature and &player when I called it. Any ideas? - (void)handleResult:(Result)result forPlayer:(Player *)player inLineup:(Lineup *)lineup { switch (result) { case Result...

How does char *blah = "hello" work?

When you make a string out of char pointers how does this work? char *name = "ben"; Is this 'hidden' pointer arithmetic? ...

Is this a self-assignment bug in ATL::CComVariant?

ATL::CComVariant has a handful of assignment operators. What I see in the implementation is that in assignment operators accepting LPCOLESTR, IUnknown* or IDispatch* the first action is to call Clear(). If the operator in invoked in such a way that a member variable of the same object is passed CComVariant variant; variant = L"string...

C++ char * pointer problem

I have a the following code: #include <iostream> using namespace std; void func(char * aString) { char * tmpStr= new char[100]; cin.getline(tmpStr,100); delete [] aString; aString = tmpStr; } int main() { char * str= new char[100]; cin.getline(str,100); cout<< str <<endl; func(str); cout<< str <<end...

trying to obtain an objects title variable gives unrecognized selector sent to instance

I have an object which holds a title and an indexReference. I save the object to an array and that works correctly. I then try to load from the array and populate the tableview. I use this code. //fill it with contents SavedFav *temp = [tableViewData objectAtIndex:indexPath.row]; cell.textLabel.text = temp.title; I then get an err...

How to get pointers and sizes of variables from the compiler - from outside the compiled code?

I'd like the compiler to output a file containing the pointers to all global variables in the source code it is compiling, and also the sizes of them. Is this possible? Is there a way to do it in any c compiler? ...

In C are malloc(256) and malloc(sizeof(char)*256) equivalent?

I see that people often write C code such as: char *ptr = malloc(sizeof(char)*256); Is that really necessary? The standard says that sizeof(char)==1 by definition, so doesn't it make sense just to write: char *ptr = malloc(256); Thanks, Boda Cydo. ...

Windows CE Samsung ARM9 device GPIO auxiliary power VC++

I was using a vendor provided GPIO dll for controlling power through an auxiliary power IO. The api was simple like callafunction(setportidentifiation,state) in vc++ . It was fine working. But it is not working currently. Calling the function does not respond.But with all other ports its fine ( LED,other set of Input / output ports). P...

What is `*((char*)ptr+4))` doing ?

#include<stdio.h> main() { int a[]={0,2,4,6,8}; int *ptr; ptr=a; printf("%d", *((char*)ptr+4)); } *((char*)ptr+4)) What is the purpose of this? ...

Get the offset of a field in a delphi record at runtime

Given a record type: TItem = record UPC : string[20]; Price : Currency; Cost : Currency; ... end; And the name of a field as a string, how can I get the offset of that field within the record? I need to do this at runtime - the name of the field to access is decided at runtime. Example: var pc : Integer; fieldName...

whats an example usage of c++ pointers?

i'm self-teaching c++ and i get how pointers work. but the doc i'm using is quite literal and the examples don't really go into why or when pointers would be used. a couple of real world examples would help me retain the knowledge. ...

Compiler error: cannot convert char* to char

i am trying to create a function like strlen() in string.h It's giving me the error can not convert char* to char #include<stdio.h> #include<conio.h> int xstrlen(char string); void main(void) { char string[40]; puts("Enter string:"); gets(string); printf(" %s is the length of %d", string, xstrlen(string)); } int xstrlen(cha...

return a pointer to a pointer array

I am having trouble freeing a pointer in a pointer array (values). typedef struct MyStruct{ char** values; }MyStruct; In C, I create dynamic array. JSDictionary **array = (JSDictionary **)malloc(sizeof(JSDictionary *) * numRows); The resultSet should be an array of JSDictionary pointers. I create the struct like: JSDictionary * ...

How do I insert a value in an arbitrary position in a block of memory?

I have two C functions, which basically operate on a stack data structure. This one pushes a value of type OBJ which is actually just unsigned long to the top of the stack. The stack is also grown if necessary. OBJ Quotation_push_(CzState *cz, CzQuotation *self, OBJ object) { if ((self->size + 1) > self->cap) { self->items =...

C/C++: Pointers within Const Struct

How do I force const-ness of the memory pointed to by obj->val1 in the function fn? #include <iostream> struct foo { int* val1; int* val2; int* val3; }; void fn( const foo* obj ) { // I don't want to be able to change the integer that val1 points to //obj->val1 = new int[20]; // I can't change the pointer, *...

cast pointer to functor, and call it

can I do something like: typedef void (*functor)(void* param); //machine code of function char functionBody[] = { 0xff,0x43,0xBC,0xC0,0xDE,.... } //cast pointer to function functor myFunc = (functor)functionBody; //call to functor myFunc(param); ...

javascript anonymous function scope

I have the following anonymous function: (function() { var a = 1; var b = 2; function f1() { } function f2() { } // this => window object! // externalFunction(this); })(); function externalFunction(pointer) { // pointer.f1(); => fail! } I need to call external function from this anonymous function and pass it's pointer to...

how to send QList<Object *> objects to another class ?

hi i'm trying to send a QList as a parameter to another class but for some reason i lose all it's content ... (when i open the object with the debuger i see for objects...) trying to send QList books to class Print: class Store: public QWidget { Q_OBJECT public: Analyze(QWidget *parent = 0); void generate_report(); ~An...

Declaring delegate

Just realized that the delegates I am declaring are not declared with pointer type. so instead of this id <AddViewControllerDelegate> *delegate; I have this id <AddViewControllerDelegate> delegate; Why the last way is correct? Since self is pointer(I guess) then why delegate is not? ...