Sign in to follow this  
Koobazaur

Weird Compiling o_0 ?

Recommended Posts

Koobazaur    1264
I am using Dev-C++ and it seems to compile and in strange way... I have this include file that holds some classes. It is included by 2 other files (including main.cpp). Yesterday, I added another class to it that is not yet used by any of the other files. It compiled just fine. Today, I changed some code in some other files and when I compiled, I got a whole list of errors regarding that class. I didn't get a single error when I compiled it yesterday o_0. So does Dev-C++ skip the error check on compilation every Saturday? Or was it because it was one day before EAster? Enlighten me please!

Share this post


Link to post
Share on other sites
Drew_Benton    1861
What happens is that if your class has errors in it, and you do not use it, then it will still compile fine. If you use it, and it has errors, you will know about it. That's what happened, yesterday, the class was not being used, and it had errors in it. When you went to use it today, those errors popped up. It is not a Dev-CPP specific problem, it just depends on what's being compiled. I had the same exact thing happen to me a couple of days ago - so I know how weird it is.

Share this post


Link to post
Share on other sites
Koobazaur    1264
Quote:
Original post by Drew_Benton
What happens is that if your class has errors in it, and you do not use it, then it will still compile fine. If you use it, and it has errors, you will know about it. That's what happened, yesterday, the class was not being used, and it had errors in it. When you went to use it today, those errors popped up. It is not a Dev-CPP specific problem, it just depends on what's being compiled. I had the same exact thing happen to me a couple of days ago - so I know how weird it is.


But the thing is... I'm STILL not using the class.

[EDIT: Well, I made a variable of that class a memeber of the other class, so I guess that would count as using it. Neverming then]


As for the error, well, I put &Variable instead of *Variable when passing a value of a pointer to a vector (silly error).


EDIT:

Actually, I'll throw in another question instead of starting a new thread.

When I make a pointer, create it and pass it to a vector (*Pointer), can I go ahead and delete it? In other words, when passing values to a vector (push_back), they are fully copied into it, right?
The same would apply to an HBITMAP, no? So I should DeleteObject a bitmap and delete a pointer that I push_back to a vector, right?

Share this post


Link to post
Share on other sites
chad_420    290
if you are doin this...

int xr = 10;
int *xp;

xp = &xr;

std::vector<int> v;

v.push_back(xp);

when you print *v[0] you'd get ten...
when you print v[0] you should get the address of xr

if you...
xr = 1;

printing *v[0] should give you one.

so if you freed xr the pointer in your vector would point to nothing(which is an exception, or crash)

But i belong in begginers as mush as the next person so if this is confusing or wrong ignore and let me know please.

Share this post


Link to post
Share on other sites
Lenox    166
Quote:
Original post by Koobazaur
EDIT:

Actually, I'll throw in another question instead of starting a new thread.

When I make a pointer, create it and pass it to a vector (*Pointer), can I go ahead and delete it? In other words, when passing values to a vector (push_back), they are fully copied into it, right?
The same would apply to an HBITMAP, no? So I should DeleteObject a bitmap and delete a pointer that I push_back to a vector, right?


Well, not exactly. I may be wrong, but if you pass a *pointer into the vector, and then delete the pointer, you'll get an access violation when you try to access it in the vector ( this is from past experiences ). ( I have had that happen to me before, where I created a pointer to a struct, stored it in a ( well, it was a map, but it's the same for any container ) map, then free()'d the memory for the struct, I'd get a read access violation whenever I iterated through the container and came across said struct. ) ( Also, that may be different for all but structs, but I doubt it. )

[edit]

Quote:

Original post by chad_240

xp = &xr;

std::vector<int> v;


That would give you an error when you try to insert xp into the vector, because right there you aren't storing a pointer, but just an int. So you'd have to have it like this:


int xr = 10;
int *xp;

xp = &xr;

std::vector<int*> v;

v.push_back(xp);





Also, you said "If you free it inside the vector" - for clarification, it doesn't have to be inside the vector.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Koobazaur
When I make a pointer, create it and pass it to a vector (*Pointer), can I go ahead and delete it? In other words, when passing values to a vector (push_back), they are fully copied into it, right?


Yes. But.

If you have a vector of pointers, then a vector will contain a copy of the pointer. There is still only one instance of the object being pointed at. Thus:

Quote:
The same would apply to an HBITMAP, no? So I should DeleteObject a bitmap and delete a pointer that I push_back to a vector, right?


No. "delete foo" does not really delete foo, it deletes what foo points to. The Windows bitmap objects already provide their own cleanup via DeleteObject; you should use that, and not try to delete an HBITMAP* at all. (Unless the documentation tells you otherwise. I don't know exactly what DeleteObject does). When the vector is destroyed, it will call destructors on everything contained within. For pointers, the "destructor" is a no-op; all that needs to happen is that that bit of memory gets freed up.

Share this post


Link to post
Share on other sites
Koobazaur    1264
You misunderstood me a bit...

What I meant was:


vector<int> V;
int * pInt; //pint!
pInt = new int;
*pInt = 4;

V.push_back(*pInt);


should I now call delete pInt; ?

Similarly:


HBITMAP Bmp = CreateCompatibleBitmap(0);
vector<HBITMAP> V;

V.push_back(Bmp);


Should I now call DeleteObject(Bmp); ?

In the pointer example I have already tried deleting the pointer and I get no access violation. But I just want to make sure (especially since vectors are weird when it comes to accessing areas that are not supposed to exist).

Share this post


Link to post
Share on other sites
Moomin    332
vector<int> V;
int * pInt; //pint!
pInt = new int;
*pInt = 4;

V.push_back(*pInt);



should I now call delete pInt; ?

Yes, unlesss of course you still need the original pointer to pInt. Deleting pInt will NOT remove the entry/make the entry null in the vector because the entry in the vector is no longer a pointer and will have a different address.


HBITMAP Bmp = CreateCompatibleBitmap(0);
vector<HBITMAP> V;
V.push_back(Bmp);



Should I now call DeleteObject(Bmp); ?

No because the value stored in both HBITMAP's will be the same and if you DeleteObject on the bitmap it will delete win32's reference to both HBITMAP's meaning you will be able to use neither.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this