Salem 5,265 Posting Sage

A short guide to const.

char a;
const char b;
char *p = &a;
const char *q = &b;
char * const r = &a;
const char * const s = &b;

Step 1, read right to left, so for example
r is a const pointer to char.

Step 2, note that const can appear in two different places.
- the const to the right of the * means the pointer itself is const, that is (*p)++ may be allowed (modifying what it points to), but p++ isn't (modifying the pointer).
Also, such pointers MUST be declared and initialised in one step. Being const, you cannot assign them later on.

- the const to the left of the * means the thing being pointed to is const, that is p++ may be allowed, but (*p)++ isn't.

In the example above, the two RED declarations allow you to modify what the pointer points to, and the two GREEN ones do NOT allow you to modify what the pointer points to.


> I just want to confirm that my understanding is correct or not
It was good.

Salem 5,265 Posting Sage

You're over-thinking the problem.

void arrSelectSort(float *, int);

int main()
{
  float *scores,  //To dynamically allocate an array  
  int numScores;  //To hold the number of test scores
  
  //sort the elements of the array pointers
  arrSelectSort ( scores, numScores );

  return 0;
}

void arrSelectSort(float *array, int size)
{
  // do stuff
}

The declaration of your parameter types just matches the declaration of the variables you intend to pass.

Further, if you used array in main, then you can just as easily use array in the function as well.

Salem 5,265 Posting Sage

You seem to have figured it out - what do you want me to suggest?

Salem 5,265 Posting Sage

Poster evaluates to a thread posted on two forums.

kvprajapati commented: Great eyes! +7
Salem 5,265 Posting Sage

http://c-faq.com/malloc/index.html

* Memory leak
Calling malloc() without calling free().

* Buffer OverFlow
Accesses outside the bounds of your memory. Can equally apply to arrays as well as allocated memory.
A typical example is a for loop which runs to <=N rather than <N
When (if ever) this actually blows up is entirely random.

* Segmentaion Falut
Accessing memory which doesn't exist (or dereferencing NULL).

* Bus Error
Accessing memory with the wrong alignment.
Usually triggered by doing dangerous pointer casts (say char* to int*), then dereferencing it.

Salem 5,265 Posting Sage

Well to be pedantic again, you should have written
const char *p="abc";
then the compiler itself would have complained about the attempt to modify a constant.

Because so much existing code (prior to C89) used this construct before the idea of putting "strings" in read-only memory came along, it was generally decided that it was better to not complain about the loss of const in such pointer assignments.

If you use some other type apart from char, it should really complain about the loss of const.

lighthead commented: nailed it! +2
Salem 5,265 Posting Sage

> NOte that here break 1 (p++) and break2 (*p++ ) doing the same thing.
Better add someone else to the reading list....

No they're not the same, one merely increments the pointer, the other one ALSO dereferences it at some point.

The second being equivalent to someImaginaryDummyWhichGetsThrownAway = *p++; A good compiler would have warned about a discarded result.

Oh, to be pedantic, you print a pointer like this printf("string:%s and address:%p\n",p,(void*)p); The rest of your analysis is wrong as well.
There is nothing wrong per se with ++*p++, so long as p is pointing to memory which can be MODIFIED. The original error was because p was pointing to read-only memory, and has nothing to do with the syntax of the expression being faulty.

John A commented: Bingo. +21
Salem 5,265 Posting Sage

g++ --ostrich-head-in-sand prog.cpp

Please tell us the name of the software you're working on, so we know to avoid it in future. I don't want to be anywhere near this train-wreck of a methodology.

Salem 5,265 Posting Sage

Shame about the indentation - good luck.

Salem 5,265 Posting Sage

It seems that most people other than Tom Gunn need to swing by here and have a nice long read.

http://c-faq.com/aryptr/index.html

tux4life commented: :$ +15
Agni commented: Nice Link !! +4
Salem 5,265 Posting Sage

http://www.daniweb.com/forums/announcement118-3.html
http://www.daniweb.com/forums/announcement118-3.html
http://www.daniweb.com/forums/announcement118-3.html
http://www.daniweb.com/forums/announcement118-3.html
http://www.daniweb.com/forums/announcement118-3.html
You already missed at least FIVE places where the board asks (nay, implores) you to use code tags, but you missed them all.

Try editing your posts again, and this time see if you can make them more readable.
Check out some other posts on the forum for readability, and make sure yours is just as good.

Salem 5,265 Posting Sage

> char *p = &q;
Did you try it?

&q has a different type to q, so any good compiler would complain about incompatible pointer assignments.

jephthah commented: keeping me honest :) +12
Salem 5,265 Posting Sage

The first ++ tried to modify what p points to.
Since p points to a string constant, your program dies because it tried to modify something in read-only memory.

Salem 5,265 Posting Sage

> Sample question:- Wat will the expression ABC AB AC will become after postfix operation??
You need to post some actual C code.

Salem 5,265 Posting Sage

> fout.write((char*) &temp, sizeof(temp));
This only works for POD data which doesn't contain any pointers.

Any class, or anything which contains pointers (even if you can't see them - so this rules out possibly all the STL types) cannot be simply saved in one step just by writing out a few bytes.

http://www.parashift.com/c++-faq-lite/serialization.html

Salem 5,265 Posting Sage

Only that strdup() isn't a portable function.

Salem 5,265 Posting Sage

Do those other devices have a "DVD-R" badge on them?

Salem 5,265 Posting Sage

We did all this less than a month ago.
http://www.daniweb.com/forums/thread198087.html

Nick Evan commented: We didn't brag about our systemspecs in that thread :) +21
tux4life commented: How could I forget :P +12
Salem 5,265 Posting Sage

> size = ftell(updf);
You do this BEFORE checking the file was opened successfully.

Besides, you just opened the file for reading, so the answer is trivially going to be zero.

> if(sizeof(updf)
This isn't going to tell you anything useful about the file.

> while(fread(idx, 1, size, updf))
Use fgets() to read a text file.

> fopen_s(&tmpf, oldf, "w");
I would generally advise avoiding non-portable vendor-specific functions.

Salem 5,265 Posting Sage

Exactly.

Salem 5,265 Posting Sage

You missed this
http://www.daniweb.com/forums/announcement8-3.html

Along with half a dozen other clues about code tags.

So now we have to wait until that gets fixed.

Salem 5,265 Posting Sage

At the tip, you've got

up_and_down(1)
  up_and_down(2)
    up_and_down(3)
      up_and_down(4)

There is no subtraction, just a nested set of values stored as local parameter 'n'.
As you return from each recursive call, you get back the previous value of n.

Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

http://forum.piriform.com/index.php?showforum=19
I'm guessing that's where you'll find the highest concentration of experts and users for that product.

Salem 5,265 Posting Sage

No error messages?

Which OS?
XP and Vista can be a bit twitchy (for good reason) about writing into the root directory of the boot disk.

Salem 5,265 Posting Sage

Split it up into steps

- read a line, and split at ","
- assign to fields in some structure (vector of vectors perhaps)

and so on.

Salem 5,265 Posting Sage

> Is MinGW Make = GNU Make?
I would guess so.

Salem 5,265 Posting Sage

Does test_timer.cpp have
#include "timer.cpp"
?

It shouldn't!.

Salem 5,265 Posting Sage

> for(ctr = 0; fp == 0; ctr++)
What does this do?

> flushall();
> gets(filename);
flushall() is non-standard, and gets() is the tool of the devil.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351

> #include<conio.h>
Why are you still using this obsolete header?
Is your compiler also obsolete?

Salem 5,265 Posting Sage

No, you blurted it out in response to Hiroshe's first suggestion without waiting for the OP to ask more questions.

If they're still confused, then your "advanced" answer is high and dry in the land of "huh?, wtf"
But hey, it's your time not mine - so that works for me.

Salem 5,265 Posting Sage

I see the OP got their wish by successive approximation without having to put in any more effort.

Congrats to all :icon_rolleyes:

tux4life commented: ROFL :P +11
Salem 5,265 Posting Sage

Try using code tags then.

> g++ -c -lrt timer.cpp test_timer.cpp
Why are you passing linker flags here?

Salem 5,265 Posting Sage

How about you give a definition (use a search engine).

Because that's about as weak and useless as a question as it's possible to get.

Salem 5,265 Posting Sage

The maximum number of open files a process is allowed to have comes from the Operating System.

It's usually possible to change this (but details vary), but doing so almost certainly requires "admin/root" privileges.

It's also something which shouldn't be done lightly to save a bit of inconvenience on your part.

Just consider it in your future designs that you don't have an unlimited number of open files to play with.

Salem 5,265 Posting Sage
csurfer commented: Super catch man !!! You do it all the time :) +2
Salem 5,265 Posting Sage

Does your Aircraft class define and implement a copy constructor?

Salem 5,265 Posting Sage

Or you could learn the portable way to do the same thing.

Then you don't have to worry about which OS is involved.

Salem 5,265 Posting Sage

> This ebook is the most suitable for you I think, superb explanations, cool code examples, answers to most of your C++ questions...
And patently wrong statement even in the first paragraph!
"Simply stated, to be a professional programmer implies competency in C++."

So all the C, Perl, Java, Python, Fortran, COBOL programmers in the world are amateur hacks?
Rubbish!

I wonder how "chummy" it gets with the Microsoft compiler, since it is hosted on their site?

Salem 5,265 Posting Sage

Why not just read a whole line with getline, then use a utility function which examines the line to see if quotes are present, and if so remove them.

All this peek() stuff is overly complicating things.

Salem 5,265 Posting Sage

Until you learn to post formatted code, no one is going to pay a blind bit of attention to you.
http://www.daniweb.com/forums/announcement8-3.html

Salem 5,265 Posting Sage

#define s scanf
#define p printf
And definitely don't do this either.

Your programs will end up as a meaningless stream of single letters and punctuation to everyone else from the start, and it will be the same for you the moment you stop working on the program.

Salem 5,265 Posting Sage

From control panel choose network "Network Connections" then "local area connection"

Choose properties (step 1 on the diagram)
Select the TCP/IP option, choose properties again (step 2)
Make sure "assigned automatically" is selected for everything.

Salem 5,265 Posting Sage

Is there some reason why the VM route isn't an option?
FYI, cost isn't one of them, there are good and $0 ones about.

Salem 5,265 Posting Sage

Tom Gunn, please read these:
http://www.daniweb.com/forums/thread78060.html
http://www.daniweb.com/forums/announcement118-2.html
There's a line between "help" and "spoon-feeding the complete answer".

Salem 5,265 Posting Sage

Except which one is "fastest on the current setup" is a moving goalpost with every patch / update / release / new compiler.

Plus if you're writing code which is in any way open source, then a whole raft of different compiler options will appear, each with their own "x is faster than y by z%" metrics.

If you don't already have any need for any C I/O, then you might avoid some code bloat by not dragging in libraries which you're not making a great deal of use of already, for what may be a marginal benefit.

File I/O is terribly slow anyway. You might want "cheetah", but all you've got is a choice between "snail" and "tortoise".

Here's an example. If your C++ I/O takes 1 hour, and your C I/O takes 50 minutes, there isn't any point in making the change. Your users are NOT going to come back from lunch 10 minutes earlier due to your efficiency.

Likewise, if it's around 10 minutes, then a few minutes either way won't get them back from the coffee break any time sooner.

If you're going to do it, one of the methods needs to break through a time barrier where your users would figure out they could go and do something more productive while your program was running.

An hour to a minute say needs a change of algorithm, not a change of I/O library.

siddhant3s commented: Well put. +11
tux4life commented: Very nice explanation :) +9
Sky Diploma commented: Lol, I want a cheetah too ;) +3
csurfer commented: Excellently put bro !!! +2
Salem 5,265 Posting Sage

Please mention some link of curses tutorials.

Salem 5,265 Posting Sage

Sorry, do you want me to mention curses again?

Salem 5,265 Posting Sage

> integer constant is too large for ‘long’ type
Oh come on, we've told you how to fix "long long" constants, can't you at least make some intuitive guess as to how to fix "long" ?

And "mark as solved" is a link at one end of the thread (top or bottom, I forget, you can find it).

Salem 5,265 Posting Sage

Try

long long num=600851475143LL,large,k,i;

to tell the compiler that the constant is a long long.

Salem 5,265 Posting Sage

a) post what you actually tried, not a description.
b) post your actual error messages
c) tell us what your OS/Compiler is.