4

enter image description here

I'm running Xcode 7.1 on Mac OS X 10.11. I'm trying to declare a VLA array in C but I can't do it.

The second I use a variable in the array declaration, it's moot. The array doesn't get created. I've dug around in the compiler settings, tried installing GCC manually, I can't figure this out. Can anyone spot the issue? From what I understand VLA's became standard since C99 and from what I can tell my Xcode is running on C11. What is the deal here? Code and settings images included.

void printTriangle (int height, char rowPatterns[][height]) {

    int rowSize = 2 * height - 1;
    char rowString[rowSize]; //string to store in pattern's array
    int characterCount = rowSize; //number of character printed per row of triangle
    int asteriskCount = 1; //number of asterisks printed in each row
    int spaces = (characterCount - asteriskCount) / 2; //how many spaces need to be printed in this current row
    int rowCount;
// rest of the code...
   }

enter image description here

enter image description here

enter image description here

Lundin
  • 195,001
  • 40
  • 254
  • 396
David Tamrazov
  • 567
  • 1
  • 5
  • 16
  • How is `printTriangle` called? – cadaniluk Oct 22 '15 at 08:50
  • printTriangle(height, patterns); Its not a function declaration issue- I've opened an entirely different Xcode project and tried creating a variable length array and it didn't work. The variable was there, but never initialized. Didn't even have empty values. Just a char variable with.. nothing. – David Tamrazov Oct 22 '15 at 08:52
  • 3
    What do you mean when you say the declaration is "moot"? Do you get compiler errors? Runtime crashes? Unexpected results? Also, if you want to store a string in `rowString`, shouldn't it be `rowSize + 1` bytes to fit the string terminator? – Some programmer dude Oct 22 '15 at 08:52
  • 2
    @DavidTamrazov How do you know array is not being created ? – ameyCU Oct 22 '15 at 08:53
  • No, no errors whatsoever. Everything runs fine- except that the array is empty. And not like "uninitialized" empty- as in 100% completely empty. There aren't even null characters in it, and if I try assigning anything manually (rowString[1] = 'b') it still doesn't register. Its just some empty variable. And its all fixed if I provide a constant size for the array declaration. – David Tamrazov Oct 22 '15 at 08:54
  • @ameyCU I run it in the debugger and I see the variable "char rowString" in there but no size to it and no values- just "rowString (char [])" – David Tamrazov Oct 22 '15 at 08:56
  • 1
    Do you have problems viewing the array in the debugger? Can you print the contents of the array using e.g. `printf`? Maybe the debugger can't handle VLAs. Really, try printing it from inside the program. – Some programmer dude Oct 22 '15 at 08:56
  • @JoachimPileborg I see the array variable in the debugger, but I cannot view its contents. its almost like they don't exist or something. And no, I can't printf the contents because as I said its like they don't exist. I'll put up a pic of what it looks like in the debugger. – David Tamrazov Oct 22 '15 at 08:57
  • 1
    @DavidTamrazov what happens if you try to printf the content of the array anyway ? Show the code. – Jabberwocky Oct 22 '15 at 08:58
  • @MichaelWalz nothing happens- no errors are thrown, no exceptions made, and nothing is printed. the program just moves on. – David Tamrazov Oct 22 '15 at 09:00
  • 2
    Have you even *tried* doing e.g. `rowString[0] = 'A'; printf("rowString[0] = '%c'\n", rowString[0]);`? – Some programmer dude Oct 22 '15 at 09:00
  • The allocation of rowString will likely not occur before the moment when you actually use the variable. It would be much more interesting to view its contents after you have filled it up with spaces. – Lundin Oct 22 '15 at 09:02
  • @JoachimPileborg I just tried it and there particularly it worked but thereafter the values don't hold and in the debugger still won't register any values in the array. – David Tamrazov Oct 22 '15 at 09:06
  • 2
    Then it's just the debugger that doesn't show the values. The array works as it should. – Some programmer dude Oct 22 '15 at 09:06
  • All right fair enough. Strange setting in the debugger I'll have to play around with looks like. Thanks a lot, I appreciate the input. Shortsightedness on my part depending on the debugger so much like that. – David Tamrazov Oct 22 '15 at 09:12
  • My guess is that the debugger doesn't know the size of the VLA, and probably just treats it as a pointer to char, probably not much different than using char * rowString = _alloca(rowSize); . – rcgldr Oct 22 '15 at 11:09

1 Answers1

1

A VLA, like any other local variable, is probably not even allocated before the moment you use it for the first time. Modern compilers do not necessarily allocate local variables at the point of declaration, most often they don't. Therefore, attempting to view the contents of a VLA in a debugger just after the point of declaration is not meaningful.

Lundin
  • 195,001
  • 40
  • 254
  • 396