pointers

Started by
19 comments, last by BloodLust666 17 years, 8 months ago
You are writing to two NULL pointers, which is a Bad ThingTM. Have these pointers point to something beforehand.

Also, you seem to be memcpy'ing classes. This is a Worse ThingTM, since classes often have no definite memory layout and/or contain pointers. Classes should be copied using their copy constructor.
Advertisement
Never use memcpy to copy classes!

You're telling it to write to a NULL memory address, twice. You will be getting a NULL pointer exception.

Your solution will look like the following.
newpointer = new myInternalClass(*oldpointer);
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
only bad thing is when i call that "new" i'd have to delete it too, and i wouldn't know which instances i copied from and which one's i created and didn't use "new" so i woudln't know which ones to call "delete" on
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Well, if you don't use new, you have no memory to write to, so it crashes.
well, how will i know which ones i called new to and which ones not?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Remember that you don't have to call new or delete for every pointer variable you use. You have to call new and delete for every dynamicly allocated value you use. You could have multiple pointers point to the same object, but you only need to use new and delete once.

I could be wrong, but I get the impression that you're lacking a basic understanding of pointers and freestore memory. I'd recommend looking up tutorials on that.
The point is you're not supposed to assign the created objects to another class, you should create new ones.

class A { public:  A() {   member0 = 0;  }    A(const A &src) {   member0 = src.member0;  }  private:  int member0;}class B { public:  B() {   member0 = new A;      member1 = new int[10];   for(int i=0;i<10;i++)    member1 = 0;      member2 = 0;  }    B(const B &src) {   member0 = new A(*src.member0);      member1 = new int[10];   for(int i=0;i<10;i++)    member1 = src.member1;      member2 = src.member2;  }    ~B() {   delete member0;   delete[] member1;  }  private:  A *member0;  int *member1  int member2;}


This way you can just create a copy of your class with
B *newB = new B(*oldB);

Additionaly it's better to use initalization lists, if you know what those are.

If you don't want to create new copies of the objects inside the class you should not make a copy, but rather a swap.
Create a new empty class object, then swap the internal variables with the old one, and then delete the old one.
ok... there's no way i'm going to be able to do this because i can't get a hold of the class at all.... it's an API struct.

here's what i'm REALLY trying to do.. maybe someone can help.

ok, i have a template that has an Add() function, internally it adds to a map<> template with a std::string and Type (the template part). the Add() created a new item using "new" and puts it into the map<>. BUT now what i'm trying to do is make an Insert() that lets you put a pointer to an already created object and add that into the map<> but i noticed when i get that object back, it doesn't have the values i put in it. here's my code.

void Insert(std::string str, Type* Item){	Type *newType = new Type(*Item); // this is where i want to have a point                                         // get created and have the same values                                         // as the old one	m_MapList.insert( std::pair<std::string, Type*>(str, newType));};


this would work and all, but because of the struct i'm using that apart of an API, i can't get a hold of the copy structure. sooo... is there another way i could do it with the map<> maybe?

thanks

edit: sry, i had other stuff that wasn't right in there
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
So are you saying the class you are using does not have a copy-constructor / assignment operator?

If the API class you are using IS copyable - then you could use either a std::map<std::string, Type> or std::map<std::string, Type*> depending on the semantics you want for ownership, and the efficiency you need to achieve.

std::map<std::string, Type> would always use value / copy semantics

std::map<std::string, Type*> would use reference semantics by default, but if you implemented it as you showed above (using new Type(...)) then you are getting value semantics with a pointer.

If the API class you are using is not copyable, then you cannot copy it. Therefore your only choice is std::map<std::string, Type*> AND you cannot make it have value semantics (because you cannot call new) ... you can ONLY use reference semantics ... so you would need to deal with ownership management issues rather than copy-creation issuse.

For instance you could use a smarter pointer .... many to choose from, but boost seems to be the way to go ...

then when you Add() the element, you do whatever management required of your smart poitner (often nothing but strait assignment).
my template is defined as map<std::string, Type*>, i'm using all pointers. doing so there's no way of taking a value from one, creating a new one and putting those values into my map?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML

This topic is closed to new replies.

Advertisement