I am trying to get a grasp on pointers and their awesomeness as well as a better C++ understanding. I don't know why this wont compile. Please tell me what is wrong? I'm trying to initialize the pointer when an instance of the class is created. If I try with a normal int it works fine but when I tried to set it up with a pointer i get th...
I'll start out by saying, use smart pointers and you'll never have to worry about this.
What are the problems with the following code?
Foo * p = new Foo;
// (use p)
delete p;
p = NULL;
This was sparked by an answer and comments to another question. One comment from Neil Butterworth generated a few upvotes:
Setting pointers to NUL...
I'm investigating a memory leak and from what I see, the problem looks like this:
int main(){
char *cp = 0;
func(cp);
//code
delete[] cp;
}
void func(char *cp){
cp = new char[100];
}
At the //code comment, I expected cp to point to the allocated memory, but it still is a null pointer meaning I never delete the mem...
I am learning about strings in C now.
How come to use scanf to get a string you can do
scanf("%s",str1);
and for printf you can do
printf("The string is %s\n", str1);
I understand that for scanf it is because the string is just a character array which is a pointer, but for printf, how is it that you can just put the variable name...
Recently I implemented a 'Paused' screen in my game. Since I wanted it to be a separate game state, I needed to somehow save the data from when the player paused the game to when they re-enter. However, when states are switched the pointer to the previous state is deleted.
Thus, I decided for the Paused constructor to take a copy of the...
In object-oriented programming, it's sometimes nice to be able to modify the behavior of an already-created object. Of course this can be done with relatively verbose techniques such as the strategy pattern. However, sometimes it would be nice to just completely change the type of the object by changing the vtable pointer after instant...
In writing a copy constructor for a class that holds a pointer to dynamically allocated memory, I have a question.
How can I specify that I want the value of the pointer of the copied from object to be copied to the pointer of the copied to object. Obviously something like this doesn't work...
*foo = *bar.foo;
because, the bar object...
Theoretically I can say that
free(ptr);
free(ptr);
is a memory corruption since we are freeing the memory which has already been freed.
But what if
free(ptr);
ptr=NULL;
free(ptr);
As the OS will behave in an undefined manner I cannot get an actual theoretical analysis for this about what's happening.
Whatever I am doing, is th...
Hi all,
typedef struct
{
struct table **symbols; // array of the stack
int top; //index of the top element
int size; //maximum size of the stack
}stack;
void *createStack(int size)
{
stack *stck;
stck = (stack *) malloc(sizeof(stack));
stck->symbols = ....
stck->size = size;
stck->top = -1;
printf("stack is created...
Hi,
when I'm trying to push elements to a stack I get segmentation fault, but if I open address for stack(i marked them with "!!!") and it's symbols it accepts it. But this time in each push, it creates new address and doesn't increase top value.
typedef struct
{
struct table **symbols; // array of the stack
int top; //index of th...
Objective-C: I need help retaining the value of an int. It's changing on me without my command.
The original question was: "How do you declare and retain an int?", that was satisfied in another post here: http://stackoverflow.com/questions/1938513
Now I have a problem where an int that was 18 is changing to 2, somehow on its own.
Her...
Reviewing a quite old project I found the following curious code snippet (only relevant code extracted):
class CCuriousClass {
~CCuriousClass();
CSomeType* object;
};
CCuriousClass::~CCuriousClass()
{
while( object != NULL ) {
delete object;
}
}
Have I overseen anything or is it a plain road to undefined behav...
Hi,
I was going to start with Win32 app development. Before I could get the first window to display i was ready to give up! I was overwhelmed by the number of datatypes you need to know about before you can write a simple WinMain and WndProc. (unless you copy-paste of course!)
Especially these -
LPSTR
LPCSTR
LPWSTR
LPCWSTR
Can so...
I have the following contrived example (coming from real code):
template <class T>
class Base {
public:
Base(int a):x(a) {}
Base(Base<T> * &other) { }
virtual ~Base() {}
private:
int x;
};
template <class T>
class Derived:public Base<T>{
public:
Derived(int x):Base<T>(x) {}
Derived(Derived<T>* &other): Base<T>(other) {}
...
I have a class like this:
class Inner;
class Cont
{
public:
Cont();
virtual ~Cont();
private:
Inner* m_inner;
};
in the .cpp, the constructor creates an instance of Inner with new and the destructor deletes it. This is working pretty well.
Now I want to change this code to use auto_ptr so I write:
class Inner;
class Con...
I want to use "_test_and_set lock" assembly language implementation with atomic swap assembly instruction in my C/C++ program.
class LockImpl
{
public:
static void lockResource(DWORD resourceLock )
{
__asm
{
InUseLoop: mov eax, 0;0=In Use
xchg eax, resourceLock
cmp ea...
Hey,
What is the result of that line:
int* ptr;
printf("%x, %x\n", ptr, &ptr);
ptr is actually an adress in a memory.
So what is &ptr ??
Thanks in advance
...
ASSERT(pointer);
pointer->x;
In this code, the ASSERT seems to be redundant. If the pointer is NULL, pointer->x will fail anyway. Is my argument correct?
...
I have to upgrade a legacy VB6 app to VB.NET; this app uses a function call from a .dll that takes a memory address as one of it's parameters. The VB6 app does this with the VarPtr() function, but this function does not exist in .NET. How do I retrieve the memory location of a variable in .NET?
-Edit1
For example
aVariable1 = aFunct...
How to delete a node in a singly link list with only one pointer pointing to node to be deleted?
[Start and end pointers are not known, the available information is pointer to node which should be deleted]
...