Wednesday, July 30, 2008

Different valid uses of Pointers

The following examples help to distinguish between the use of a pointer and of the pointer's value:

void main()
{
int *p, *q;

p = (int *)malloc(sizeof(int));
q = p;
*p = 10;
printf("%d\n", *q);
*q = 20;
printf("%d\n", *q);
}

The final output of this code would be 10 from line 4 and 20 from line 6. Here's a diagram:


The following code is slightly different:

void main()
{
int *p, *q;

p = (int *)malloc(sizeof(int));
q = (int *)malloc(sizeof(int));
*p = 10;
*q = 20;
*p = *q;
printf("%d\n", *p);
}

The final output from this code would be 20 from line 6. Here's a diagram:


Notice that the compiler will allow *p = *q, because *p and *q are both integers. This statement says, "Move the integer value pointed to by q into the integer value pointed to by p." The statement moves the values. The compiler will also allow p = q, because p and q are both pointers, and both point to the same type (if s is a pointer to a character, p = s is not allowed because they point to different types). The statement p = q says, "Point p to the same block q points to." In other words, the address pointed to by q is moved into p, so they both point to the same block. This statement moves the addresses.

From all of these examples, you can see that there are four different ways to initialize a pointer. When a pointer is declared, as in int *p, it starts out in the program in an uninitialized state. It may point anywhere, and therefore to dereference it is an error. Initialization of a pointer variable involves pointing it to a known location in memory.

1. One way, as seen already, is to use the malloc statement. This statement allocates a block of memory from the heap and then points the pointer at the block. This initializes the pointer, because it now points to a known location. The pointer is initialized because it has been filled with a valid address -- the address of the new block.

2. The second way, as seen just a moment ago, is to use a statement such as p = q so that p points to the same place as q. If q is pointing at a valid block, then p is initialized. The pointer p is loaded with the valid address that q contains. However, if q is uninitialized or invalid, p will pick up the same useless address.

3. The third way is to point the pointer to a known address, such as a global variable's address. For example, if i is an integer and p is a pointer to an integer, then the statement p=&i initializes p by pointing it to i.

4. The fourth way to initialize the pointer is to use the value zero. Zero is a special values used with pointers, as shown here:

p = 0;

or:

p = NULL;

What this does physically is to place a zero into p. The pointer p's address is zero. This is normally diagrammed as:


Any pointer can be set to point to zero. When p points to zero, however, it does not point to a block. The pointer simply contains the address zero, and this value is useful as a tag. You can use it in statements such as:

if (p == 0)
{
...
}

or:

while (p != 0)
{
...
}

The system also recognizes the zero value, and will generate error messages if you happen to dereference a zero pointer. For example, in the following code:

p = 0;
*p = 5;

The program will normally crash. The pointer p does not point to a block, it points to zero, so a value cannot be assigned to *p. The zero pointer will be used as a flag when we get to linked lists.

The malloc command is used to allocate a block of memory. It is also possible to deallocate a block of memory when it is no longer needed. When a block is deallocated, it can be reused by a subsequent malloc command, which allows the system to recycle memory. The command used to deallocate memory is called free, and it accepts a pointer as its parameter. The free command does two things:

1. The block of memory pointed to by the pointer is unreserved and given back to the free memory on the heap. It can then be reused by later new statements.
2. The pointer is left in an uninitialized state, and must be reinitialized before it can be used again.

The free statement simply returns a pointer to its original uninitialized state and makes the block available again on the heap.

Dynamic Data Structures: The Heap

A typical personal computer or workstation today has somewhere between 16 and 64 megabytes of RAM installed. Using a technique called virtual memory, the system can swap pieces of memory on and off the machine's hard disk to create an illusion for the CPU that it has much more memory, for example 200 to 500 megabytes. While this illusion is complete as far as the CPU is concerned, it can sometimes slow things down tremendously from the user's perspective. Despite this drawback, virtual memory is an extremely useful technique for "increasing" the amount of RAM in a machine in an inexpensive way. Let's assume for the sake of this discussion that a typical computer has a total memory space of, for example, 50 megabytes (regardless of whether that memory is implemented in real RAM or in virtual memory).

The operating system on the machine is in charge of the 50-megabyte memory space. The operating system uses the space in several different ways, as shown here:


The operating system and several applications, along with their global variables and stack spaces, all consume portions of memory. When a program completes execution, it releases its memory for reuse by other programs. Note that part of the memory space remains unused at any given time.

This is, of course, an idealization, but the basic principles are correct. As you can see, memory holds the executable code for the different applications currently running on the machine, along with the executable code for the operating system itself. Each application has certain global variables associated with it. These variables also consume memory. Finally, each application uses an area of memory called the stack, which holds all local variables and parameters used by any function. The stack also remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are "pushed onto" the stack. When the function returns, these locals and parameters are "popped." Because of this, the size of a program's stack fluctuates constantly as the program is running, but it has some maximum size.

As a program finishes execution, the operating system unloads it, its globals and its stack space from memory. A new program can make use of that space at a later time. In this way, the memory in a computer system is constantly "recycled" and reused by programs as they execute and complete.

In general, perhaps 50 percent of the computer's total memory space might be unused at any given moment. The operating system owns and manages the unused memory, and it is collectively known as the heap. The heap is extremely important because it is available for use by applications during execution using the C functions malloc (memory allocate) and free. The heap allows programs to allocate memory exactly when they need it during the execution of a program, rather than pre-allocating it with a specifically-sized array declaration.

Monday, July 28, 2008

Create Shortcut to Hibernate Windows XP instead of Shutdown

Hibernate is great because it saves the status of your Windows XP session (all the programs and documents you have open) to the hard drive, so it can automatically restore it the next time you power up. The first step to making a Hibernate shortcut is to make sure you have Hibernate turned on. Go to the Control panel then click Performance and maintenance. Next, choose Power options, and select the hibernate tab. Finally, make sure Enable hibernation is checked.

Now to actually make the shortcut right click your desktop and choose New | Shortcut. Next, type this case-sensitive command into the dialog box

Code:
rundll32.exe powrprof.dll,SetSuspendState Hibernate

click next. Give the shortcut a name like Hibernate and click finish. From now on all you need to do to enter hibernation is double click the shortcut.

Alos you can put this shortcut to Windows task scheduler so that system Hibernate can be achived at a particular time automatically.

What not to do at a workplace

Angry employees can waste time worrying over conflicts. Keeping the peace in the workplace is imperative to a good day at work.

While colleague romances may litter offices with disgruntled exes and unbearably cute sweethearts, hostility and dislike also leave the workplace reeling. Angry employees can waste time worrying over conflicts, and even lead to office violence.
John Challenger, chief executive of outplacement firm Challenger, Gray & Christmas Inc., offers the following tips on keeping the peace at work:
-Get together with co-workers outside the workplace. Fortifying an office bond with out-of-office activities helps build respect and strengthens relationships among officemates, said Challenger.
Also Watch:
-Don’t embarrass or yell at colleagues in front of others. Make conflicts private.
-Allow employees to openly discuss issues in order to avoid pent-up resentment. Challenger suggests encouraging written complaints and suggestions and calling follow-up meetings to discuss grievances.
-Personalize work spaces with homey touches. A comfortable environment helps cut down on tension.
-Be civil to unlikeable co-workers and generally considerate in the workplace, especially in shared spaces.
-Actually use vacation days. Time away can help ease stress and interoffice tensions.
-Don’t gossip maliciously or "BCC" e-mails.
-Don’t steal credit from co-workers or miss deadlines. Respond promptly to e-mails and always come prepared and on time to meetings.

Gossip can be productive

Workers who gossip between tasks, not during them, are more productive than those workers who remain isolated, says a new US research.

What’s more, communication at the wrong time reduces productivity.

Employees who remain closely knit with one another frequently are more productive than those who are more isolated, the researchers have found.

The researchers used electronic monitoring to tease apart the various types of interaction in the workplace and their differing effects.

Such monitoring could improve how individuals and organizations work, but it raises issues about the extent to which companies monitor their employees' behaviour.

Many studies of communication within organizations, such as of who e-mails whom, have suggested that loose networks, in which people have few contacts in common, boost productivity. But these don't capture face-to-face, moment-to-moment communication, says Benjamin Waber of the MIT Media Lab in Cambridge, Massachusetts.

"People have formal structures and reporting relationships, but when you look at who's actually talking to each other you get a different picture. We can predict productivity far more accurately from these informal structures and behaviours,” Nature quoted Waber, as saying.

Waber and his colleagues equipped a team of 23 employees at a Chicago IT company with badges that detect when they are talking, who they are close to and when they are moving about.

The workers were designing server systems. Over the course of a month, the researchers collected data on 911 individual jobs done by 23 employees in 1,900 hours. As well as measuring the time spent on each job — anything from five minutes to several days — they were able to control for its complexity and detect errors.

People who spent lots of time between jobs interacting with their colleagues — going to lunch or stopping for a chat — ultimately got much more done, the results showed. The best connected employee was 60 percent more productive than the least, says Waber, who presented his results at the International Conference on Network Science in Norwich, UK, on 27 June.

No one suspected that such interaction would help, says Waber.

"The company was astounded — formally, these people were not supposed to be talking to each other," he says.

On the other hand, if someone communicated while they were assigned to a task — whether seeking help or distracted by others — their productivity dropped sharply.

When work is hell

A 19-year-old flight attendant, angry at his job, was arrested in Minneapolis for setting a fire in an aeroplane bathroom. It might seem a bit extreme, but how many of us haven't felt like choking our bosses at some point?

Or felt that urge to throw in your resignation letter and go for a vacation (which, consequently you won't be able to afford). Just Google "I hate my job" and the search throws up 62,40,000 matches.
A BBC survey of the top 10 most hated professions throws up surprising results. If you thought the most hated profession would be that of a Proctologist - its not.
Number One on the list was being a traffic warden. Flapping your arms the entire day must be tough. Also on the list were politicians (I though we hated their profession, not them) and Reality TV show contestants - you'd hate it too if you were stuck in a house full of people like the cast of Big Boss. What's wrong with work? So what is it about a job, that makes people hate it even if it seems like a dream come true to those on the outside?
Sanya, who works with a news channel says, "News readers have to sit glued to their seats an hour before the show starts. And as soon as the bulletin ends, everyone rushes to the bathroom. I have to wait in a long line even to take a leak."
For others, work only gets worse with time and even making truckloads of money is not ample compensation. Raj, who recently resigned from the post of a Sales Process Manager with a multinational company, says, "In this sales figure driven world, the individual and his or her demands are completely ignored."
Prateek, an engineer, says, "Hectic work schedules leave no time for a personal life. I also hate the fact that I'm stuck in a cubicle instead of doing field jobs that I love." For Rakesh Jha, an IT person, his is the worst job in the world. "All you get is complaints - this doesn't work, that doesn't work. My whole day is marked with cribbing, frustrated people."
Sounds familiar? But, no matter what the grouch, the sad truth is, we're all heading to work tomorrow.

Unlearning helps at work

Unlearning practices associated with an old workplace is just as good as picking up new ones to impact a company's productivity, according to new research.

“Unlearning” implies discarding prior knowledge to make room for new information that would be useful when applied to major change affecting such an organisation.
Karen Becker of Queensland University of Technology has been studying unlearning and said it was an important, but often overlooked, way of dealing with workplace changes.
“I came from 12 years in business with a lot of involvement in change management -- there was always resistance around change and people are often not willing to relinquish past practices, so I was always looking for ways to minimise this and make transitions easier,” she said.
“When I came across the term unlearning, I pinned my research on exploring this concept further. I found that it's still not a widely used term, but it is an important and effective way of dealing with change in an organisation.”
Two of the cases Becker studied were the installation of a new computer system in a corporation, and a change in safety procedures in a heavy industry setting.
She said she found the most important thing was recognising that to make new processes effective, older ones needed to let go.
“It is about having people acknowledging previous habits and recognising that though they may be ingrained, they may not be the best way, and to accept that change is coming and be open to it,” she said.
She said assisting employees to unlearn past practices could mean changes were accepted more easily, and this would increase the productivity of an organisation by minimising the stumbling blocks that usually came when there was a change in systems.

Obese workers not lazier, says new study

A Michigan State University study demolishes negative stereotypes about obese workers

Washington: Obese workers are not lazier, emotionally unstable and harder to get along with than their thinner colleagues, says a new study that demolishes some of the stereotypes about such people.

Overweight and obese adults were not found to be significantly less conscientious, less agreeable, less extraverted or less emotionally stable.

Mark Roehling, associate professor in Michigan State University, and two of his colleagues based the study on the relationship between body weight and personality traits of 3,500 adults.

"Previous research has demonstrated that many employers hold negative stereotypes about obese workers, and those beliefs contribute to discrimination against overweight workers at virtually. . . from hiring to promotion to firing," Roehling said.

"This study goes a step further by examining whether there is empirical support for these commonly held negative stereotypes. Are they based on fact or fiction? Our results suggest that the answer is fiction."

The findings are based on two separate but convergent national studies. Roehling, who's also a lawyer, said the practical implication of the research is that employers should take steps to prevent managers from using weight as a predicator of personality traits when it comes to hiring, promoting or firing.

"Employers concerned about the fair and effective management of their work force," Roehling said, "should be proactive in preventing negative stereotypes about overweight workers from influencing employment decisions."

The research appeared in the current edition of Group & Organization Management.

Salary top reason why employees quit

The study found 70 percent of the best employers see a large connection between improved performance and higher salaries. Read on

Singapore: The top reason why employees in Asia quit is unhappiness with their pay, a study by a human resources firm said Saturday.
It found 70 percent of the best employers see a large connection between improved performance and higher salaries.
While Asian employers have "increased investment" in compensation, they are not yet getting the "strategic and financial results", The Business Times quoted Hewitt Associates principal Nishchae Suri as saying.
In China, 71 percent of employees are unhappy with their pay, 51 percent are unsatisfied in Hong Kong, 44 percent in India, 73 percent in Japan and 42 percent in Singapore, the published survey said.
Dissatisfaction with compensation averages 54 percent for Asia as a whole amid the battle for talent.
Pay must not only be fair, but seen to be fair in terms of the job and compared to the pay of other employees, Suri said, noting employers in Asia are generally doing a bad job at this.
Poor communications on the part of employers is the main problem, particularly with the new breed of talent seeking to switch jobs more often and becoming increasingly competitive.

Know when to quit your job

Working in an organisation that is going through crisis? Can’t decide between staying or leaving the job? Well, don’t fret, for here are some tips which can help you decide whether to stay or go from the company.

According to Will Werhane, global managing director at the human resources consulting firm Hay Group, look around to see if your most talented colleagues are heading for the exit sign.

That's a pretty good indication that those among the most vested in the organisation are worried and don't think it's worth staying, Will said.

"When the people who are the future of the organisation are leaving, that's a sign that company isn't doing well," News.com.au quoted him, as saying.

"It doesn't mean you should run out the door--it means (you should) examine other elements,” he added.

First, don't pay attention to rumours. Thoroughly research the state of your company as if you were investing money into it. Pay particular attention to how your organisation stacks up against the rest of the industry. One fiscal quarter of bad earnings does not spell catastrophe.

Don't rely on gossip, says a management author.

"There's a difference between a bad quarter and a troubling trend," says John Pearson, president of the management consulting firm John Pearson and author of Mastering the Management Buckets: 20 Critical Competencies for Leading Your Business or Nonprofit.

"When you hear disappointing news about your company, get the official and credible explanation of what's happening,” he said.

If you don't work for a public company, find out how others in the industry are doing by talking to people you know at competing companies, John said.

He added: Ask those contacts how sales are and whether they're laying anyone off. If that route proves a bust, ask head hunters; they''re a wealth of information about the companies they cover.

Saturday, July 26, 2008

24 Things to Always Remember

1. Your presence is a present to the world.
2. You're unique and one of a kind.
3. Your life can be what you want it to be.
4. Take the days just one at a time.
5. Count your blessings, not your troubles.
6. You'll make it through whatever comes along.
7. Within you are so many answers.
8. Understand, have courage, be strong.
9. Don't put limits on yourself.
10. So many dreams are waiting to be realized.
11. Decisions are too important to leave to chance.
12. Reach for your peak, your goal, and your prize.
13. Nothing wastes more energy than worrying.
14. The longer one carries a problem, the heavier it gets.
15. Don't take things too seriously.
16. Live a life of serenity, not a life of regrets.
17. Remember that a little love goes a long way.
18. Remember that a lot . . . goes forever.
19. Remember that friendship is a wise investment.
20. Life's treasures are people . . . together.
21. Realize that it's never too late.
22. Do ordinary things in an extraordinary way.
23. Have health and hope and happiness.
24. Take the time to wish upon a star.