pointers

In C++, how do I push an object to a vector while maintaining a pointer to the object?

In my code, I have a vector of Student objects. vector<Student> m_students; I want to: Check to see if the vector contains any Student of a certain name. If no such Student exists, add a new one. Add data to the Student of that name. Consider the following code: // Check to see if the Student already exists. Student* targetStuden...

Why can't the Nodes be linked together?

EDIT: Is it possible to NOT use new? (do not dynamically allocating memory) I think it is push that is wrong, but I don't know where, how, and why. here is the code: struct Node { string fileName; Node *link; }; int size(Node *&flist) { int count = 0; Node *tempPtr = flist; while (tempPtr != 0) { count += 1; ...

Create new C++ object at specific memory address?

Is it possible in C++ to create a new object at a specific memory location? I have a block of shared memory in which I would like to create an object. Is this possible? ...

Meaning of "@far int* @near IntegerPointer;"

I have the following definition. far int* near IntegerPointer; Does this mean, a pointer placed in 'near' memory pointing to a integer placed in far memory area. Can anyone please clarify. ...

Pointers for data returned by delegate methods in MacRuby

I'm working on a small MacRuby project, using 0.5b1, which is implementing the delegate methods required for Growl (the app is using Growl for notifications). I would like to be able to respond to the Growl callbacks when the notification is clicked, however when you register the Growl delegate with ::GrowlApplicationBridge.setGrowlDele...

What arguments does the sizeof operator take in C?

[Original title referred to 'sizeof function'.] I tried these and they all worked: char *p; printf("Size of *p is %d\n",sizeof(*p)); //result =1 printf("Size of p is %d\n",sizeof( p)); //result =4 printf("Size of p is %d\n",sizeof( //result =4 I wonder why the first printf is 1, the 2nd and 3rd is 4? So what arguments can size...

Divide large malloc-block into smaller "partitions"

Is there a way to use poiter arithmetic on a large malloc block, so you can assign multiple structs or primitive data types to that area already allocated? I'm writing something like this but it isnt working (trying to assign 200 structs to a 15000byte malloc area): char *primDataPtr = NULL; typedef struct Metadata METADATA; struct M...

Why does *(str+i) = *(str +j) not work here?

void reverse(char *str){ int i,j; char temp; for(i=0,j=strlen(str)-1; i<j; i++, j--){ temp = *(str + i); *(str + i) = *(str + j); *(str + j) = temp; printf("%c",*(str + j)); } } int main (int argc, char const *argv[]) { char *str = "Shiv"; reverse(str); printf("%s",str); return 0; ...

Pointers as function parameters in C

I am having trouble getting this to work. I have variables initiated in main which I want to pass onto other functions and have changed. I know the only way this can be done is with pointers or to declare the variables outside the main function. I would prefer to use pointers How is it done? eg int main(){ int variable1 = 5; ...

When do you want to use pointers vs values in C++?

I'm coming from Java and attempting to learn C++. As far as I can tell, using Pointers is very similar to how reference variables work in Java, in that you pass a memory address to the value. So I feel like I have gotten a pretty good understanding of them. I also understand that these variables are stored on the heap. However, I see ...

C++ Overloading << Operator problem

I'm fairly noobish at C++, but very comfortable with pointers, dereferencing, etc. I'm having a problem with my overload of the << operator for a class, in that it compiles fine but crashes when run. It feels like an infinite loop, but I'm not certain. Here's the code, and any help is appreciated. #include <string> #include <iostream> ...

Clarification on lists and removing elements

If I have a list<object*>>* queue and want to pop the first object in the list and hand it over to another part of the program, is it correct to use (sketchy code): object* objPtr = queue->first(); queue->pop_first(); return objPtr; // is this a pointer to a valid memory address now? ? According to the documentation on http://www.cp...

Objective-C: instance variables out of scope in debugger

I have a superclass and a subclass, both of which define instance variables. Rough outline of superclass: /* GenericClass.h */ @interface GenericClass : NSObject { /* some variables */ } @end /* GenericClass.m */ @implementation GenericClass /* ... */ @end Outline of subclass: /* SpecificClass.h */ #import "GenericClass.h" ...

Optimal way to free() a malloc'ed 2D array in C

Supposing I have a 2 dimensional array which was created with something like this, char **foo = (char **) malloc(height * sizeof(char *)); for(i = 0; i <= height; i++) foo[i] = (char *) malloc (width * sizeof(char *)); First of all, Is this even the right way to create an array like this?. The catch here is, 'height' and 'width' ...

Getting the size of the data of a Pointer

I tried the following code in order to see how to get size of the data of a pointer: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ char *test_char_ptr = "This is just a test"; int *test_int_ptr = (int *) malloc(16*sizeof(int)); for(int i = 0; i<16; i++){ test_int_ptr[i] = i; } printf("%s\n",te...

What's the ampersand for when used after class name like ostream& operator <<(...)?

I know about all about pointers and the ampersand means "address of" but what's it mean in this situation? Also, when overloading operators, why is it common declare the parameters with const? ...

Confusion between passing and modifying char pointers in C (reference vs. value)

Hi, I was wondering if you could help me out with a C string problem I don't quite understand. I have a function to which I send 3 char pointers. Within this function, the char pointers are shifted and modified correctly. However, when I return to the main function from which they are called, said functions are not changed. Am I passing ...

Efficient way of handling file pointers in Java? (Using BufferedReader with file pointer)

I have a log file which gets updated every second. I need to read the log file periodically, and once I do a read, I need to store the file pointer position at the end of the last line I read and in the next periodic read I should start from that point. Currently, I am using a random access file in Java and using the getFilePointer() m...

C++ Abstract base class array of ptrs to ptrs

I have an abstract base class (Comparable) with Date and Time virtually inheriting from it and a DateTime class v-inheriting from Date and Time. My problem is this: I was tasked with dynamically allocating an array of Comparables. Comparable ** compArray; compArray = new Comparable *[n]; // where n is user specified number of elements ...

Checking if a pointer is allocated memory or not

Hi I have a doubt regarding how can we check whether a pointer passed to a function is allocated with memory or not in C ? I have wriiten my own function in C which accepts a character pointer - buf [pointer to a buffer] and size - buf_siz [buffer size]. Actually before calling this function user has to create a buffer and allocate it ...