-1

How to implement sizeof function using only Bit-wise operator? This question asked me in interview. I tried to solve it but it will work only for unsigned int. Here I attached the code as well.

unsigned int a = 1;
int b = 0;
while(a != 0){
    a = a << 1;
    b++;
}
b = b/8;
printf("%d",b);

Note: Pointer is not allowed to use.

alk
  • 69,737
  • 10
  • 105
  • 255
Dip
  • 1
  • This is already being answered [here][http://stackoverflow.com/questions/14171117/implementation-of-sizeof-operator]. – Milind Deore Feb 19 '17 at 03:46
  • 1
    What is a "sizeof function", and what should it work on that's not an unsigned int? – Ry- Feb 19 '17 at 03:50
  • 2
    I'm voting to close this question as off-topic because it's misleading and unlikely to be useful to future visitors; sizeof isn't a function, and SO isn't about how to solve simple problems in the least sensible way... – autistic Feb 19 '17 at 03:52
  • In reality, what I wrote should have been your response, followed by "here's my business card, let me know when you need programmers who *won't* waste their time on such pointless exercises". – autistic Feb 19 '17 at 03:54
  • 2
    @Seb: I don't agree that it's a stupid exercise, at least not for the reasons you give. Of course it wouldn't make sense to re-implement `sizeof` in production code, since the `sizeof` *operator* already exists. That's not the point of the question. The point is to test the interviewee's ability to solve a problem. (I don't believe something that (mostly) works like `sizeof` *can* be implemented using only bitwise operators, but that's a different issue; either the problem or the OP's statement of it seems incomplete.) – Keith Thompson Feb 19 '17 at 05:31
  • @KeithThompson Surely there are better problems to form a test for..? – autistic Feb 19 '17 at 05:33
  • @Seb: Perhaps. I just don't think that, in the context of an interview, the *usefulness* of a problem should be the only criterion. (If it is, the OP should charge a consulting fee for the useful work he does in solving the problem.) I've been asked to solve FizzBuzz in interviews; the solution has no real-world application, but it demonstrates my ability to program. – Keith Thompson Feb 19 '17 at 05:35
  • @KeithThompson Have you ever trained to be *on the other side* of an interview? You appear to be focusing on programming, when this is a HR aspect. Wouldn't you prefer to hire someone who can point out that `sizeof` isn't a function, that the exercise in question is pointless and that they don't want to waste time on such exercises, to someone who *does* attempt it? Surely such defiance could be considered the most appropriate response to the question. – autistic Feb 20 '17 at 02:33
  • @Seb: No, I haven't, but I'd want someone who could explain why it's not the best approach *and* would be able to solve the problem as stated. – Keith Thompson Feb 20 '17 at 07:32

1 Answers1

2

As I wrote earlier, sizeof isn't a function; it's an operator, and a special kind of operator where the argument isn't evaluated for its value, hence the reason sizeof *(char *)NULL is perfectly valid and doesn't cause segfaults. This should be pointed out in the interview.

Furthermore, this question is a waste of time, also a fact that should be pointed out to the interviewer. At that point I'd be walking out the door, and off to a more sensible interview. You probably don't want to work for this company, if they commonly chase such pointless exercises.

Having said that, one who is entertained by this might come to the conclusion that the offsetof macro could be useful. Form a struct containing the given type and a dummy element, then use offsetof to determine what the offset of the dummy element is.

#include <stddef.h>
#include <stdio.h>
#define my_sizeof(x) ((size_t) offsetof(struct { x fu; char ba; }, ba))
int main(void) {
    printf("my_sizeof (char):   %zu\n", my_sizeof(char));
    printf("my_sizeof (void *): %zu\n", my_sizeof(void *));
}

Note that behind the scenes the offsetof macro may use pointers. The only alternative I see is using the &address-of operator (e.g. this question), not to be confused with the bitwise & operator (which I think you and/or your interviewer is doing, also to be pointed out); this violates your requirements as that definitely uses a pointer.

Then again, if you can't use pointers at all, then you rule out arrays and function calls; the entire language is rendered virtually useless by that requirement.

Community
  • 1
  • 1
autistic
  • 1
  • 3
  • 35
  • 80
  • 1
    You need `__attribute__((packed))` on the anonymous struct. Otherwise `my_sizeof(char)` might be 4 because of padding for alignment. – John Zwinck Feb 19 '17 at 04:11
  • @JohnZwinck A useful sidenote, yes, you're right... Now we delve into implementation-specific details, and unfortunately `__attribute__((packed))` is non-standard. – autistic Feb 19 '17 at 04:13
  • Sure, probably you should edit your answer to use `char` instead of `int`. – John Zwinck Feb 19 '17 at 04:21
  • 1
    @JohnZwinck Done. I still don't like this because padding is still technically allowed there, but... stupid exercise begets stupid solution, eh? – autistic Feb 19 '17 at 04:35