Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #161979
| From | dfs <nospam@dfs.com> |
|---|---|
| Subject | Re: Allocate length of word vs fixed length |
| Newsgroups | comp.lang.c |
| References | <U3iJI.61694$VU3.5811@fx46.iad> <slrnsfbqkl.cnu.ike@rie.sdf.org> |
| Message-ID | <_EmJI.26906$Yv3.21593@fx41.iad> (permalink) |
| Organization | blocknews - www.blocknews.net |
| Date | 2021-07-19 18:03 -0400 |
On 7/19/21 5:11 PM, Ike Naar wrote:
> On 2021-07-19, dfs <nospam@dfs.com> wrote:
>> //different memory allocations
>> if(memoption==1)
>> {memlen=strlen(word);}
>
> /* allocate one extra for the terminating null character */
> memlen = strlen(word) + 1;
I did it right. Look a few lines up (you snipped it)
strcpy(word,rtrim(wordin));
So 'word' already has the nul.
>> else
>> {memlen=maxlen;}
>>
>> words[i] = malloc(sizeof(char*) * memlen);
This works.
> /* words[i] contains memlen chars, not memlen pointers to char */
> words[i] = malloc(sizeof(char) * memlen);
> /* or */ words[i] = malloc(memlen); /* sizeof(char) == 1 by definition */
> /* or */ words[i] = malloc(memlen * sizeof *words[i]); /* clc idiom */
All 3 of those options compile but when run:
lettermatch: malloc.c:2539: sysmalloc: Assertion `(old_top ==
initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >=
MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize
- 1)) == 0)' failed.
Aborted (core dumped)
>> memtotal += memlen;
>>
>> strcpy(words[i],word);
>>
>> //printf("%d. '%s' ",i,words[i]);
>> i++;
>> }
>>
>> fclose(fwords);
>> printf("\nloaded %d words\n",i-1);
>
> /* the number of words is i (numbered from 0 to i-1) */
> printf("\nloaded %d words\n",i);
>
>> printf("\nFirst and Last words = '%s', '%s'\n",words[0],words[i-1]);
Of course (i starts at 0), but gremlins overcount by 1 so I compensated.
The file has 370103 words in it, but the fgets() block results in 370104.
You can run my code against words_alpha.txt from
https://github.com/dwyl/english-words
And see if it counts correctly.
Thanks for looking at it.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-19 12:50 -0400
Re: Allocate length of word vs fixed length Bart <bc@freeuk.com> - 2021-07-19 19:01 +0100
Re: Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-19 14:29 -0400
Re: Allocate length of word vs fixed length Bart <bc@freeuk.com> - 2021-07-19 19:49 +0100
Re: Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-19 18:01 -0400
Re: Allocate length of word vs fixed length Bart <bc@freeuk.com> - 2021-07-19 23:25 +0100
Re: Allocate length of word vs fixed length scott@slp53.sl.home (Scott Lurndal) - 2021-07-19 20:00 +0000
Re: Allocate length of word vs fixed length Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-07-19 23:29 +0000
Re: Allocate length of word vs fixed length Real Troll <real.troll@trolls.com> - 2021-07-20 00:30 +0100
Re: Allocate length of word vs fixed length DFS <nospam@dfs.com> - 2021-07-19 20:40 -0400
Re: Allocate length of word vs fixed length Real Troll <real.troll@trolls.com> - 2021-07-20 17:00 +0000
Re: Allocate length of word vs fixed length Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-24 01:07 +0000
Re: Allocate length of word vs fixed length David Brown <david.brown@hesbynett.no> - 2021-07-19 21:35 +0200
Re: Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-19 18:02 -0400
Re: Allocate length of word vs fixed length Ike Naar <ike@rie.sdf.org> - 2021-07-19 21:11 +0000
Re: Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-19 18:03 -0400
Re: Allocate length of word vs fixed length Ike Naar <ike@sdf.org> - 2021-07-20 05:28 +0000
Re: Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-19 18:21 -0400
Re: Allocate length of word vs fixed length Bart <bc@freeuk.com> - 2021-07-19 23:29 +0100
Re: Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-19 18:40 -0400
Re: Allocate length of word vs fixed length Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> - 2021-07-20 11:44 +1000
Re: Allocate length of word vs fixed length Real Troll <real.troll@trolls.com> - 2021-07-20 22:15 +0000
Re: Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-20 20:22 -0400
Re: Allocate length of word vs fixed length dfs <nospam@dfs.com> - 2021-07-21 23:23 -0400
csiph-web