3

I am attempting to initialize an array with a size of ceil(buflen/125.0) as follows:

long long maxjpg = ceil(buflen/125.0);
long long arr[maxjpg];

I do not receive a compiler error, but GDB reports "no symbol 'arr' in current context". The only fix I have found is by hardcoding a numerical value into the array size like so:

long long arr[5];

I have tried casting, using different variable types, using const and any combination of these approaches. I know that ceil returns a double, I have tried working with that too.

Initializing a value and printing it like so works:

arr[0] = 25;
printf(pos 0 is %d\n", arr[0]);

output: pos 0 is 25

Printing arr[0] through GDB after that modification results in "value has been optimized out".

Minimum viable code to reproduce:

#include <math.h>

int main(void){

long long size = ceil(123.45);    
long long arr[size];

return 0;
}

GDB Fedora 7.4.50.20120120-52.fc17

7
  • Prepare a C file in a string(programmatically) and change that string programmatically before compiling it programmatically also before running it programmatically. Commented Oct 1, 2013 at 12:56
  • Clearly the size doesn't need to be constant, or would fail to compile. Your GDB problem is an issue with the debugger. Please show the minimal complete (compilable) code that reproduces it, and also give the version of GDB you're using. Commented Oct 1, 2013 at 12:56
  • The feature with dynamic array size was added to the standard fairly recently. You should check that your debugger supports the latest C version. Commented Oct 1, 2013 at 13:10
  • @Arik what gdb command did you execute? Commented Oct 1, 2013 at 13:14
  • @HAL I used print arr[0], I also used print arr@10 to try and print the first few elements, but it didn't work either. Commented Oct 1, 2013 at 13:24

1 Answer 1

3

VLAs do not currently work in gdb. There's a bug open about it and an ongoing project to fix it: https://sourceware.org/gdb/wiki/VariableLengthArray

There's an implementation in archer.git that works in some cases, but it isn't considered good enough to go in trunk.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.