gcc compiler dynamic array declaration without warning

14 Dec 2011 . category: . Comments

I demonstrate for one of Prof Richard Bowden's modules on C programming for first year Electronic Engineering students. Recently one of the tasks given to students was read in a matrix, multiply and write it out. To start with they did 3x3 then 7x7 then were instructed to make it variable size.

So when going round to help I showed a few of them how to allocate deallocate dynamic memory then asked the question had they been taught this. Apparently at they hadnt by this point, later finding out it comes up after Christmas.

My ramblings today are not about the module content or structure but actually the gcc compiler. When learning to program in C++ I learnt on a fairly sensible compiler inside Visual Studio. Then hit the gcc compiler and got annoyed by stupid things, for example

Visual Studio interprets this fine

std::vector<std::vector<int>>

g++

Error, required:

std::vector<std::vector<int> >

To note the space between the last two angle brackets.

While walking round I saw some students code and got a little scare by it they performed operations such as:

 

int size = 0;

scanf("%i",size);

int myArray[size];

for (int i = 0 ; i < size ; i++)

scanf("%f",myArray[i]);

for (int i = 0 ; i < size ; i++)

printf("%f",myArray[i]);

 

To my horror this compiled without warning or error and even worse it worked. I later while chatting to friends found that this was normal for the gcc compiler, and apparently the g++. So therefore we had some guesses about how this would work and the common answer was:

myArray is initialised to length 0, then since taken of the stack when read and written to you corrupted the stack.

The message to take away from this horror story of teaching C, is two things use a compiler that may not follow the letter of the standard but actually is intelligent and knows what you mean; always use -wall when compiling on gcc otherwise you will end up in a pile of mess later on!


Stuart James  


Stuart James

Assistant Professor in Visual Computing at Durham University. Stuart's research focus is on Visual Reasoning to understand the layout of visual content from Iconography (e.g. Sketches) to 3D Scene understanding and their implications on methods of interaction. He is currently a co-I on the RePAIR EU FET, DCitizens EU Twinning, and BoSS EU Lighthouse. He was a co-I on the MEMEX RIA EU H2020 project coordinated at IIT for increasing social inclusion with Cultural Heritage. Stuart has previously held a Researcher & PostDoc positions at IIT as well as PostDocs at University College London (UCL), and the University of Surrey. Also, at the University of Surrey, Stuart was awarded his PhD on visual information retrieval for sketches. Stuart holds an External Scientist at IIT, Honorary roles UCL and UCL Digital Humanities, and an international collaborator of ITI/LARSyS. He also regularly organises Vision for Art (VISART) workshop and Humanities-orientated tutorials and was Program Chair at British Machine Conference (BMVC) 2021.