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


Groups > comp.lang.c > #112437

Re: Program crashing on declaring large char array

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Program crashing on declaring large char array
Date Sun, 18 Jun 2017 16:34:21 +1200
Lines 37
Message-ID <eqme6dFf4vmU4@mid.individual.net> (permalink)
References <oi4u87$jld$2@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net vEMOBxdF2yXw1EMxRpbLJAiw5JJewa4PncgyMZUuZ3bj5umhw4
Cancel-Lock sha1:hOW8wOKvXQRIg+EBkXIsTSq9ZOw=
User-Agent Mozilla/5.0 (X11; SunOS i86pc; rv:38.0) Gecko/20100101 Thunderbird/38.5.1
In-Reply-To <oi4u87$jld$2@dont-email.me>
Xref csiph.com comp.lang.c:112437

Show key headers only | View raw


On 06/18/17 04:11 PM, 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.

It doesn't have anything to do with memory allocation because you don't 
allocate any memory!

What is does show is you have exceed the stack size (let's assume there 
is a stack...) limit for your implementation.

> char data[lines][5][25] = malloc(lines * 5 * 25);  says 'Variable length
> array cannot be initialized'

Assuming "lines" isn't a compile time constant, you are creating a 
variable length array (VLA) which, as your compiler is happy to report, 
can not be initialised.

You need to allocate a block of rows, something like:

typedef char Row[5][25];

Row* data = malloc( lines * sizeof *stuff );

Then you can access the data by line.

-- 
Ian

Back to comp.lang.c | Previous | Next — Previous in thread | Next 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