rje7 commented: good.. thanks buddy +1
knight fyre commented: help boost my understanding +1
Before you decide that threads are the magic bullet to fix all your problems, a cautionary tale, and some other approaches (like event based programming) to consider.
http://www.kuro5hin.org/story/2002/11/18/22112/860
If you thought normal debugging was hard, then you've seen nothing which compares to trying to debug race conditions. A more colloquial term would be Heisenbug.
There's a whole bunch of free compilers available, so cost is not the issue.
The problem is your tutor refuses to leave their comfort zone of teaching the only compiler they know. Very comfortable for them, but increasingly useless to the students who have a hell of a lot of catchup in the real world.
> So please.. why the stack is filling so soon when floofill() is successfully
> working for x-y<15 but not for x-y>15 ??
Put a counter in the function and see how many recursive calls you get.
The larger the counter gets, the more stack it eats.
You mean like using a decent 32-bit compiler on your 32-bit OS, complete with a wide range of graphics libraries.
floodfill() is recursive, massively so.
Unless you do something, your default stack size of a few KB will disappear all too rapidly.
Even if you increase the size of the stack to it's max of 64K, you're not going to fix the underlying problem.
Looks like a good start to me - keep going, you'll get there.
It might be worth reading, but I wouldn't spend too long on it.
Like learning asm when you're programming in C++. You don't need it, but if you know a few things, you'll have an appreciation of some of the finer points.
> char* plaintext;
Use a std::string to store the information, rather than a bare char pointer which isn't allocated any memory.
That, as soon as you call fork(), you have no control over the scheduling of the parent and child processes.
Anything can happen in any order, and can vary from one run to another.
Why are there two j++ in your code?
> i was not able to understand the :i(j) in line 4
It initialises i (you member variable) with j (the parameter).
Compare with integer (int j = 0) { i = j; };
which is an assignment.
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
> const which is at the end of line 5. What does these mean?
It means that the function will not modify the internal state of the class.
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10
> because long is the same as int(cheked with the ANSI standart) so no problem there.
The standard makes no such guarantee.
The standard only mandates MINIMUM sizes for various types. In the case of int, that's 16 bits and for long, it's 32 bits. That int and long are the same size on your machine is a happy coincidence.
> I have the bloodshed DEV C++ v4 and run it on a win xp 32bit.
Excellent information.
http://msdn2.microsoft.com/en-us/library/56e442dc(VS.71).aspx
Although Dev-C++ uses the GCC compiler, it also uses the Microsoft run-time. Which means you have the bugs and portability issues of that run-time to deal with.
unsigned long long int foo = 1234;
printf( "%I64u\n", foo );
Two things wrong with dTime
1. You're returning the value of an uninitialised variable.
2. You're ignoring that result (such as it is) in main.
All the printf formats for the data types you're trying to print are also wrong.
Also, tell us which OS/Compiler you're using because "long long" is a C99 feature, and support for that is not that widespread.
> scanf(i);
Have you any idea how dangerous this is?
> What is the cause of above weird results and how to avoid it?
I see nothing weird about it at all, and it implements what you wanted to implement.
Your mis-conception is that every write by the parent immediately causes a context switch to the child. It would be simply inefficient if this happened on a per character basis.
The parent will wait if the pipe becomes full, the child will wait if the pipe becomes empty.
The parent MAY run if there is still room in the pipe.
The child MAY run if there is data in the pipe.
The inner loop should not be there - increment i once each time around the while loop.
The second loop needs to count down from however many words you found.
You MUST NOT use gets(), use fgets() instead. Countless examples of how to use it can be found.
struct places
{
char places1[100];
char places[10][10];
Pick a different name.
Your use of feof() is wrong as well.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351
scanf( "%d",year);
You need &,
as in
scanf( "%d", [COLOR="Red"]&[/COLOR]year);[/ICODE]
You should also start using a compiler which will warn you about printf / scanf format string errors before you get to run your code and have it fail in a variety of interesting ways.
gcc -W -Wall -ansi -pedantic -O2 prog.c
is usually a revelation the first time you try it
Post an attempt - and mention your OS/Compiler..
Well that would depend entirely on your OS.
gcc in itself has no idea about graphics of any sort, it's all down to one or more libraries which interface to your specific OS.
A simple example might be to use ncurses, which is a nice simple console based API for generating user interfaces.
As for your 'OO' question,
typedef struct rec{
char FName[10], LName[10], address[50];
unsigned int TNum;
}REC;
typedef struct node{
struct node* left,* right;
char fname[10] ,lname[10];
unsigned int tnum;
char add[50];
}bintree;
There is no point to redeclaring all the members of rec inside bintree. For example
typedef struct rec{
char FName[10], LName[10], address[50];
unsigned int TNum;
}REC;
typedef struct node{
struct node* left,* right;
REC rec;
}bintree;
For one thing, it means you can then assign all the members of rec in one simple assignment rather than one at a time.
2. Replace all those char arrays with std::string - this is C++.
> REC r;
> bt trec;
There is no point in maintaining r as a member of each tree. It should be a local variable in the function which reads the file.
http://en.wikipedia.org/wiki/Model-view-controller
Think about separating your data from the presentation of that data.
Adding all that screen handling to your TD class is just going to make the whole thing messy.
Well you read in '1' and '2' and say that's 12
Then press the 'A' key, and note that you want to perform an addition
Then read '3' and '4' and get 34
Then press say 'E' to get an equals, perform the addition of 12 and 34, display the answer.
I guess you use 0 to 9 for 0 to 9, then say A for Addition, B for suBtraction etc.
Do you also have somewhere to display the answer?
It gets stored in a memory block of your choice.
You then need to write that out to a BMP file, along with the correct BMP header.
Between every fwrite() and fread() call, there should be a fflush() call.
I hope this is limited to a small number of unsorted records at the end of the file, because this approach is going to take some time.
> send(sClient, buffer, strlen(buffer), 0)
1. You already have the size of the buffer in your size variable.
2. strlen() will return at the first \0 in your binary data, or the first \0 in some random location beyond the end of your data
3. send doesn't guarantee to send all the data in a single call.
totalSent = 0;
numSent = 0;
toBeSent = size;
while ( toBeSent > 0 ) {
numSent = send ( sClient, &buffer[totalSent], toBeSent, 0);
if ( numSent == 0 ) { return "socket closed"; }
if ( numSent == SOCKET_ERROR ) { return "error"; }
totalSent += numSent;
toBeSent -= numSent;
}
Similarly, the recv() does NOT guarantee to reassemble the whole message in one call, that's your job using a similar kind of loop.
Remember, it's binary data, so ALL your strlen() calls are wrong.
And your modified code looks like what now?
feof() doesn't do what you think it should do.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351
feof() can only return true AFTER some other operation (say fread) has already failed. But you ignore that fread result, and thus what is left on the failed read is actually the same as the last successful read (hence the last line / record is repeated).
Use something like while ( fread( §ion, sizeof (ADMISSION_DATA), 1, logger ) == 1 )
Generally speaking, you should be checking for success on all your other file operations as well.
http://www.daniweb.com/forums/thread116609.html
Not so much a hint, but you'll get more help if you post an example which focusses on the real problem rather than merely dumping your current program state.
Almost certainly, but you're going to have to tell us which OS/Compiler you're using.
And try to be specific, and not just say "Windows" and "Microsoft" for example.
Maybe start with reading
- Intro threads
- how to post code threads
- the watermarks at the back of the edit window
All of which tell you about using [code]
[/code] tags when posting code.
Everything except float and double store integers only (of varying sizes).
The unsigned variants are restricted to positive integers (and zero) only.
for (int posDivisor = 0; posDivisor <= number / 2; posDivisor ++) {
if (number % posDivisor == 0)
Modulo zero is the same as division by zero.
Both are wrong.
http://cboard.cprogramming.com/showthread.php?t=88495
You've basically copy/pasted the same error 4 times without first checking that the original copy was in any way good.
Compiling is essentially free, so it's fine to do it all the way through your program development, not just when you're ready to run.
If you only type in 5 lines, and get an error, then you know pretty much where to look. But type in lots of code and get lots of errors, then you're back here posting your code.
Create a class called 'deck'
Initialise it with 52 cards.
Implement a function called 'shuffle', which randomises the order of 52 cards.
Implement a function called 'deal', which gives the next card in the pack. Calling deal 52 times will give you the whole pack, in a random order.
You don't need a random number generator with a series of magic properties.
> My program works properly under windows, however, it seg faults when
> I try to compile/run it under linux.
Unfortunately, that just makes you lucky, not good.
Code sometimes works, despite your best attempts to muck it up. Yet at other times, even the most minor transgression is severely punished.
> strcpy(location, (location + strlen(fileline)));
Overlapping copies using strcpy() are undefined (meaning anything can happen - like working vs. seg fault).
Try copying via a second array.
Why are you using messy C char arrays in a C++ program by the way?
Like I said, if we can't see YOUR code, we can't tell you how to fix it.
In your link, scroll down to "How do I find the day of the week for any date?"
Calculate the result for 1st January for your input year.
Since we can't see either the formula you've chosen, or how you've implemented it, there isn't much we can do.
Post your code.
Look at std::vector and the push_back method.
You need to close the file before trying to rename it.
The first question is why are you using something as clunky as realloc in a C++ program?
The second problem is you're using realloc in an unsafe manner.
You need to do this.
void *temp = realloc ( p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts );
if ( temp != NULL ) {
// success, update the pointer
p_Verts = temp;
} else {
// p_Verts is still pointing at the OLD memory. This needs to be saved
// or free'd as appropriate
}
The third problem is you're calling the function by value, not by reference.
void func ( OURCUSTOMVERTEX *&p_Verts ) {
// your code
void *temp = realloc ( p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts );
if ( temp != NULL ) {
// success, update the pointer
p_Verts = temp;
} else {
// p_Verts is still pointing at the OLD memory. This needs to be saved
// or free'd as appropriate
}
// your code
}
Thus, if the allocated memory really does move when realloc is called, the actual pointer to that memory (from the caller's point of view) will also change.
In your code, this change was NOT reflected in the caller, and it probably ended up using an invalid pointer as a result.
You didn't change the Speech() prototype at the same time.
It still thinks the function expects only 1 parameter.
> In case you didn't notice, the fellow wanted the code in C.
He also wanted it 3 YEARS ago as well.
Not only late, and in the wrong language, it's also old C++, and non-portable.
Use '0', not 48 (like was used oh so long ago in a galaxy far far away).
A few questions for your college / tutor.
Why are you teaching using tools, languages and operating systems which are at least 15 years out of date?
How do you consider this to be an effective preparation for the world after leaving college?
Have you ever had feedback from recent ex-students as to how they found the course, and how well it prepared them for their career?
When was the last time the curriculum was revised to meet the modern demands of students and prospective employers. When is the next such review due?
With the prevalence of many excellent, up to date and most importantly, free tools available, what other reasons are there for sticking with the old tools?
Regardless of whatever waffle you get in response, one likely reason is that the tutors themselves don't know enough C++ to move away from their comfort zone of that particular compiler. Their lack of real C++ knowledge would be exposed were they to use another compiler. It's easy to come to this conclusion because of all the "well my tutor said..." rubbish which is reported by students when we correct them here on the message board.
> so that i can display it using outtextxy() ...is that possible?
Of course it is.
If you've already managed to display a fixed string, then replacing that with a char array filled with data read from a file should be a breeze.
What exact …
> -- invalid conversion from `const char' to `const CHAR*'--
The [n] subscript is doing that.
You need to generate the filename string, with n appended, before you pass it to CopyFile().
In C, I'd use say sprintf(), but in C++ I think a string stream would be more appropriate.
> but i just wondered if there is a class specification 'how - to' template I could read up on?
Your company's coding guidelines should have this kind of information.
It should cover things like style, content, presentation etc.
Bear in mind that if you type in ./myprog *nabil
at most Linux/Unix prompts, then the default is for the shell to have a go at expanding the wildcard expression to begin with. If that fails, then it's likely that your program will see argv[1] as being NULL, and that's where your segfault is coming from.
Perhaps check argc / argv before trying to use them in your code.
In bash, you can modify the shell's wildcard expansion, to allow such expressions to be passed to your program.
$ echo *.c
bar.c foo.c new.c util.c wrapper.c
# Turn if off
$ set -o noglob
$ echo *.c
*.c
# Turn it back on
$ set +o noglob
$ echo *.c
bar.c foo.c new.c util.c wrapper.c
Oh, and yes the code is better in that it is in code tags, but still looks pretty horrible because there is no indentation.
OK, you've been a member for 6 months now, were you at any point planning to read the rules for posting code ?