pointers

Uninitialised value was created by a heap allocation

I have been chasing this bug around, and I just don't get it. Have I forgotten some basic C or something? ==28357== Conditional jump or move depends on uninitialised value(s) ==28357== at 0x4C261E8: strlen (mc_replace_strmem.c:275) ==28357== by 0x4E9280A: puts (ioputs.c:36) ==28357== by 0x400C21: handlePath (myshell.c:105) ==2...

c++ use of ptr as array base

On page 42 of Effective C++, a pointer is used as an array name ala AirPlane *newBlock = ... newBlock[i].next=0; I've not been aware that this is legal. Is this part of the c++ standard? Is it common practice? ...

When to use pointers in C++

I just started learning about pointers in C++, and I'm not very sure on when to use pointers, and when to use actual objects. For example, in one of my assignments we have to construct a gPolyline class, where each point is defined by a gVector. Right now my variables for the gPolyline class looks like this: private: vector<gVector3*> ...

C++ Questions: pointers/memory addresses and subclasses

Why are we allowed to run this code: int* FunctionB(int x) { int temp =30; //more code return &temp; } It seems to me that I am not returning what I said I would. Why is it that a memory address can be returned if I declared the return type to be a pointer. Isn't a pointer something that points to a memory address, not...

Member Pointer to Base Class

Hi, all. I can't undestand why the bellow code need a cast to work. Someone can explain it? class Base { }; class Derived : public Base { }; class Class { public: Derived member; }; ... Derived obj; Base *ptrObj = &obj; // ok, no cast needed Derived Class::* ptr = &Class::member; // ok Base Class::* ptr = &Class::member; // ...

PHP | Remove element from array with reordering?

Hey Folks, how can I remove an element of an array, and reorder afterwards? <?php $c = array( 0=>12,1=>32 ); unset($c[0]); // will return sth. like array( 1=>32 ); ?> How can I do a "rearrange" like pointing the 1 to 0 after delete (automatically)? Thanks! ...

Problem with hash-table insertion function in C

Hey hey, I am trying to wrap my head around C right now (mostly how pointers operate). I am trying to write this function described below: /* EnterName enter a Name into a symbol table. Returns a boolean indicating whether an entry for Name was already listed in the table. Passes back an argu...

Freeing a character pointer

I have a function which is called multiple times during the program's execution. In said function, I have a dynamic character pointer which I resize numerous times. My question is: do I need to free this pointer before the end of the function? void functionName() { char *variable = (char *) malloc(0); //variable is resized with re...

C: Finding end of string: *s++ VS *s then s++

I'm writing a simple string concatenation program. The program works the way I have posted it. However, I first wrote it using the following code to find the end of the string: while (*s++) ; However, that method didn't work. The strings I passed to it weren't copied correctly. Specifically, I tried to copy "abc" to a char[] vari...

Why have a pointer to a pointer (int **a)?

int **a = malloc2d(M, N) // dynamically allocates a 2D array What is the purpose of having int **a vice int *a. I understand that pointers are needed for dynamic allocation but why have a pointer to a pointer? For a 3-dimensional array would it be: int ***a ? ...

How to free() a malloc()'d structured correctly?

Hi! i have a structure malloc()'d, and after using them, i want to free() it, but my program freezes out here. Can anyone tell me, what i am doing wrong? Here is my code: struct data { char *filename; char *size; }; //primarypcs is a long type variable struct data *primary = (struct data *)malloc( primarypcs * sizeof( stru...

Error working with structs and pointer arrays: incompatible types in assignment

#define STRMAX 50 struct Person { char sName[STRMAX]; int iAge; }; typedef struct Person PERSON; int main() { PERSON *personen[1]; personen[0]->sName = "Pieter"; personen[0]->iAge = 18; return 0; } This code generates an error on personen[0]->sName = "Pieter"; saying incompatible types in assignment. Why? ...

Why can't I access this struct through its pointer?

#include <stdio.h> #include <stdlib.h> typedef int element; struct cell { element e; struct cell *p; }; typedef struct cell* CELL; int main() { CELL* p; p = (CELL*) malloc (sizeof(struct cell)); p->e = 8; /* This ain't working */ *p.e = 8; /* This doesn't help anything either */ return 0; } I...

Stop condition for printing linked structs?

I have the following struct: struct cell { int nmbr; struct cell *p; }; I have created a chain of linked structs from this type. Every struct is connected to its predecessor through *p. If I decide to print all nmbrs with a recursive algorithm as shown below, how do I define the stop condition? void write(struct cell* l) { ...

How to guard against memory leaks?

I was recently interviewing for a C++ position, and I was asked how I guard against creating memory leaks. I know I didn't give a satisfactory answer to that question, so I'm throwing it to you guys. What are the best ways to guard against memory leaks? Thanks! ...

Catching weird C pointer arithmetic bugs

I recently ran into a very sneaky bug, in which I forget to dereference a pointer to a string (char array) and thus sometimes overwrote one byte on the stack. Bad: char ** str; (*str) = malloc(10); ... str[2] = 'a'; //overwrites 3 bytes from the location in which str is stored Corrected: char ** str; (*str) = malloc(10); ... (*str...

Learning Pointers in C

I have been cutting my teeth for the past 48 hours or so trying to implement this hash table function in C. My code is rather long (I realize it is not the most efficient, some of it is more me playing around with C to get a feel for how it works etc). The problem I am having is with the last line of my main program at the bottom (prin...

Why [object doSomething] and not [*object doSomething]?

Hi, In Objective-C, why [object doSomething]? Wouldn't it be [*object doSomething] since you're calling a method on the object, which means you should dereference the pointer? Thanks! ...

Where is pointer metadata stored?

Could be that I am overlooking something obvious, but where is pointer metadata stored? For instance if I have a 32-bit int pointer ptr and I execute ptr++ it knows to advance 4 bytes in memory. However, if I have a 64-bit int pointer it knows to advance 8 bytes. So who keeps track of what type of pointer ptr is and where is it stored? F...

How to bind control's properties to variables in C#?

I have form with lot's of TextBoxes and also there is a lot of threads in my program that need to read content's of those TextBoxes (settings). Since I can't access controls from other threads, I decided to make special class that will hold all the settings, and if user changes some, control will invoke OnTextChanged event and change cor...