|
Even that your code made no sense as-is (due to lack of formatting and HTML "stealing" your "<" and ">" chars), the error is most definitely in your use of memcpy.
If you want to copy objects, copy objects. As a rule-of-thumb (and unless you're prepared to not being able to maintain the code yourself a short while from now), never ever memcpy a non-POD type.
Replace your DogHouse constructor (the one taking a Dog* and a size_t) with a version that really does the copy, like:
std::copy(lpDog, lpDog + size, p);
and you should at least be closer to a solution. Though, since you are already copying Dog's around here, I see no reason to not use the standard vector .
|
|
|
|
|
Never copy objects of complexed types (user defined type, ie. class, union, OOP struct, ... ) using memcpy.
Especially :
1) that contains new / delete operations for its member data.
2) a inherited one (it is derived from some kind of base class.
Please read Stanley Lippman's "Inside C++ Object Model".
Maxwell Chen
Lets make bugs better!
|
|
|
|
|
Hi There,
Thanks for the replies.
First of all, apologizes for the previous chunck of code, it was all messed up and did not make much of sense.
The reason because a wanted to keep using 'pointers' instead of a 'vector container' was because of the initialization array.
[code]
arrDog [2][5] = {
{ Dog("00"), Dog("01"), Dog("02"), Dog("03"), Dog("04"), }
{ Dog("10"), Dog("11"), Dog("12"), Dog("13"), Dog("14") }
{......}
}
[/code]
In this case initialization array is very small and could easily be changed using vector declaration.
[code]
vector <Dog> arrDog[2];
arrDog[0].push_back(Dog("00"));
arrDog[0].push_back(Dog("01"));
etc...
[/code]
Any way to avoid this anoying declaration?
In my real case, arrDog is about [100][100] and the class used isn´t as simple as Dog.
That is the only reason, just avoid rewriting all the stuff again.
I know this is a *very* poor reason, please don´t flame at me
Althought I will use vectors I have found that using std::copy instead of memcpy things seems to work a bit better.
Does anybody know the gory details of std::copy?
Does std::copy calls copy-constructors members?
Using memcpy instead of std::copy I get rubish when printing stuff saved in vectors but I don´t get memory error, any reason?
[code]
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using std::string;
using std::cout;
using std::vector;
using std::ostream;
using std::endl;
class Dog {
string nm;
public:
Dog ( void ) : nm("NO NAME") {}
Dog(const string& name) : nm(name) {
cout << "Creating Dog: " << *this << endl;
}
Dog(const Dog& d) : nm("copy " + d.nm) {
cout << "Copy-constructed Dog " << nm << endl;
}
~Dog() {
cout << "Deleting Dog: " << *this << endl;
}
friend ostream&
operator<<(ostream& os, const Dog& d) {
return os << "[" << d.nm << "]";
}
};
class DogHouse {
Dog * m_p; //Dog Array
int m_n; //Number of dogs
string m_houseName;
public:
DogHouse (const Dog * p, const int n , const string& hn)
: m_p(new Dog[n * sizeof(Dog)]) ,m_n (n), m_houseName(hn) {
std::copy(p, p + n, m_p);
//memcpy(m_p, p , n * sizeof(Dog)); Uncomment this
}
DogHouse(const DogHouse& dh)
: m_p(new Dog[dh.m_n * sizeof(Dog)]), m_n(dh.m_n),
m_houseName(dh.m_houseName + " copy-constructed") {
std::copy(dh.m_p,dh.m_p+dh.m_n,m_p);
//memcpy(m_p, dh.m_p, dh.m_n * sizeof(Dog)); Uncomment this
}
~DogHouse() { delete [] m_p; }
friend ostream&
operator<<(ostream& os, const DogHouse& dh) {
os << "[" << dh.m_houseName << "] contains " << endl;
std::copy(dh.m_p,dh.m_p + dh.m_n,
std::ostream_iterator<Dog>(os,"\n"));
return os;
}
};
int main() {
Dog arrDog[4][5] = {
{ Dog("01"),Dog("02"),Dog("03"),Dog("04"),Dog("05") },
{ Dog("11"),Dog("12"),Dog("13"),Dog("14"),Dog("15") },
{ Dog("21"),Dog("22"),Dog("23"),Dog("24"),Dog("25") },
{ Dog("31"),Dog("32"),Dog("33"),Dog("34"),Dog("35") }
};
vector <DogHouse> HouseContainer;
HouseContainer.push_back(DogHouse(arrDog[0], 5 ," Dog Row 0 "));
HouseContainer.push_back(DogHouse(arrDog[1], 5 ," Dog Row 1 "));
HouseContainer.push_back(DogHouse(arrDog[3], 5 ," Dog Row 3 "));
std::copy(HouseContainer.begin(),
HouseContainer.end(),
std::ostream_iterator<DogHouse>(cout,"\n"));
return 1;
}
[/code]
Regards
Carlos.
|
|
|
|
|
Your choice for the global array is perfectly valid since it's a compile-time static set, no need to use a vector here.
However, I think you'd be better off to make the Dog array m_p inside doghouse a standard container. It would make your code easier to maintain, it would take care of all resource management, and (unless you really wanted to) it would allow you to use the compiler generated versions of copy-contructor, assignment operator and destructor.
Does anybody know the gory details of std::copy?
Yes, and so do you if you look it up in the documentation or even the header files. It's defined by including <algorithm>. If you're using the Dinkum library it's acually defined in <xutility>, though you are only to get to its definition using <algorithm>. It's basically a copy loop.
|
|
|
|
|
Hi,
I am looking to code a method to record audio in a suitable compressed format comaparable to radio quality, with the minimum amount of fuss.
Can someone point me in the right direction? Which libraries and codecs etc.
Thanks,
Richard
|
|
|
|
|
MPEG 1.0 Layer III is the most suitable codec for audio, good quality, small filesize.
|
|
|
|
|
I have an application where i need to store a string for each item in a listview control and later retrieve it and use it again. What would be the best soulution for that? Btw, i wan't the value to be hidden, in other words it can only be accesses inside the program.
Thankyou!
|
|
|
|
|
here's one way: use the Item Data tag on each list item to store an index into an array of strings.
here's another: use the Item Data tag on each list item to store unique ID and use that ID as a key into a std::map of strings.
here's another: use the item data tag on each list item to store a pointer to its string
the last way is probably the easiest, since you don't have to worry about keeping the index order correct (as in the first way) and you don't have to hassle with STL maps (like the second).
-c
ABSURDITY:
A statement or belief manifestly inconsistent with one's own opinion.
|
|
|
|
|
Thankyou. Actually i need to store many items in the listview, maby 1000, so what solution do you think would be the best?
|
|
|
|
|
in theory, they will all work equally well. depending on how items are added and removed and managed in your application, one may work better than the other. you'll have to pick the one that fits best with your app.
-c
ABSURDITY:
A statement or belief manifestly inconsistent with one's own opinion.
|
|
|
|
|
that std::map choice, is that something where you can add a string and then retrieve it again using an index number? if so, do you have documentation on it?
Thanks
|
|
|
|
|
yes. a map allows you to create key/value pairs. the key and value can be of any type (int and string, or int and pointer, string and int, etc.)
there are probably a few articles here on std::map. if not, there are samples in MSDN.
-c
ABSURDITY:
A statement or belief manifestly inconsistent with one's own opinion.
|
|
|
|
|
Does anyone know why pressing "ENTER" closes a dialog-based MFC application ? And I removed all controls, if someone thinks of that ...
Even with controls, if I "ENTER" text in a text control closes the application.
|
|
|
|
|
Override OnOK and delete all the code inside it.
|
|
|
|
|
override PreTranslateMessage. add this code:
if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN))
{
return TRUE;
}
-c
ABSURDITY:
A statement or belief manifestly inconsistent with one's own opinion.
|
|
|
|
|
Read this article :-
http://www.codeproject.com/useritems/pretransdialog01.asp
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
Hi
Is there a tool with which I can find all the bmp file in one directroy (include the bmp file in exe ,dll)?
Thanks
benben
|
|
|
|
|
ive written a tool a long tima ago in vb that searchs ALL files ina directory for bitmaps and extracts them:
Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Sub cmdExtract_Click()
c = 1
a = 1
OO = 1
Dim FileHeader As BITMAPFILEHEADER 'Bitmap file header structure
myname = Dir(txtPath & "*.*")
Do While myname <> ""
a = 1
OO = 1
Open txtPath.Text & myname For Binary As #1
Dim ss As String
ss = Space(LOF(1))
Get #1, , ss
Do While a <> 0
a = InStr(OO, ss, "BM", vbBinaryCompare)
If a = 0 Then Exit Do
Get #1, a, FileHeader
If FileHeader.bfSize < 350000 And FileHeader.bfSize > 150 Then
OO = a + FileHeader.bfSize
bmp1 = Mid(ss, a, FileHeader.bfSize)
c = c + 1
FullPath = txtDest.Text & "pic" & c & ".bmp"
Open FullPath For Output As #2
Print #2, bmp1
Close #2
Else
OO = a + 1
End If
Loop
Close
myname = Dir
Loop
Close #1
End Sub
make a textbox call txtPath that you'll put the dir to search and another called txtDest for the directory to extract to, and button called cmdExtract to start extraction .
its the worse way to do it, but it works rather well
|
|
|
|
|
Hi,
I have a dll ,which works fine in debug mode & it doesn't in release mode.How can i make the release build to work properly?.THe OS i work on is WIndows ME.
Regards
Neha
|
|
|
|
|
intialize all variables. check all pointers for out of range conditions.
you can also put debug symbols in your release build to see what happens: 1. turn off optimizations in Project / Settings / C/C++ 2. put debug symbols in the DLL from Project / Settings / Link.
-c
ABSURDITY:
A statement or belief manifestly inconsistent with one's own opinion.
|
|
|
|
|
HI FRIEND
There may be bcz of many facts...
in debug mode most of the assertions are skipped and they dunno show any errors..for example in many situations the buffer overflow is not reported by the DEBUG mode..but the same will cause some assertions in release
look in to the project>settings>link tab and make sure that all of the .lib files u linked in the time of DEBUG is also included in release..
i think u can get it now...
Trace The Bugs...
|
|
|
|
|
Dear All,
In a dialog-based application i have menu and some toolbars, now
i also want to add the status bars into the project. How should
i do?
Thanks in advance!
chen
|
|
|
|
|
Add a CStatusBar member to your dialog.
Now call Create or CreateEx in the OnInitDialog.
You can use GetStatusBarCtrl to get the underlying CStatusBarCtrl and call SetText on that
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|
Nish, I added a CStatusBar to the dialog, and call Create in the OnInitDialog, like this:
m_ctlStatusBar.Create(this);
Then I added a line of code:
m_ctlStatusBar.SetIndicators(indicators, 4);
which contains the cap, num, scrl standard status panes.
then in OnPaint, I did this:
CStatusBarCtrl& ctl = m_ctlStatusBar.GetStatusBarCtrl();
ctl.SetText("Status", 0, SBT_NOBORDERS);
but nothing happened. What did I miss?
thank you a lot in advance!
(hey, one thing I couldn't figure out is that how you post messages when it is night your time? you sleep very little? )
|
|
|
|
|
Dear Lucy,
You are correct. It won't give you a status bar that way. I have written an article that shows how to add a status bar to an MFC dialog based app.
http://www.codeproject.com/useritems/dlgstatbar01.asp
I hope that helps
Nish
Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.
|
|
|
|
|