-4

I was in an interview recently where I was given a piece of paper with a few function signatures and asked to fill in the code, I was also instructed not to "allocate memory".

The question was relatively simple (Smallest value in a list) so I solved it recursively which the interviewers seemed unimpressed by, they seemed to suggest that I could have declared variables on the stack but i was nervous and regrettably didn't press the interviewer on it.

What does it mean to "allocate" memory in C?

user3678912
  • 69
  • 2
  • 4
  • 7
    there is no language called c/c++ (should be part of any interview ;) – 463035818_is_not_an_ai Apr 04 '18 at 10:10
  • The interviewer didn't want you to use `malloc` &c. Solving an O(N) problem using recursion is never a particularly good idea. If I were you I'd read K & R from cover to cover, completing all the example exercises, then reapply. You'd crush it then. – Bathsheba Apr 04 '18 at 10:11
  • 1
    frankly your question is extremely vague. Why dont you show the code and then we can tell you where the allocation is and how to avoid it – 463035818_is_not_an_ai Apr 04 '18 at 10:11
  • it probably means **dynamically** allocate memory, that is, using one of the `malloc()` functions or the `new` operator in C++ –  Apr 04 '18 at 10:11
  • 2
    You could have pushed back -- variables on the stack *are* allocated. They're allocated on the stack. If the interviewer said the wrong thing, you could hold them to that, or get them to correct their requirements. – Kerrek SB Apr 04 '18 at 10:13
  • read here https://en.wikipedia.org/wiki/C_dynamic_memory_allocation – jamek Apr 04 '18 at 10:13
  • 5
    The question should have been clearer. If no memory can be allocated, you cannot run your program at all. I guess they meant 'no malloc/calloc/realloc/free' but hey, just a guess:) – Martin James Apr 04 '18 at 10:13
  • I'm aware that recursion is a poor solution and I know how to solve it iteratively ;) it seems too obvious to specify that memory shouldn't be allocated on the heap. Local variables are allocated on the stack are they not? – user3678912 Apr 04 '18 at 10:16
  • 1
    @KerrekSB If variables count as allocation, recursive function calls must count as well. They eat up much more stack memory than a few variables for a non-recursive approach. – Gerhardh Apr 04 '18 at 10:24
  • 3
    Beyond no malloc/calloc I suppose they also meant no VLA's. Ultimately, I think the requirement would have been better expressed as "your memory usage should be O(1)", i.e. independent of the input size. – StoryTeller - Unslander Monica Apr 04 '18 at 10:33
  • @Gerhardh: Yes, true, I was thinking that too. – Kerrek SB Apr 04 '18 at 12:11
  • If I see recursive functions - I know someone has no programming experience at all. I do not understand why the recursive evil is still taught as a main solution for many algorithms. So I am not surprised that the interviewer was not impressed. – 0___________ Apr 04 '18 at 12:45
  • 1
    @PeterJ_01 bit rude – user3678912 Apr 04 '18 at 12:54
  • @user3678912 What is rude in it? Your answer shows lack of the programming experience. I did not write anything rude. – 0___________ Apr 04 '18 at 13:36
  • "no programming experience at all" – user3678912 Apr 04 '18 at 14:17

1 Answers1

1

Allocate memory means you keep for your program an amount of memory situated in the HEAP section. On the contrary, when you don't allocate memory, new variables are stored in the STACK section.

See What and where are the stack and heap?

Umaiki
  • 354
  • 2
  • 14