Wednesday, July 30, 2008
Different valid uses of Pointers
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
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
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
Gossip can be productive
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
Unlearning helps at work
Obese workers not lazier, says new study
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
Know when to quit your job
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
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.