1

I am using an array and I tried this code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char **q = (char*)malloc(1*sizeof(char*));
q[0]="So Many Books";
q[1]="So Many Books";
q[2]="So Many Books";
q[3]="So Many Books";
q[4]="So Many Books";
printf("%s\n",q[0]);
printf("%s\n",q[1]);
printf("%s\n",q[2]);
printf("%s\n",q[3]);
printf("%s\n",q[4]);
return 0;
}

Why is the compiler not giving me an error here? I only have booked a place for one string out of an array of strings.

I have looked to some resources like:

Community
  • 1
  • 1
Salman
  • 41
  • 7
  • There's no syntax errors, so no compiler errors. In `c`, memory management is up to the programmer. Sounds like you already know your code is rife with UB. – yano Oct 28 '16 at 19:20
  • You're a C programmer. You're assumed to know what you're doing. If you want hand-holding, choose a different language. That makes it tough on beginners in particular, but the mindset is "the programmer knows what they're doing", even when it is manifest that they don't. – Jonathan Leffler Oct 28 '16 at 19:22
  • 4
    Why are you casting the result of `malloc` to `char *`, when you need a `char **`? Why are you casing the result of `malloc` at all??? – AnT stands with Russia Oct 28 '16 at 19:23
  • The compiler doesn't know that you haven't allocated enough memory; that's a problem that doesn't show up until compile time. A sufficiently clever compiler *might* be able to figure it out, but in general the argument passed to `malloc` needn't be a compile-time constant, and it's not possible to detect this kind of error in all cases. – Keith Thompson Oct 28 '16 at 19:24
  • is not about handd-holding ,, but should't stop ?cus i am assigning in un booked place ? – Salman Oct 28 '16 at 19:25
  • I just noticed the incorrect cast. You're initializing a `char**` object with a `char*` value. The types are not compatible and are not implicitly convertible. A conforming compiler must at least warn you about that. Did you get a warning that you didn't tell us about? A helpful idiom is this: `char **q = malloc(N * sizeof *q);` – Keith Thompson Oct 28 '16 at 19:26
  • @Salman: As I said, it cannot in general be detected at compile time. Detecting it at run time would require the compiler to add code to do the checking, slowing down the normal case. – Keith Thompson Oct 28 '16 at 19:27
  • Actually, best case, you have allocated an array of pointers that may eventually be able to hold strings. Each array also needs to be allocated - or you need to get a bit more creative overlaying a buffer onto a single alloc. (As a rule, I hate 2-dim dynamic arrays in C - it just is too easy to screw things up) – Michael Dorgan Oct 28 '16 at 19:31

1 Answers1

2

Why is the compiler not giving me an error here

Simply because, the issue here is not related to any syntactical error, it's a logical error that is beyond the jurisdiction of a compiler-error-check.

The problem here is, apart from index 0, any other index is out of bound access here. There is nothing in C standard to stop you from doing that, but accessing out of bound memory invokes undefined behavior, hence the program output is also, undefined. Anything could happen. Just don't do that.

Remember, just because you can do something (write code to access out of bound memory) does not mean you should be doing that.

That said, please see this discussion on why not to cast the return value of malloc() and family in C..

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261