Banfa 597 Posting Pro Featured Poster

You have multiple problems but most of them stem from lines 10 and 16.

At line 16 you have detected that you have an end condition but rather than return an end condition flag you return the same value as you leaf condition at line 6. If your end condition is 2 you should return 2 so in fact you should probably always return a rather than conditionally returning it only when it is not 2.

Then at line 10 you do not check the return condition of one call to common before making the next one, so even if the first call returned the end condition you still carry on with recursion in the second call. It probably wants to change to something like

    a = common(temp->left,c,d);
    if (a != 2)
    {
        a += common(temp->right,c,d);
    }

There is a problem with that, if the first call returns 1 and the second call returns 2 the value of a is 3 which has no defined meaning in you value scheme

0 = leaf reach with no match
1 = match found in branch
2 = end condition reached    

So you will need to modify it to prevent a getting or at least returning the value 3

Banfa 597 Posting Pro Featured Poster

Yes that's pretty much it. Good practice would be to treat something that has been declared as const as const. On most of the embedded platforms I have programmed on anything declared const was put into read only memory so assignments wouldn't have worked, you can never be 100% sure your code wont work it's way onto a platform that can do this.

The basic principle is if you need to change the value it isn't constant so don't declare it as such.

Banfa 597 Posting Pro Featured Poster

You haven't got 1 quite right, arr is not "a pointer to an array of 2 elements in which each element is a pointer to a 2-D array" but rather the expression arr evaluates to a pointer to the first element in an array of 2 elements in which each element is an array of 2 elements of an array of 2 ints. It is a little complex because C does not properly have 2 or more dimensional arrays but rather arrays of arrays (I admit the difference is subtle at times).

The easiest way I know to visualise these things is this

  1. Start with the array definition int arr[2][2][2]
  2. Remove the [] brackets furtherst to the right int arr[2][2]
  3. Repace the symbol name with (*) int (*)[2][2]
  4. That is your type if you want a symbol name add it immediately after the * int (*q)[2][2]

This will produce the correct type to allow an assigment without a cast as you have in your most recent code. You can then read the type int (*)[2][2] as

A pointer ((*)) to an array of 2 elements (right hand [2]) where each element is an array of 2 (left hand [2]) integers (int).

Banfa 597 Posting Pro Featured Poster

Thats because line 59 is the first time in the file that the identifier sortVecByCreditsAscending is mentioned so as far as the compiler is concerned it is undeclared, hence the error.

Line 61 and 62 are wrong, 61 declares a function and 62 returns from main, not what you intended. The cmp function from your first code listing does what you want, I would reinstate that and then change line 59 to

std::sort(userData.begin(), userData.end(), cmp);
Banfa 597 Posting Pro Featured Poster

You have not read the documentation carefully

SND_NOSTOP

The specified sound event will yield to another sound event that is already playing in the same process. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound.

If this flag is not specified, PlaySound attempts to stop any sound that is currently playing in the same process. Sounds played in other processes are not affected.

If the flag is specified then the specified event yields if a sound is already playing in the process. If the flag is not specified then PlaySound stops any sound already playing in the current process.

It appears that, just as it did last time I used it in the days of Windows 3.11, PlaySound can only play 1 sound at a time in a given process.

Banfa 597 Posting Pro Featured Poster

You never allocate any memory for layout, you are dereferencing and invalid pointer.

Banfa 597 Posting Pro Featured Poster

Because 0.3f has type float and 0.3 has type double so using 0.3 inloves an implicit conversion of your variable i to type double and since, as discussed, floating point representations are approximations and double as a different represenation to float the conversion can change the value slightly making it higher or lower.

You should not use comparisons in floating point anywhere that you require absolute acuracy, it is fine if you are testing some tolerance, you have a complex equation with a check value and the output is right if the check value is <0.1 because if the check value is actually 0.1000001 then the result was borderline anyway and it is probably fine to discard it.

If you want accuracy, or you are on an embedded platform and you don't want to pull the bloat of the floating-point emulation library into your application then you use scaled integers. A typical example is the banking world, they do not hold money as pounds (£) in a float type with pence as a fractional part. Rather they use a scaled integer and hold money as pence (or may be even 1/10th of pence) in an integer value where practical maths operates as you would expect theoryetical maths to work. Of course you have to watch out for the upper bound of an integer but generally today that can be solved by using some sort of big-int class that has no upper bound.

Banfa 597 Posting Pro Featured Poster

Floating point values are approximations, and any manipulation of a floating point number is like to produce an approximate value that may not be what pure mathmatical logic would suggest the answer should be. For example take

float i=0.3;

i += 0.000005 * 30000000;
i -= 0.000005 * 30000000;

Logic suggests that at the end of these 3 line i still has the value 0.3 since you have taken off exactly what you added on but on my system it has the value 0.300003 because of the inherent inaccuracies introduced in performing floating point calculations.

You should absolute never use == or != with floating points you can assume that they always equate to false and true respectively. If you do use <, <=, > and >= then you need to be aware and accept that there will be cases where the program produces the opposite result to that which pure logic produces.

Banfa 597 Posting Pro Featured Poster

Basicall you can't do what you are trying to do at line 8. You can not compare a value to an array of values, you have to do the comparison individually, or in this case have a string containing your hexadecimal characters and use a string search function (such as strchr) to see if your entered character is contained in that string.

Banfa 597 Posting Pro Featured Poster

To start with scanf("%s",&sh); is incorrect it should be scanf("%s",sh); because sh is an array so using it without any [] already produces a pointer to it, you don't need to de-reference it.

Secondly, never, ever use gets, always use fgets, so not gets(sh); but fgets(sh, sizeof sh, stdin); it is safer since you can't overwrite the buffer, but note that gets does not return the '\n' at the end of the line and fgets does.

So the problem is mixing scanf and gets. gets reads up to the first '\n' character in the stream, scanf reads a data to fit the provided format string, skipping white space. So when you make this call scanf("%d",& cont_flag); it leaves a '\n' in the stream since you didn't tell it to read it in any way and the following call to gets reads the '\n' that is left in the stream and returns immediately. When you use scanf("%s",&sh); it is not looking for '\n' characters so it just skips it as whitespace and looks for actual data.

Mixing input methods nearly always leads to confusion.

Banfa 597 Posting Pro Featured Poster

Clearly it is running since it "exited" just not in the way you intended.

Build and run in debug mode, if you are getting some sort of exception it should stop you where the code is going wrong. Alternitively step through the code in the debugger so you can locate by hand where it fails.

Banfa 597 Posting Pro Featured Poster

Line 41 to 45, since you don't change d, e or phi in the loop and since d is always 1 the value of s is constant with-in the loop and if e % phi is not equal to 1 the loop will never exit.

Banfa 597 Posting Pro Featured Poster

This is all platform dependent, that is different platforms may do different things, however, the "hello world" would not be stored in the read-only code segment, it is data not code so most likely it would be stored in the read-only data segment. The initial value for foo, 1, would also be stored in the read-only data segment.

The size of the data segment where foo is stored will be in the executable image somewhere so that the memory can be allocated as the program starts.

Both bar and num are on the stack (on implementations that use a stack) memory will be reserved for them in the stack frame created when main is called, this size is stored in the program code.

Banfa 597 Posting Pro Featured Poster
    typedef int (*HIG) (int,int);

In this statement HIG is the name of the new type, the type it is defined to is int (*)(int,int). This statement is defining a type that is a point to a function that returns int and takes 2 integers as parameters, as always the syntax for creating a typedef is exactly the same as the syntax for declaring a variable of that type, that is

    int (*fnPtr) (int,int);

and

    typedef int (*HIG) (int,int);
    HIG fnPtr;

are functionally equivilent, they both create a variable, fnPtr, with a type of int (*) (int,int).

You can't use sizeof in #if because #if is evaluated completely by the preprocessor (which runs first) but sizeof is evaluated by the compiler (which runs second).

However you can use sizeof in #define because the processors evaluation of #define is to set-up a text substitution so it you have a definition like this

    #define SIZE(x) (sizeof(x))

the proprocess replaces every occurance of SIZE it finds in the code with what is in the rest of the macro definition (sizeof in this case) with out actually evaluating the new text it has inserted. The compiler comes along later and evaulates the code so the compiler never sees the SIZE but does see and evaluate the sizeof.

Banfa 597 Posting Pro Featured Poster

It's not at all clear what the issue might be, you are clearly using a very specialised library. I would suggest some techniques used in a general approcah to any problem:

  1. Try running the software in a symbolic debugger, when it gets to the point of not responding (assuming it doesn't crash and halt) pause the program and see where it is stuck.

  2. Use the process explorer (procexp.exe) to monitor how much memory the application is actually using and if it is leaking memory (indicated by every increasing memory usage).

Banfa 597 Posting Pro Featured Poster

You can't implement what sizeof does because it is done by the compiler at compile time, the only way to get sizeof behaviour is to use sizeof.

It you pass an array to a function

void function1(int* parameter)
{
    // Some code using parameter but element count in parameter not known
}

int array[5];

function1(array);

Then the function does not know how many elements where in the array unless you also pass that it, there is no way of knowing this.

void function2(int* array, int size)
{
    // Some code using array, element count given by size
}

int array[5];

function2(array, 5);

In C++ the way to get round this is to use a std::vector. This is an object that encasulates an array and one of the things it maintains is the array size so it does not need to be passed separately

void function3(std::vector<int> vec)
{
    // Some code using vec, vec knows its own element count
}

std::vector<int> array(5);

function3(vec);
Banfa 597 Posting Pro Featured Poster
  1. Don't use arrays for a co-ordinate array use a vector of a co-ordinate class

    class coordinate
    {
    public:
        // Constructors etc
        coordinate();
        coordinate(double lat, double lon);
        coordinate(const coordinate& cpy);
        ~coordinate();
    
        // Getters setters
        double getLat() const;
        double getLong() const;
        void setLat(double value);
        void setLong(double value);
        void setLatLong(double lat, double lon);
    
        // Add utilities if you wish 
        // e.g. a method to get baring of one coord from another
    
    private:
         double latitude;
         double longitude;
    };
    
    std::vector<coordinate> coordList;
    

Then pass the vector by reference to functions.

It looks like you are combining trying to sort and trying to remove interior points, you need to sort first. Also you have a lot of code for the sort but it you just added the atan2 value to your coordinate class and create a comparitor based on the atan2 value you can just call std::sort

// Assuming atan2 added into class

bool compCoord(const coordinate&op1, const coordinate&op2)
{
    return op1.getAtan2() < op2.getAtan2();
}

std::sort(coordList.begin(), coordList.end(), compCoord);

Since you are sorting if there are a lot of coordinates it may be better to use a list rather than a vector to hold you coordinate list

std::list<coordinate> coordList;

Once you are using a STL container (vector or list) you can use std::transform and or std::for_each with simple operators to perform you transformations and do atan2 calculations.

I think the logic at line 174 is wrong, assuming all values are positive then it will always return true, take old_atan as 2 and …

Banfa 597 Posting Pro Featured Poster

Do you check your return value from strchr for NULL? I ask because you search for '*' but you string does not appear to contain any *.

Also you do not appear to be assigning any memory to the pointer nameBuffer before you copy into it.

You need something like

char *nameBuffer = 0;
if (nameEnd != 0 && msgStart != 0)
{
    int size = nameEnd - msgStart;
    if (size > 0)
    {
        char *nameBuffer = new char[size+1];
        if (nameBuffer != 0)
        {
            strncpy (nameBuffer, msg, size);
        }
    }
}

The hard way is long (and hard) you need to check everything worked before continuing.

Ancient Dragon commented: yes :) +14
Banfa 597 Posting Pro Featured Poster

Most likely cause is that AIwords is either NULL or invalid so when you dereference it to get the name member you get a SIGSEGV (memory error).

Banfa 597 Posting Pro Featured Poster

Yes firstly don't have a text box for number it is entirely superfluous when you can just design the interface so the user can enter any number of values and the software can read that number later.

Use a list box, a text box and a button labeled "Add". When the user enters a value in the text box and hits add read the text box and put the value into the list box (and may be clear the text box). Have a second button (labeled 'OK') which the user can hit when they have finished entering values to process all the values in the list box.

For a brucy bonus included a 'Remove' button to allow the user to remove erroneous values from the list box.

Banfa 597 Posting Pro Featured Poster

At line 12 of you code where you create the new node there seems to have been nothing done to check that the value produced is a valid index for the tileMap array.

For example if newParent->x == 0 then on the loop iterations where x == -1 the result will be -1 which if used at lines 16 or 21 or anywhere else tileMap s accessed seems likely to produce an out of bounds access to the tileMap array which would certainly result in the unhandled exception.

Probably the easiest way to catch the error would be to run you program in the symbolic debugger, it should trap the exception and halt allowing you to examine the code and variables at the point of the exception.

Banfa 597 Posting Pro Featured Poster

It is very important to comment why some code does a particular thing, you can normally tell what it does just from reading the code itself. If you happen to have implement some specific algorithm then I would also comment what algorithm and may be why it was chosen.

If you work in any sort of sized company then comments are extemely important because the next person editing the code may not be you and they will need to know what it going on and why it was done like that.

Banfa 597 Posting Pro Featured Poster

It partly depends on what you mean by "safe". You function is heavily reliant on being passed a well formed C string and will cause problems if it is not. However most C string handling functions suffer from this particular issue. You could insist the buffer size is passed along with the pointer to the buffer and ensure that the function does not write outside the bounds of the supplied buffer.

Banfa 597 Posting Pro Featured Poster

Because you return s from mkUpperCase which by the time the function has finished running points to the termintor of the string.

If you print name as well as the return from mkUppercase you can see the function actually did it's job.

mkUpperCase should store and return the input pointer.

Banfa 597 Posting Pro Featured Poster

lol, OK so I wasn't seeing the ads because of NoScript but I was getting the fade effect which was highly confusing until I found this thread. I had seen the "No Ads" user profile option but for some reason had assumed that it wouldn't work since I'm not a paying member. However I have enabled it now and everythings fine. What a fantastic option to have thank you.

Banfa 597 Posting Pro Featured Poster

If you happen to want to override the default behaviour of new/delete which you might to monitor allocations or for a number of other reasons but you still want to allocate the memory returned from the heap then you will need malloc/free to implement your new/delete.

Banfa 597 Posting Pro Featured Poster

Because you have passed the vector into insert by value. When you pass by value a new copy of the variable (whatever type it is but vector in this case) is made for the lifetime of the function and gets deleted when the function exits.

So in your code when insert is called the vector is copied, a new value is inserted into the copy and then the copy is deleted leaving the original vector unchanged.

The fix is easy, don't pass by value pass by reference (note the &)

void insert(int i, string elem, vector<string>& st){
    st.insert(st.begin()+i, elem);
}
Banfa 597 Posting Pro Featured Poster

Your problem here is that you are assigning to a float variable

float f;
Stack MyStack;

// Code to populate stack

f = MyStack;

The left hand side is a float but it is the left hand side that determines the class used when calling a member function; clearly you can not set the operator= on the float class, it is built-in and not a class.

Another way to work out that there is a problem is to look what your current method works on, it operates on the current object this and another Stack object which is passed in, that is 2 Stack object which is not what you are after.

The answer is not to use a method at all but rather to use a helper function external to the class, something like this

float operator=(float& left, const Stack& right)
{
    left = right.pop();
    return left;
}

For symetry you could also implement you previous assignment operator as a function rather than a method, noting it is normal to return what was assigned to from the assignment operator

const Stack& operator=(Stack& left, const float& right)
{
    left.push(right);
    return left;
}
Banfa 597 Posting Pro Featured Poster

You have your declarations of new and delete wrong, new is declared like this void* operator new (std::size_t size) throw (std::bad_alloc);, similarly delete also has throw statement you have missed out, check the documentation of these functions.

Additonally you have only provided you own implementation of a single version of new but new has all the following overloads (the first and fourth ones are particularly important).

void* operator new (std::size_t size) throw (std::bad_alloc);
void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();
void* operator new (std::size_t size, void* ptr) throw();
void* operator new[] (std::size_t size) throw (std::bad_alloc);
void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_constant) throw();
void* operator new[] (std::size_t size, void* ptr) throw();

delete also has more overloads than the single one you have implemented.

Banfa 597 Posting Pro Featured Poster

In operator= (lines 41 - 46) you create a temporary stack which gets deleted as soon as the function exits. This is surely not what you intend, I imagine you want to push onto the current stack don't you?

You are already in side a member of stack so you can just use the this pointer to access the current object to execute the push operation return this->push(right);

Banfa 597 Posting Pro Featured Poster

It is not so much that there is problem with your euclid function as the fact that you don't call it in add/subtract/multiply/divide thus break OOP rule number 637

A method worth implementing is worth invoking.

Not forgetting

638: A method worth invoking is worth implementing.

and

639: No method is worth implementing twice.

:D

Banfa 597 Posting Pro Featured Poster

strcmp - compares 2 strings up to their zero terminator
strncmp - compares 2 strings up to a limit of either their zero terminator or a supplied count of characters. This is useful if you happen to want to look for a specific substring of characters within a larger string.

You don't just have an & you also have [] which are important in this context. The entire expression is &line[j].

Assuming that line is defined something like char line[100];, strings are just arrays of characters there is nothing particularly special about them in C except that the last character must always be the NUL terminator '\0'.

In this case you are presumably used to calling strcmp like this

strcmp(line, "text");

You need to understand what the expression line does in this case. When you use an array name without the [] then the evaluated expression returns a pointer to the first item in the array and as a type of pointer to the type of the array, in the case of line this is char*. So is there any other way to get a pointer to the first item in the array? We case use [] to access items in the array some line[0] is the first item in the array. To get the address of the first item in the array we can use the address of operator which is & so &line[0] also returns a pointer to the first item in an array, line and &line[0]

Ancient Dragon commented: good explanation +14
Banfa 597 Posting Pro Featured Poster

Should work fine but why not just divide by matrix1.size()?

mrnutty commented: Agree, infact he should generalize it into a CalculateMean function +12
Banfa 597 Posting Pro Featured Poster

You use the value of r1 at lines 8, 10, 11 and 12 (and 9 if you uncomment it) before initialising or assigning it (which you do at line 13).

Using an automatic scope variable without initialising it is undefined behaviour. There is no need for further explaination undefined behaviour means anything could happen.

You should always initialise automatic scope variables to some sensible value before using them.

Banfa 597 Posting Pro Featured Poster

There is nothing special about an exception class, in fact if anything I would expect it to be simpler than a normal class, if you are in an exceptional case you do not want to be using lots of additional resources.

I have given you an outline for a class and main, you can write your own exception classes or take the ones from your own post 1, I would expect the code in read to output the exceptions if the data provided was invalid.

Banfa 597 Posting Pro Featured Poster

A segmentation fault is normally a memory error, access to an invalid or NULL address. Basically your compiler has crashed compiling your code. This does not necessarily mean there is any error in the code, there could be an bug in the compiler.

Common fixes would be

  1. Use a new version of the compiler with the bug fixed
  2. Restructure your code so it doesn't produce the fault
Banfa 597 Posting Pro Featured Poster

Lines 41 - 43 have the assignments the wrong way round, you are assigning the value of the class members to the initialisation parameters rather than the value of the initialisation parameters to the class variables e.g. base = i;

Banfa 597 Posting Pro Featured Poster

At line 12 in quicksort.h you attempt to call template <class Comparable> void quickSort(vector<Comparable> & arr, int left, int right) before you have declared it, you define it at line 17.

Banfa 597 Posting Pro Featured Poster

Imagine you have a data class, then you would probably just need to assign the values to something valid like 0

class Date
{
public:
    Data() :
        day(0),
        month(0),
        year(0)
    {
    }

    void read(istream& in);    // Read function taking stream to read from
    void write(ostream& out);  // Write function taking stream to write to

    string getMonthString();  // Convert the month integer to a string January Feb etc

private:
    int day;
    int month;
    int year;
};

.

int main()
{
   try
   {
       Date date;

       date.read(cin);
       data.write(cout);
   }
   catch ... write catch blocks here
}
Banfa 597 Posting Pro Featured Poster

Out of interest how many man hours have been spent by how many people rolling your own forum code?

Banfa 597 Posting Pro Featured Poster

To start with the reason you have to use classes for a program that you could just write in main is so that you get used to classes. While you are studying all of you exercises are contived and often fairly small you do them to learn the techniques that you will need once you get into the real world and start working on applications with 10,000s of lines of code. But it is easier to explorer and understand the techniques on small example classes/examples.

I would have thought you would want a data class that had 2 methods (in addition to a default constructor), 1 method to read the data from standard in and 1 method to write the date to standard out. The method to read from standard in would throw your exceptions.

You can then write a main that instantiates this class and uses a try clause to encapsulate the read/write operation outputting errors when it catches an exception.

Banfa 597 Posting Pro Featured Poster

uset getline to get you file a line at a time as a string and then vector::push_back to add it to the vector. The will be a little inefficiency as the vector reallocates memory as it grows but for 161 entries this is unlikely to be a big issue.

Banfa 597 Posting Pro Featured Poster

2) Steps a and b can be combined into a single click by clicking the last post time rather than the thread title which takes you striaght to the last post.

Of course that doesn't really help that much in a thread with lots of posts where you have to try and find the last post you haven't read.

Banfa 597 Posting Pro Featured Poster

A bit of a sledgehammer approach but try calling InvalidateRect on you controls as you paint your window e.g. InvalidateRect(License, NULL, FALSE);

Banfa 597 Posting Pro Featured Poster

On entering 1111 as the input number I should get 17

I think you mean 15?

Your error is in the loop at lines 26 - 29.

For a 4 digit binary number the loop at lines 18 - 24 will c=increment i to 5, the loop in lines 26 - 29 will then run 5 times adding the values for 5 binary digits to output1, but there were only 4 binary digits. The first value you add to output1 dec[i] where i = 5 has not been set to anything and so you get a random value.

Change the loop in lines 26 - 29 to run the correct number of times (rather than the correct number of times +1) and access the correct indeces of dec. You have already solved a similar problem in the 2 loops at lines 31 and 40.

Banfa 597 Posting Pro Featured Poster

Look at the way operator++ is defined in your code at line 8; it has no class name before the method name. It is not a member of the class point but rather just a function, the only difference being it has been declared as a friend of point so that it can access the private data members of point.

Since it is a function not a method on the class you have to pass the point you are changing into the function by reference or you will get and change a temporary copy rather than the class you called the operator on.

Personally I might well make operator++ a method rather than a friend function and if I did the code would look like, I would also make it return a const ref rather than a value to help reduce copying (an issue for large classes)

class point 
{
  int a,b;
public:
  point(){};
  point(int Ia, int Ib) {a=Ia; b=Ib;}
  const point& operator++(point &p1);
};

const point& point::operator++() {
  this->a++; 
  this->b++;
  return p1;
}

However once you get to implementing binary operators, like operator+ you might well want to implement the operators as friend functions so you can implement them with the parameters both ways round i.e. point operator+(const point& p, int i); and point operator+(int i, const point& p);

Banfa 597 Posting Pro Featured Poster

What would be really nice is if you could duplicate the "Watched Articles" link from the main menu bar somewhere in the "Recently Updated Articles" Bar back that way all the links I regularly use would be in a single place (at the bottom) rather than me needing to drag my mouse up to the top of the screen to get to "Watched Articles".

(Naturally I'm assuming that you are modifying the interface solely to meet my personal browsing requirements I trust everyone is OK with that :D )

But it is really nice to get the bottom bar back thank you.

Banfa 597 Posting Pro Featured Poster

Conventional wisdom seems to be that trying to read data from a file (or the console) directly as formated data leads to issues detecting end of file/ bad data. You should read the file line by line and then for each line process it into the data you are expecting to receive.

Pseudo code something like

while(getline from file)
{
  if line is not blank/empty
  {
     put line into (i)stringstream

     read data from stringstream

     if (stringstream good (i.e. no errors reading the data))
     {
         process data
     }
  }
}
Banfa 597 Posting Pro Featured Poster

When you called strtok at line 3 it replaced the ':' in line with a '\0' (because that is how it works). That reduces the string in line to "end" which does not contain ".word" and leaves tptr pointing at line. If you called strtok a second time then tptr will be updated to point at the rest of the string " .word 5" which does contain ".word".

On the whole I would recomend not using strtok at all because of this destruction of data and because it is not re-entrant. It is relatively easy to parse the strring yourself.

Banfa 597 Posting Pro Featured Poster

sem_wait blocks execution for potentially an infinite amount of time if the semaphore value is already 0 in order to lock the semaphore. It only returns -1 if there is an error, including a signal, during the wait.

So "Blocked in Parent" and "Blocked in Child" will never be output. You could try calling sem_trywait which will return immediately if the semaphore can not be locked with a value of -1 and set errno to the value EAGAIN.