pointers

Memory allocation of identical structs.

So I was teaching my friend about pointers. While doing so, I noticed that the address of two identical structs are exactly back-to-back. struct Foo { int a; }; struct Bar { int b; }; Which allowed me to do this: Foo foo; foo.a = 100; Bar bar; bar.b = 100; Foo *pFoo = &foo; Bar *pBar = &bar; (pFoo+1)->a = 200; This overr...

Creating an interface for an abstract class template in C++

I have the code as below. I have a abstract template class Foo and two subclasses (Foo1 and Foo2) which derive from instantiations of the template. I wish to use pointers in my program that can point to either objects of type Foo1 or Foo2, hence I created an interface IFoo. My problem is I'm not sure how to include functionB in the inte...

double point memory allocation

hi, char **r; r = (char **)malloc(10); the above allocation is enough?dont i need allocate for char *r through for loop?any can explain which one is right? ...

Appending pointers to QList

I need to insert pointers of classes (inherited from QObject) into a QList. I know that the following syntax can be used: .h QList<MyObject*> list; .cpp list.append(new MyObject("first", 1)); list.append(new MyObject("second", 2)); ... and then free memory: if(!list.isEmpty()) { qDeleteAll(list); list.clear(); } This sh...

Is it legal/well-defined C++ to call a non-static method that doesn't access members through a null pointer?

I came across the following code recently: class Foo { public: void bar(); // .. other stuff }; void Foo::bar() { if(!this) { // .. do some stuff without accessing any data members return; } // .. do normal actions using data members } The code compiles because in C++ methods are just functions th...

What is an idiomatic way to determine pointer limits in C++?

I'd like to know at compile-time the range of values for a pointer type. limits.h only specifies maximums and minimums for pure number types. I don't wish to use hard-coded constants, and I prefer not to compute a max using sizeof(foo*). ...

Can I read a dynamical length variable using fread without pointers?

I am using the cstdio (stdio.h) to read and write data from binary files. I have to use this library due to legacy code and it must be cross-platform compatible with Windows and Linux. I have a FILE* basefile_ which I use to read in the variables configLabelLength and configLabel, where configLabelLength tells me how much memory to alloc...

c++: set<customClasS* how to overload operator<(const customClass&*...)?!

Good Evening (depending on where u are right now). I am a little confused with the stl stuff for sorted sets... I want to store pointers of a custom class in my set and I want them to be sorted by my own criterion and not just the pointer size. Anyone has an idea how to do this? Since it is impossible to do it like operator<(const foo ...

What every c++ programmer need to know about 'this' pointer?

Hi, straight and clear as the title says :) Background: I run into some tutorial and I realized that I missed some things... So, I would really appreciate if anyone can point out any must_know_facts with an example. Thanks! ...

C Pointer Write Error

void fileNameProcess(char * inputName){ int size =strlen(inputName); bool change=false; char * name=inputName; for(int i =0; i<size; i++){ char temp=* (name+i); if(temp<0x10||temp>0x5b){ change=true; }else if(0x19...

Executable Initialization

When is it decided where the stack, global, and frame pointers are in memory? I'm trying to load an ELF executable into a simulator and I can't figure out what instructions load the global, stack and frame pointers into the regfile. ...

Pointer syntax in C: why does * only apply to the first variable?

The following declaration in C: int* a, b; will declare a as type int* and b as type int. I'm well aware of this trap, but what I want to know is why it works this way. Why doesn't it also declare b as int*, as most people would intuitively expect? In other words, why does * apply to the variable name, rather than the type? Sure you ...

Appending a data to file in c

I require to add a string before 45byte in an existing file. I tried using fseek as bellow. int main() { FILE *fp; char str[] = "test"; fp = fopen(FILEPATH,"a"); fseek(fp,-45, SEEK_END); fprintf(fp,"%s",str); fclose(fp); return(0); } I expected that this code will add "test" in before 45 char from EOF...

Arguments in a function prototype

Hi. My question is: when i write a function prototype in C like this: void foo(int *vector); It's the same thing to do: void foo(int vector[MAX_LENGTH]); To the function, is passed always as a pointer? The code it's the same? Thanks in advance. ...

How to get the Char a const * TCHAR pointer points to

Hey, I've got some problems with a pointer void getPartOfString(const TCHAR * wholeString) { const TCHAR * pPointer = NULL; pPointer = _tcsstr(wholeString, searchedString); //searching for a special string in string pPointer += _tcslen(searchedString); //set pointer to the beginning of the string wanted //here I want t...

question about auto_ptr::reset

please can anybody explain this code from C++ Reference site: #include <iostream> #include <memory> using namespace std; int main () { auto_ptr<int> p; p.reset (new int); *p=5; cout << *p << endl; p.reset (new int); *p=10; cout << *p << endl; return 0; } ...

Learning C - Pointers and memory addressing

I am learning C programming and I have a simple question about pointers... I used the following code to play around with pointers: #include <stdio.h> int main (int argc, const char * argv[]) { int * c; printf("%x\n",c); return 0; } When I print the value of C, I get back a 0. However, when I print &c (i.e. printf("&x\n",&c) I get ...

C++ noob question: pointers and overloaded []

I've been staring at this for a while and not getting very far. FruitBasketFactory, FruitBasket, and Fruit are three classes from an API I'm using. My goal is to make a fruit basket and then retrieve the fruit. According to the FruitBasketFactory.hpp: const FruitBasket* getFruitBasket() const; and then in the FruitBasket.hpp siz...

C++ Multiple Callback funtions problem

Hi, I have a dll which requires me to set a callback function for it (actually it's a camera sdk and it will callback my function when it receives a picture). I wanna have multiple (user input) cameras but I can't. Since I should make unknown number of callback functions. the easy way is to make a class (camera) which have a function f...

Memory problems

Hello, I have some singleton class (please, don't speak about singleton usage). class InputSystem : boost::serialization::singleton<InputSystem> { private: boost::shared_ptr<sf::Window> mInputWindow; public: InputSystem() { mInputWindow = boost::shared_ptr<sf::Window>( new sf::Window(someARgs) ); someMethod(); } ...