Hi,

Please answer the below questions....thanks

1) How can I free the memory from main(). But the memory was
allocated in function a().

    void a()
    {
        char *b=malloc(10);
    }

    main()
    {
        a();
    }

2) How can I return the local pointer variable?

    main()
    {
        char *a=result();
    }
    char * result()
    {
        char *b = malloc(10);

        return b /* I hope this would die once the program gets back to
                    the caller.  But, somehow this needs to be achieved */
    }

3) Is there any disadvantage of returning a static variable from
function? Assume the "static" variable is declared globally.

4) Why realloc is inefficient? How many possible ways are there
to overcome inefficiency using realloc itself? Or if not possible
using realloc then what other function we could use.

5) I have a text file which has some content in it and the words
can repeat. For ex: words like "the", "then" may repeat. The user
will type a "word" to be searched in a file and the "count" for that
word should get displayed.

Can anyone suggest me the logic for the above.
Basic restrictions: Temporary array and strstr() should not be used.

6) I have a text file (file size is more than 2 GB) and I want to
find out a given word? The word could be available anywhere in the
file.

Can anyone suggest me the logic for the above.
Basic restrictions: I cannot read the whole file in an array. I cannot read all the lines one by one using

fgets()

7) I have a "static" function in a file say

            static int sum(int a, int b);

What does this mean?

I know in C "static functions" are local to the file. But,
interviewer expected something else. What could be that?

8) Can anyone tell me a real-time example for "abstract"
classes? Interviewer accepted the definitions and the basic examples
which I explained. He wanted a real world scenario? Have any worked
on C++ please let me know about the same.

> 1) How can I free the memory from main(). But the memory was allocated in function a().
If you do not have a pointer to the memory, it is lost.

> 2) How can I return the local pointer variable?
Your code is fine. Returning a pointer to dynamic memory makes a copy of the pointer without destroying the memory. Returning a pointer to local memory is the bad thing.

char *result(void)
{
    char b[10]; // Local variable, destroyed on return
    return b; // Returns a pointer to reclaimed memory
}

> 3) Is there any disadvantage of returning a static variable from
> function? Assume the "static" variable is declared globally.
Aside from wasting the return value on a variable that can be accessed by any function in the file? ;)

> 4) Why realloc is inefficient?
Dynamic allocation in general is inefficient, relatively. The question is whether this "inefficient" is a deal breaker for your code.

> 5)[...]
Edward would suggest scanf() to read words and a binary search tree to store unique words along with the count.

> 6) [...]
> Basic restrictions: I cannot read the whole file in an array.
Obviously. If the file is more than 2GB then the average system would grind to a slow crawl by reading the whole file into memory.

> I cannot read all the lines one by one using fgets()
You cannot read the whole file at once and you cannot read the file piecemeal? You might be SOL. ;)

> 7) I have a "static" function in a file say
> static int sum(int a, int b);
> What does this mean?
Static functions are only visible inside the file they are defined. This is as opposed to extern functions which are visible everywhere given a declaration. extern is the default if you do not specify, but that is generally viewed to be a mistake in the design of C.

> 8) Can anyone tell me a real-time example for "abstract" classes?
Ed uses abstract classes as a C++ version of Java's interface. One example is an image encoder. The base class is abstract because an unknown encoding is pointless.

class encoder; // abstract
class jpeg_encoder: public encoder;
class gif_encoder: public encoder;
class png_encoder: public encoder;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.