views:

1517

answers:

5

This was an job placement interview I faced. They asked whether we can realloc Array, I told yes. Then They asked - then why we need pointers as most of the people give reason that it wastes memory space. I could not able to give satisfactory answer. If any body can give any satisfactory answer, I'll be obliged. Please mention any situation where the above statement can contradict.

Thank you.

+1  A: 

OK. You can't realloc an array that has been statically declared. The purpose of using pointers (instead of statically declared array), would be so that you can realloc the space. I guess, to be technically correct, you could realloc a statically defined array by passing a pointer to it, but I wouldn't want to. The behavior is undefined if the pointer is to memory that wasn't allocated by malloc.

tvanfosson
The realloc function lets us increase or decrease the size of a previously allocated area.(The most common usage is to increase an area.)For exmple,if we allocate room for 512 elements in an array that we fill in at runtime but find that we need room for more than 512 elements, we can call realloc.
Ankit S
http://linux.die.net/man/3/realloc - still won't help with statically-allocated items
warren
+5  A: 

You can only reallocate an array that was allocated dynamically. If it was allocated statically, it cannot be reallocated [safely].*

Pointers hold addresses of data in memory. They can be allocated, deallocated, and reallocated dynamically using the new/delete operators in C++ and malloc/free in C.

I would strongly suggest that you read The C Programming Language by Kernighan and Ritchie and a solid C++ text like C++ From the Ground Up by Herbert Schildt.

Using dynamic memory, pointers, offsets, etc are all fundamental to using these two languages. Not knowing how they work, and why they exist will be a likely reason for potential employers to turn you down.

*the compiler shouldn't let you reallocate anything that's been allocated statically, but if it does, the behavior is undefined

warren
Is Schildt's C++ better than his 'Annotated C Standard'?
Jonathan Leffler
I haven't seen his Annotated C Standard - so I can't comment directly, but I've used the Ground Up book in classes, and found the students to really 'get it' using that text
warren
+1  A: 

I don't really understand how using pointers "wastes memory". The realloc function: -

void *realloc(void *ptr, size_t size);

takes a pointer to the memory to resize as a parameter. Also, there are plenty more uses for pointers than dynamic memory, passing by reference for example.

Tarski
"wastes memory" might be intended to mean that int *ra = malloc(10*sizeof(int)); results in the use of more storage than int ra[10]; due to the overhead of your malloc implementation. But there are better reasons to use arrays on the stack where applicable.
Steve Jessop
Oh, and when the array belongs to something represented by a struct, there's the obvious memory "waste" that storing a pointer to the array, rather than just embedding the array in the struct and accessing by member name, uses an extra sizeof(void*) bytes.
Steve Jessop
+4  A: 

The phrasing is a bit strange, but to me it seems like the interview question was an open ended question designed to get you to explain what you know about arrays, pointers, dynamic memory allocation, etc. If I was the interviewer I'd want the candidate to articulate the differences between int *a = malloc(10 * sizeof(int)); and int a[10];. The followup question is not worded very well, but it was probably an attempt at nudging the candidate in the direction of the difference between a pointer and an array and setting off the train of thought.

Greg Rogers
A: 

say i have declared an array statically line char a[500]. and i have set memory during

runtime using memset to initialize the above buff with zeros. later point if i want to

increase the memory , can i use realloc like the ones below??

realloc(a, sizeof(a)*2);

but somehow iam getting a memory corruption. Iam suspecting that if i copy anything

after the initial declared size 500, or even trying to access, it might not work,.

kindly enlighten me in this regard!

aparna
You are asking a different question here.
Amit
With GCC, you should get a *** glibc detected *** ./a.out: realloc(): invalid pointer:... That is because, from http://linux.die.net/man/3/realloc, 'realloc' expects 'a' to have been returned by a earlier call to malloc, calloc, or realloc
Amit
@aparna: You should post this as it's own question, not as an answer to this here. More people would look at it and try to solve your problem. The "Ask Question" button is in the top right of the page.
sth