Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #112482

Re: Program crashing on declaring large char array

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Program crashing on declaring large char array
Date 2017-06-18 14:10 -0400
Organization A noiseless patient Spider
Message-ID <oi6ffl$1bt$1@dont-email.me> (permalink)
References <oi4u87$jld$2@dont-email.me>

Show all headers | View raw


On 06/18/2017 12:11 AM, DFS wrote:
> I'm reading in a .csv file, counting the lines, and allocating arrays 
> based on the line count.
> 
> I want a multidimensional array of N rows (lines) by 5 columns.  I need 
> each address in the array to store up to 25 characters.
> 
> char data[lines][5][25];
> 
> If the .csv file is small (few dozen lines) it works, but if the .csv 
> file has 10,000 lines, it crashes.
> 
> I assume it has something to do with memory allocation.
> 
> char data[lines][5][25] = malloc(lines * 5 * 25);  says 'Variable length 
> array cannot be initialized'

That line does three thrings:

It creates a VLA.
It calls malloc() to attempt to dynamically allocate a block of memory
that happens to be the same size as that array.
It attempts to use the pointer returned by the call to initialize the
entire array.

There's advantages and disadvantages to be considered when deciding
between a VLA and dynamically allocated memory, but it seldom makes
sense to do both to store the same data. However, trying to initialize
an array of char using a pointer is meaningless, and not permitted.

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Program crashing on declaring large char array DFS <nospam@dfs.com> - 2017-06-18 00:11 -0400
  Re: Program crashing on declaring large char array Ian Collins <ian-news@hotmail.com> - 2017-06-18 16:34 +1200
    Re: Program crashing on declaring large char array Ian Collins <ian-news@hotmail.com> - 2017-06-18 16:36 +1200
  Re: Program crashing on declaring large char array Barry Schwarz <schwarzb@dqel.com> - 2017-06-17 21:39 -0700
  Re: Program crashing on declaring large char array GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-06-18 09:51 +0200
  Re: Program crashing on declaring large char array already5chosen@yahoo.com - 2017-06-18 03:49 -0700
    Re: Program crashing on declaring large char array already5chosen@yahoo.com - 2017-06-18 03:55 -0700
    Re: Program crashing on declaring large char array supercat@casperkitty.com - 2017-06-19 09:25 -0700
  Re: Program crashing on declaring large char array James Kuyper <jameskuyper@verizon.net> - 2017-06-18 14:10 -0400

csiph-web