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


Groups > comp.lang.c > #100076

Re: Piece of code to read and process data from a file, separating chars and ints

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: Piece of code to read and process data from a file, separating chars and ints
Date 2016-12-23 14:28 +0000
Organization Fix this later
Message-ID <o3jc6p$cpr$1@dont-email.me> (permalink)
References <0fb61ff9-91e4-4001-b99d-6cc1ce28dc6f@googlegroups.com>

Show all headers | View raw


On 23/12/16 13:02, Alla _ wrote:
> Hello!
>
> I will be grateful for your reviews and comments

Are you sure about that? You seem to have ignored a lot of previous 
reviews and comments, by your own admission.

> (1) I have not yet implemented what Barry and others have suggested on
> how to work with csv files in case they have inconvenient format, i.e. files,
> in which lines end with some unexpected blank spaces, commas, etc, like
> the ones I come up with every time I save excel file as csv. I will do my best
> to implement these features.

That doesn't bother me, although it might bother others. It's just an 
assignment. Assignments generally don't expect you to bullet-proof your 
code to the extent that would be usual in the "real world" of commercial 
production code. The important thing is to be aware of the issues that 
can arise, so that you will know how to deal with them when you have to. 
In the meantime, if your program does basic input checking, I think 
that's sufficient. The level of robustness that is sometimes appropriate 
in a commercial environment would just obscure the lessons that you are 
trying to learn about C programming. That /doesn't/ mean you should be 
off-hand about such matters, but you can cut yourself a /little/ slack.

>
> (2) Attn. Richard: I have disobeyed you, Sir )

Now /that/ bothers me.

> My apologies.

Oh, in that case you're forgiven. Maybe. Let's wait and see.

> I have implemented
> the dynamic memory allocation to follow one of the headsups suggested by Keith.
> This is the only one feature that is “sort of” more advanced, but I do believe it is
> necessary for this program, not only because the lines might be arbitrarily long,
> but because it is a great practice, and it is not such a fancy feature after all.

Well, that's fair enough if it works. But if it doesn't work, there's 
going to be trouble.

> As to the memory allocation and reallocation: I have quite a few test versions,
> and in the one that precedes this code, I do not break mallocing and reallocing
> into separate functions - all calls to malloc and realloc are within three main
> functions. I know that it is always better to initialize a new pointer with malloc
> or realloc, and, if the allocation was successful, assign the value of that new
> pointer to the original pointer.

For realloc, yes. The reason is simple: if the re-allocation *fails*, 
you still have a pointer to the original data, and sometimes that can be 
good enough --- it may be possible, for example, to adopt an alternative 
algorithm that doesn't need the extra memory, perhaps at the expense of 
speed. In a student exercise, I wouldn't worry too much about it, but 
don't get into the habit of taking short-cuts like this.

> Memory leaks: I have problems re-installing Valgrind. But even without
> it I know I have lots of memory leaks.

That's because you're going too quickly. Because you continually ignore 
advice to slow down, you are well on the way to learning how to write 
bad programs quickly. It is a well-trodden path.

> (3) For now the program records all data into two arrays: array of chars and
> array of ints. I have put both into the scope intentionally because I have not
> yet decided if I leave all the data in the form of arrays, or create a
> structure, or a few structures. I will come back with this question here, or
> in another thread.
> Therefore, please, do not yet comment on these arrays.

This is a direct result of not designing the program before you write 
it, and that's one of the many bad consequences of rushing ahead.

> (5) Please, help me with the style: some of my functions have rather
> long arguments’ lists due to types and names used; is it possible to
> break such long lines into separate ones? Same for if statements.

Functional decomposition is a skill. It's easy to do, but it's harder to 
do well. A good, clean, simple design will typically lead to short 
argument lists.

> OUTPUT:
> (NAMES)
> Years
<snip lots of result data>
> 70 74 98 145 230 0
> SUCCESS

That's not what I get. I get:

$ ./alla test6.csv
*** Error in `./alla': realloc(): invalid next size: 0x0000000000bc4a80 ***
Aborted (core dumped)

The message "invalid next size", while no doubt accurate, is a little 
misleading, but it's generally a clue that you've completely screwed up 
your memory at some point in the program before this symptom occurs.

No doubt someone else here will have the time to spend tracking down the 
exact problem for you. But my own advice would be to scrap it and start 
again, this time paying attention to all the advice you've been 
studiously ignoring.

MORE HASTE, LESS SPEED.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

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


Thread

Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 05:02 -0800
  Re: Piece of code to read and process data from a file, separating chars and ints Richard Heathfield <rjh@cpax.org.uk> - 2016-12-23 14:28 +0000
    Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 06:39 -0800
      Re: Piece of code to read and process data from a file, separating chars and ints Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-23 16:29 +0000
        Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 08:44 -0800
          Re: Piece of code to read and process data from a file, separating chars and ints Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-23 20:31 +0000
            Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 20:52 -0800
      Re: Piece of code to read and process data from a file, separating chars and ints Richard Heathfield <rjh@cpax.org.uk> - 2016-12-23 19:34 +0000
        Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 21:31 -0800
  Re: Piece of code to read and process data from a file, separating chars and ints Keith Thompson <kst-u@mib.org> - 2016-12-23 14:10 -0800
    Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 21:09 -0800
  Re: Piece of code to read and process data from a file, separating chars and ints "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2016-12-24 14:49 +0000

csiph-web