|
|
Yeah, I know what you mean, I've recently taken my first C++ project in 2 years. I'm loving it, but it's a steep curve at first.
BTW, I downloaded and read your XAML article. I think I'll get it better when I actually use it a bit though.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Marc Clifton wrote:
What happened to good ol' realloc?
It is good only for POD types. In this case it would work, but if you have real objects, their destructors would have never been called. Here is a link to BS's FAQ explaining the matter:
Why doesn't C++ have an equivalent to realloc()?[^]
Marc Clifton wrote:
And isn't calling delete[] pointer the wrong thing to do, because that can actually call the destructors on the items in the array
That's exactly why it is a right thing to do - you want your destructors being called before the objects are destroyed - that's the whole point of having them.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
PJ Arends wrote:
As has been said previously using std::vector is the best and easiest way of doing this. But you also said you are a beginner starting to learn C++ and perhaps you want to learn how to do it yourself.
Generally, these days most C++ "gurus" advise to learn how to use Standard Library first, and only then mess with pointers. That's the approach from Koenig's Accelerated C++[^] and also the latest edition of Lippman's C++ Primer[^]
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Hi guys!
I see that most of people here are very smart programmer so my question may seem like a silly one but keep in mind that I am just started to learn C++.
I am writing a sorting program using dynamically allocated array. I know how to write a program when the size of the array is known. Well, this time I don't know the size of it. My program suppose to read the positive numbers from the keyboard and it need to quit only when i hit ^C. How do you declare your array and populate it when you don't know the size?!
I know that I should use something like this:
int *pointer;
int size;
pointer = new int [size];
but when I go to my for loop what do I say my size should be?
Any help would be greatly appriciated!!!!
Sveta
-- modified at 14:36 Sunday 11th September, 2005
|
|
|
|
|
|
Trollslayer wrote:
You might care to post this in the Visual C++[^] forum.
what? no axe swinging? I guess I need to go back to playing BloodRayne 2, no excitement here....
_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
|
|
|
|
|
|
Ummmmmmm. I am not even going to hazard a guess what that means ...
|
|
|
|
|
|
Marc Clifton wrote:
Trollslayer wrote:
The "No programming questions please." wasn't big enough...
Actually, it appears to have disappeared. Or at least, I can't see it. Hmmm....
Yes, in fact it is "Please do NOT post programming questions or ads to this forum.". Maybe too long and confusing sentence?
Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy
|
|
|
|
|
dnh wrote:
Yes, in fact it is "Please do NOT post programming questions or ads to this forum.". Maybe too long and confusing sentence?
Oh, I see it now!
Posting Guideline: The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. * Do not post programming questions (use the programming forums for that) and please don't post ads.
I stopped reading that paragraph where I placed the "*", thinking the rest of the paragraph was more rambling about PG and kid sisters. It used to be more obvious, IIRC.
Oh well. I'm blind. I admit it.
Marc
My website
Traceract
Understanding Simple Data Binding
Diary Of A CEO - Preface
|
|
|
|
|
Marc Clifton wrote:
I stopped reading that paragraph where I placed the "*", thinking the rest of the paragraph was more rambling about PG and kid sisters.
hmm maybe that's the reason why people post programming questions?
Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy
|
|
|
|
|
Sveta80 wrote:
I know that I should use something like this:
Actually, you should be using C# so you can write:
ArrayList myChars=new ArrayList();
...
myChars.Add(myKeyboardChar);
Isn't that so much simpler than pointers and constructors and destructors and malloc and realloc and STL and...(errr, oops!).
Marc
My website
Traceract
Understanding Simple Data Binding
Diary Of A CEO - Preface
|
|
|
|
|
Or alternatively in C++
vector<char> myChars;
...
myChars.push_back(myKeyboardChar);
I can see how much more complex the C++ solution is over
C# here.
It is a fact that there are almost not circumstances when
it is necessary to use new and delete in C++ anymore,
it is almost always better to use a Standard library
container, which handles all that stuff for you.
However, Microsoft's secret plan to replace C++ with
C# is that they make these containers very hard to use
within the debugger. Debugging support of the STL really
blows in VS 2003. Has anybody tried it out in VS 2005?
|
|
|
|
|
nde_plume wrote:
It is a fact that there are almost not circumstances when
it is necessary to use new and delete in C++ anymore,
Oh, I don't know about that. There's plenty of times when I need a countainer to maintain a reference to an object that isn't constructed on the stack.
nde_plume wrote:
However, Microsoft's secret plan to replace C++ with
C# is that they make these containers very hard to use
within the debugger.
Not really. STL has been a PITA to debug since the days when I was using Sun's STL. Drilling into a collection in STL is like bumbling around in a universe consisting of 3.2 dimensions. If anything, maybe Microsoft has made is easier, not harder, to debug.
Marc
My website
Traceract
Understanding Simple Data Binding
Diary Of A CEO - Preface
|
|
|
|
|
However, Microsoft's secret plan to replace C++ with
C# is that they make these containers very hard to use
within the debugger
You really think it isn't possible to present containers
in an easier way in the debugger? Heck, if you are using
a vector you can hack the debugger to display the contents
by typing this in the watch window:
my_vector._Myfirst,10
shows the first ten items in the vector in a fairly nice
way. There is no reason at all why the debugger can't do
that itself. It is much harder with tree structures like
maps and sets to view by hand in the debugger (though you
can walk throught it, so the data is certainly renderable)
it is just that MS didn't think it was very important.
|
|
|
|
|
|
Hi, thanks for posting your interesting C++ question in Lounge!
I'll be glad to help out!
You'll probably be looking for something like this:
while ( get_keybrd_inpt()->is_num_pos() ? true : false >> 0 ) {
int s = 1234 * (2 >> 8 / 3 % 4 <<1) * 3 ? 200: 7;
int&* p = new int[s];
for ( (*p.->len()++).ossfet+1; ; ) {
s = p->query_length_ctr() + 1 * (*p->offset);
p = new (int)(p)[s];
}
}
else ( break; ) { set_jmp(120); asm{ _int 3;} exit( if_ctl_C_ht() ); }
That should do the trick. Let us know how this works for you!
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
-- modified at 16:08 Sunday 11th September, 2005
|
|
|
|
|
Not sure at the end of the day if you feel your question has been answered. The answer is std::vector. Put #include <vector> at the top of your code, and then using std::vector just below. Then in your code you define a vector like this vector<type> = name; For example
#include <vector>
#include <string>
using std::vector;
using std::string;
int main()
{
vector<int> vecInt;
vector<string> vecString;
vecInt.push_back(1);
vecInt.push_back(2341);
vecInt.push_back(341);
vecInt.push_back(176);
vecString.push_back("fsdfsdfdf");
vecString.push_back("fgkkf");
vecString.push_back("45j4");
// Now this is how we use it
vector<int>::iterator begin = vecInt.begin();
int total = 0;
for(;begin != vecInt.end();++it)
{
total += *begin; // You need to dereference the iterator to get the value.
}
// You could also use the algorithm in <algorithm> to do stuff like this.
}
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Sveta80 wrote:
pointer = new int [size];
but when I go to my for loop what do I say my size should be?
You would loop from 0 to size times. For example:
int size = 10;
int *pointer = new int[size];
for (int x = 0; x < size; x++)
{
cout << "Enter number " << x << ": ";
cin >> pointer[x];
}
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
But the problem is that I don't know the actual size of my array. My size depends on the user input and I don't know in advance how many numbers my user is going to enter. User can enter 10, 20, 100 numbers. After the user finished entering the numbers, my code should read through them and whenever the input number is negative, the code should start sort the numbers it already got (except the negative number) and prints the orders.
|
|
|
|
|
But the problem is that I don't know the actual size of my array. My size depends on the user input and I don't know in advance how many numbers my user is going to enter. User can enter 10, 20, 100 numbers. After the user finished entering the numbers, my code should read through them and whenever the input number is negative, the code should start sort the numbers it already got (except the negative number) and prints the orders.
|
|
|
|
|
In that case, one of the other suggestions is more in line with what you need. You can, however, ask the user how many numbers s/he will be entering and allocate an array that big. Good luck.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
I'm creating an ActiveX control via ATL. It's something like ruler. So I draw a lot of scale marks and captions under it.
Also I implemented scrolling the window by mouse handling WM_MOUSEMOVE message and calling ScrollWindowEx function from there. And I paint all I need in WM_ERASEBACKGROUND message handler to overome flickering problems.
Everything works fine, but I have a little flickering effect.
And also everyting paints slowly, if one moves mouse fast.
How can I solve this problem?
|
|
|
|