pointers

Getting the start address of the current process's heap?

I am exploring the lower level workings of the system, and was wondering how malloc determines the start address of the heap. Is the heap at a constant offset or is there a call of some sort to get the start address? Does the stack affect the start address of the heap? ...

C++ standard: dereferencing NULL pointer to get a reference?

I'm wondering about what the C++ standard says about code like this: int* ptr = NULL; int& ref = *ptr; int* ptr2 = &ref; In practice the result is that ptr2 is NULL but I'm wondering, is this just an implementation detail or is this well defined in the standard? Under different circumstances a dereferencing of a NULL pointer should re...

How are iterators and pointers related?

Code with iterators looks pretty much like code with pointers. Iterators are of some obscure type (like std::vector<int>::iterator for example). What I don't get is how iterators and pointer are related to each other - is an iterator a wrapper around a pointer with overloaded operations to advance to adjacent elements or is it something...

C++: getting the address of the start of an std::vector ?

Sometimes it is useful to use the starting address of an std::vector and temporarily treat that address as the address of a regularly allocated buffer. For instance replace this: char* buf = new char[size]; fillTheBuffer(buf, size); useTheBuffer(buf, size); delete[] buf; With This: vector<char> buf(size); fillTheBuffer(&buf[0],...

Why is comparing against "end()" iterator legal?

According to C++ standard (3.7.3.2/4) using (not only dereferencing, but also copying, casting, whatever else) an invalid pointer is undefined behavior (in case of doubt also see this question). Now the typical code to traverse an STL containter looks like this: std::vector<int> toTraverse; //populate the vector for( std::vector<int>::i...

How to determine 2D unsigned short pointers array length in c++

Hello, I am finding it difficult to determine the length of the columns in a 2D unsigned short pointer array. I have done memory allocation correctly as far as I know. and can print them correctly. plz see the following code segment: int number_of_array_index_required_for_pointer_abc=3; char A[3][16]; strcpy(A[0],"Hello"); strcpy(A[1...

Setting pointee to NULL so any pointer pointing to that address is NULL

Rather than assigning a pointer to NULL, is there any way to set the location in memory the pointer is pointing to, to NULL so that any other pointer pointing to that location will be NULL? ...

How do I output an individual character when using char *[] = "something"

I've been playing with pointers to better understand them and I came across something I think I should be able to do, but can't sort out how. The code below works fine - I can output "a", "dog", "socks", and "pants" - but what if I wanted to just output the 'o' from "socks"? How would I do that? char *mars[4] = { "a", "dog", "sock", "...

How to determine the end of an integer array when manipulating with integer pointer?

Here is the code: int myInt[] ={ 1, 2, 3, 4, 5 }; int *myIntPtr = &myInt[0]; while( *myIntPtr != NULL ) { cout<<*myIntPtr<<endl; myIntPtr++; } Output: 12345....<junks>.......... For Character array: (Since we have a NULL character at the end, no problem while iterating) char myChar[] ={ 'A', 'B', 'C', 'D', 'E', '\0' }; char ...

Getting a seg fault, having trouble with classes and variables.

Ok, so I'm still learning the ropes of C++ here so I apologize if this is a simple mistake. I have this class: class RunFrame : public wxFrame { public: RunFrame(); void OnKey(wxKeyEvent& keyEvent); private: // Configuration variables. const wxString *title; const wxPoint *origin; const wxSize *size; const...

Declaration of pointers in C++

In C++ whats the difference between char const *ptr=&ch; and const char *ptr=&ch; ...

C++ pointers simple question

If I have the following lines inside a loop: Type *unite = new Type(newSize); or double *array= new double[anySize]; what is the behavior in what concerns to memory if I don't have delete operators inside it? It will be constantly allocating objects and arrays on different memory locations, and therefore memory leaks? ...

C++: Pointers and scope

int* test( ) { int a = 5; int* b = &a; return b; } Will the result of test be a bad pointer? As far as I know a should be deleted and then b would become a messed up pointer, right? How about more complicated things, not an int pointer but the same with a class with 20 members or so? ...

What happens when you deallocate a pointer twice or more in C++?

int main(){ Employee *e = new Employee(); delete e; delete e; ... delete e; return 0; } ...

c++ File input/output

Hi: I am trying to read from a file using fgets and sscanf. In my file, I have characters on each line of the while which I wish to put into a vector. So far, I have the following: FILE *fp; fp = fopen(filename, "r"); if(!fp) { fprintf(stderr, "Unable to open file %s\n", filename); return 0; } // Read file int line_...

How many instructions to access pointer in C?

Hi All, I am trying to figure out how many clock cycles or total instructions it takes to access a pointer in C. I dont think I know how to figure out for example, p->x = d->a + f->b i would assume two loads per pointer, just guessing that there would be a load for the pointer, and a load for the value. So in this operations, the point...

Why function does not know the array size?

If I write int main() { int a[100] = {1,2,3,4,}; cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array, //isn't it return 0; } I get 400! If I write void func(int *a); int main() { int a[100] = {1,2,3,4,}; func(a); return 0; } void func(int...

Different types for declaring pointer variables

Consider the below 2 declarations. * appears next to the datatype and not next to variable char* ptr1, * ptr2, * ptr3; //all 3 are pointers * appears next to the variable and not next to datatype char *ptr1,*ptr2,*ptr3; //again all 3 are pointers Is there any difference in intepretation between the 2 declarations. I know there is ...

Pointers and Addresses

Consider the below example int nCount[2] = {5,10}; int* ptrInt; ptrInt = nCount; cout<<ptrInt<<Endl;//this will print the address of arrar nCount now consider this char *str = "Idle mind is a devil's workshop"; int nLen = strlen(str); char* ptr; ptr = new char[nLen+1]; strcpy(ptr,str); cout<<ptr<<endl;//this wil print the string ...

Array of function pointers in Java

I have read this question and I'm still not sure whether it is possible to keep pointers to methods in an array in Java, if anyone knows if this is possible or not it would be a real help. I'm trying to find an elegant solution of keeping a list of Strings and associated functions without writing a mess of hundreds of 'if's. Cheers ...