Showing posts with label memory allocation interview questions. Show all posts
Showing posts with label memory allocation interview questions. Show all posts

Memory Leak interview question c programming


Memory Leak interview question

Question: Will the following code result in memory leak?
#include

void main(void)
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return;
    }
    else
    {
        // Do some processing
    }

    return;
}
Answer: Well, Though the above code is not freeing up the memory allocated to ‘ptr’ but still this would not cause a memory leak as after the processing is done the program exits. Since the program terminates so all the memory allocated by the program is automatically freed as part of cleanup. But if the above code was all inside a while loop then this would have caused serious memory leaks.


www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Memory techniques for Interview Questions

Memory techniques for Interview Questions

Interviewing for a job can tax your memory. You must remember the names of your interviewers, their titles and details about the company, plus all the specifics of your own work history. Even finding the interview location taxes your short-term memory. At the same time, being nervous causes many people to forget details. A great help to your interview preparation is to boost your memory before the interview so that you can arrive confident and fully focused.

1. When researching the company, break it down into several study sessions spaced apart rather than try to learn it in a single sitting. Never cram the night before unless this is a spur-of-the-moment interview.

2. Paying attention plays a big role in information recall, as you may recall from your experiences daydreaming during high school chemistry class. Study the company in a quiet environment that’s free of distractions.

3. Pay attention to what is said during the interview. Many candidates focus so intently on what they are going to say next that they miss key pieces of information that the interviewer is giving.

4. Repeating information helps you to retain it. If you are in a panel group interview, with more than one interviewer, there are sure to be names that are new to you. Say, “Nice to meet you, Mr. Hanson,” and you’ll remember his name later.

5. Organize pieces of information into groups. Educational psychologists call this process “chunking.” The reason it helps boost your memory is simple. It’s easier to remember five groups with five items in each one than it is to remember 25 separate items. Draw a big-picture organizational chart that includes divisions and subsidiaries of the company. Then plug in the people and departments that you would be working with if you had the job. Go through the same process with the company’s products and markets, organizing each one into categories.

6. Use mnemonic devices. Association and visualization are especially effective devices to boost your memory in situations in which there’s no time for complicated strategies. Simply associate something you’re trying to remember with something else, and then visualize it. For example, if an interviewer’s name is Jim Newberger, picture your college roommate Jim eating a new burger. If you can, associate a physical feature to remember the face as well. Perhaps Jim Newberger has a full head of black hair like your college roommate. Afterwards, jot down the name as soon as possible.

7. If you have a longer time to learn information, acronyms and acrostics work well. For example, if the division in which you’re interested makes coatings, adhesives, special polymers and inks, take the first letter of each word to form the acronym CASPI. You can even visualize a friendly ghost. An acrostic using the first letter of each word might be “Cats Always Smell Pretty Interesting.”

8. Try the loci technique to boost your memory. In this method, you imagine the things you are trying to remember as objects in a familiar place. Let’s say that you want to remember the company’s satellite offices in Troy, Lansing, Grand Rapids and Ann Arbor. You might picture Helen of Troy sitting on your living room sofa, a lance mounted on the wall of your dining room, grand rapids spilling from an overflowing kitchen sink and your cousin Ann sitting under an arbor in your back yard.

9. Keep all belongings related to the interview super-organized. This will free up your memory for more important tasks in the same way that cleaning up your computer’s hard drive frees up more memory. Rummaging through a disorganized briefcase to find a pen—or, worse, asking the interviewer for one—could kill your chances of landing the job. The interviewer will think, “If this candidate can’t even find a pen, what’s a business trip going to be like?”

Keep a few pens tucked inside your jacket pocket or in an outside pocket of your purse. Store relevant phone numbers on your computer and phone rather than on scraps of paper.

10. Practicing for the interview is one of the best ways to commit something to memory. If possible, do a practice drive to the place of the interview and use a GPS if you have one. Think of this as the equivalent of taking a practice test in school. If you get lost and arrive late, you can probably forget about getting the job even if your qualifications are top-notch.

11. Also, at least practice the 10 common interview questions and answers with someone else. Practice our list of top 10 interview trick questions and rehearse how you will handle them. Even if the interviewer surprises you with a different question, chances are that you’ve already rehearsed your response to a similar type of query.

Memory Allocation Interview Questions

Memory Allocation Interview Questions
1.Why doesn’t the code “char *answer; gets(answer);” work?
A:The pointer variable “answer” has not been set to point to any
valid storage. The simplest way to correct this fragment is to use
a local array, instead of a pointer.

2.I can’t get strcat to work. I tried “char *s1 = “Hello, “,
*s2 = “world!”, *s3 = strcat(s1, s2);” but I got strange results.

A:Again, the problem is that space for the concatenated result is not
properly allocated.

3.But the man page for strcat says that it takes two char *’s as
arguments. How am I supposed to know to allocate things?

A:In general, when using pointers you _always_ have to consider
memory allocation, at least to make sure that the compiler is doing
it for you.

4.You can’t use dynamically-allocated memory after you free it, can
you?

A:No. Some early man pages implied otherwise, but the claim is no
longer valid.

5.How does free() know how many bytes to free?
A:The malloc/free package remembers the size of each block it
allocates and returns.

6.Is it legal to pass a null pointer as the first argument to
realloc()?

A:ANSI C sanctions this usage, but several earlier implementations do
not support it.

7.Is it safe to use calloc’s zero-fill guarantee for pointer and
floating-point values?

A:No.

8.What is alloca and why is its use discouraged?
A:alloca allocates memory which is automatically freed when the
function which called alloca returns. alloca cannot be written
portably, is difficult to implement on machines without a stack,
and fails under certain conditions if implemented simply.

c faqs

C faqs

Question1: Difference between arrays and pointers?
 Answer: Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them
 Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer expression.

Question2: What is the purpose of realloc ( )?
 Answer: The function realloc (ptr,n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument n specifies the
new size. The size may be increased or decreased. If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc ( )
may create a new region and all the old data are moved to the new region.

Question3: What is static memory allocation and dynamic memory allocation?
Answer: Static memory allocation: The compiler allocates the required memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variable has static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time.

Dynamic memory allocation: It uses functions such as malloc ( ) or calloc ( ) to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time.

Question4: How are pointer variables initialized?
Answer: Pointer variable are initialized by one of the following two ways
              Ø Static memory allocation
              Ø Dynamic memory allocation

Question5: What is a pointer variable?
Answer: A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.

Question6: What is a pointer value and address?
Answer: A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location.
Keywords:
difference between arrays and pointers
purpose of realloc in c
static memory allocation and dynamic memory allocation in c
pointer variable initialization in c
pointer variable in c
what is a pointer value and address in c
difference between array and pointer in c in tabular form
differentiate between array of pointers and pointers to array
relation between array and pointer in c
difference between array and pointer in c in hindi
array of pointers in c
difference between array and pointer in hindi
which of arrays or pointers are faster
difference between array and structure
static memory allocation in c geeksforgeeks
difference between static and dynamic memory allocation? - quora
advantages and disadvantages of static and dynamic memory allocation
dynamic memory allocation in c++
static memory allocation in c++ with example program
when is memory allocated and deallocated in c? answer both static and dynamic memory.
static and dynamic memory in computer organization
dynamic memory allocation in c pdf
declaration and initialization in c
how to initialize a pointer c++
accessing a variable through its pointer
pointer arithmetic in c
pointer to pointer in c
pointer and array in c
pointer operator in c
pointer assignment in c
pointer in c example
pointers in c pdf
use of pointers in c
double pointers in c
types of pointers in c
pointers in c geeks for geeks
list of c programs on pointers
pointers and arrays in c
use of pointers in c
pointers in c geeks for geeks
list of c programs on pointers
what is pointer in c++
pointers in c pdf
pointer in c example
types of pointers in c
double pointers in c