pointers

C pointer Q: malloc inside a fn call appears to be getting freed on return of fn?

I think I've got it down to the most basic case: int main(int argc, char ** argv) { int * arr; foo(arr); printf("car[3]=%d\n",arr[3]); free (arr); return 1; } void foo(int * arr) { arr = (int*) malloc( sizeof(int)*25 ); arr[3] = 69; } The output is this: > ./a.out car[3]=-1869558540 a.out(4100) malloc:...

int matrix with pointers in C - memory allocation confusion

Hello! I'm having some issues with producing an int matrix without creating memory leaks. I want to be able to make a given (global) matrix into any size dynamically via read_matrix(). But then i want to be able to free the memory later on. So in my main method the second printf should result in a bus error since it should not have any ...

What is a smart pointer and when should I use one?

What is a smart pointer and when should I use one? ...

Pointers and Reference

I understand the overall meaning of pointers and references(or at least I think i do), I also understand that when I use new I am dynamically allocating memory. My question is the following, if i were to use cout << &p it would display the "virtual memory location" of p. Is there a way in which I could manipulate the "virtual memory loca...

Needless pointer-casts in C

I got a comment to my answer on this thread: http://stackoverflow.com/questions/105477 In short I had code like this: int * somefunc (void) { int * temp = (int*) malloc (sizeof (int)); temp[0] = 0; return temp; } I got this comment: Can I just say, please don't cast the return value of malloc? It is not required and ca...

C pointer assignment behavior

temp2, temp1 are pointers to some struct x: struct FunkyStruct x; struct FunkyStruct *temp1 = &x, *temp2 = &x; Now, after execution of following lines: temp2=temp1; temp1=temp1->nxt; ...Will temp2 and temp1 still point to the same memory location? If not, please explain why they would be different. ...

Pointer vs. Reference

What would be better practice when giving a function the original variable to work with: unsigned long x = 4; void func1(unsigned long& val) { val = 5; } func(x); or: void func2(unsigned long* val) { *val = 5; } func2(&x); IOW: Is there any reason to pick one over another? ...

Why does strcpy trigger a segmentation fault with global arguments?

So I've got some C code: #include <stdio.h> #include <string.h> /* putting one of the "char*"s here causes a segfault */ void main() { char* path = "/temp"; char* temp; strcpy(temp, path); } This compiles, runs, and behaves as it looks. However, if one or both of the character pointers is declared as global variable, strcpy res...

Transparently swapping pointers to character arrays in C++

Hello, I have a 2D character array: char nm[MAX1][MAX2] = { "john", "bob", "david" }; I want to swap two of these elements (without std::swap) by simply writing swapPointers(nm[0], nm[1]); where swapPointers looks like this void swapPointers(char *&a, char *&b) { char *temp = a; a = b; b = a; } However, this d...

How do you pass a member function pointer?

I am trying to pass a member function within a class to a function that takes a member function class pointer. The problem I am having is that I am not sure how to properly do this within the class using the this pointer. Does anyone have suggestions? Here is a copy of the class that is passing the member function: class testMenu : p...

C++ STL: should I store entire objects, or pointers to objects?

Designing a new system from scratch. I'll be using the STL to store lists and maps of certain long-live objects. Question: Should I ensure my objects have copy constructors and store copies of objects within my STL containers, or is it generally better to manage the life & scope myself and just store the pointers to those objects in m...

when to pass fuction arguments by reference and when by address?

Could anyone explain with some examples when it is better to call functions by reference and when it is better to call by address? ...

Calling a C++ function pointer on a specific object instance

I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a way to handle that function with a specific instance of a C++ object? class A { private: EventFunction handler; public: void SetEvent(EventFunction func) { handler = func; } void EventOne() { handler(1); } }; class B { private: ...

What is a Pointer?

See: Understanding Pointers In many C flavoured languages, and some older languages like Fortran, one can use Pointers. As someone who has only really programmed in basic, javascript, and actionscript, can you explain to me what a Pointer is, and what it is most useful for? Thanks! ...

Why use pointers?

I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages. Basically I have three questions: Why use pointers over normal variables? When and where should I use pointers? How do you use pointers with arrays? Also, can any of you recommend a go...

Garbage with pointers in a class, C++.

I am using Borland Builder C++. I have a memory leak and I know it must be because of this class I created, but I am not sure how to fix it. Please look at my code-- any ideas would be greatly appreciated! Here's the .h file: #ifndef HeaderH #define HeaderH #include <vcl.h> #include <string> using std::string; class Header { public: ...

C++ alternatives to void* pointers (that isn't templates)

It looks like I had a fundamental misunderstanding about C++ :< I like the polymorphic container solution. Thank you SO, for bringing that to my attention :) So, we have a need to create a relatively generic container type object. It also happens to encapsulate some business related logic. However, we need to store essentially arbitr...

How to Pass an Object Method as a Parameter in Delphi, and then Call It?

I fear this is probably a bit of a dummy question, but it has me pretty stumped. I'm looking for the simplest way possible to pass a method of an object into a procedure, so that the procedure can call the object's method (e.g. after a timeout, or maybe in a different thread). So basically I want to: Capture a reference to an object'...

C++: Asterisks and Pointers

I've recently decided that I just have to finally learn C/C++, and there is one thing I do not really understand about pointers or more precisely, their definition. How about these examples: int* test; int *test; int * test; int* test,test2; int *test,test2; int * test,test2; Now, to my understanding, the first 3 cases are all doin...

Function pointer cast to different signature.

I use a structure of function pointers to implement an interface for different backends. The signatures are very different, but the return values are almost all void, void * or int. struct my_interface { void (*func_a)(int i); void *(*func_b)(const char *bla); ... int (*func_z)(char foo); }; But it is not required ...