Re: Array of Pointers

From: Gerald (cxgerald_at_yahoo.com)
Date: 12/22/03


Date: 22 Dec 2003 09:41:08 -0800

Bruno Desthuilliers <bdesth.tagada@tsoin-tsoin.free.fr> wrote in message news:<3fe637fb$0$24026$626a54ce@news.free.fr>...
> Gerald wrote:
> > I have a problem with an array of pointers.
> >
> > In a program I'm writing, I have to read a file, containing thousands
> > of short lines. The content of another file will be compared against
> > each line later on.
>
> <OT>
> You may (or may not) need a more appropriate (and probably a bit more
> complex) data structure than a simple array to do this efficiently.
> </OT>

I'm aiming for accuracy, speed and low memory use, in that order.

That's why I read the file into memory as a whole and then edit memory
to add string terminators (\0). Next thing I needed was an array of
pointers which can be used easily and swiftly for strcasestr (libc
2.3.2: strcasestr.c) compares.

char *__strcasestr (phaystack, pneedle)
  const char *phaystack;
  const char *pneedle;

This function is called for each line in the file. The haystack is
usually larger than the list of needles, but not always.

When I found the memory allocation problem I wasn't sure if this was a
good way, until you posted this:

<snipped lots of very interesting information>

> char **array = malloc(size * sizeof *array);

That solved the problem. Thank you!

For some reason it didn't work when I tried this before I wrote to
this group. Probably due to a typo.

<snipped lots of very interesting information again>

> This may or may not work, depending on your computer and what's running
> on it at the time...

It works on my computer.

> And it also may take some time to execute !-)

Yes, it takes about a second on my Pentium 4.

My program does not create a new array, I just use the pointers to
point to places inside the file loaded into memory. Checks are done to
assure that there's nothing bad in the input file. This way, memory
usage is low and speed is high.

> Hope That Helps (and that I didn't left too much stupidities in my code...)
> Bruno

The example helped a lot! I also learned from Kevin and Mark's
replies.

Gerald



Relevant Pages

  • Re: Dusty Deck and C memory manager, Part 1
    ... CMEMC.C -- C memory management routines for use with FORTRAN. ... Return an index into ARRAY representing LENGTH words of available ... void *ptr, *malloc; ... Rather than introduce Cray pointers and use C routines to allocate memory for Fortran arrays, you could use straight F90, for example: ...
    (comp.lang.fortran)
  • Re: Simple C containers, std::vector analog
    ... You're right about not needing to care very much about the memory ... blocks, in not knowing the ultimate size of the growable array, because ... there is no realloc function, and besides realloc might be having ... whether it's used for the list of array block pointers only or a C++ ...
    (comp.lang.c)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • Re: far pointer
    ... The restriction that pointers must be calculated within the object, has nothing to do with comparing pointers for equality. ... array y is probably next to array x in memory. ... and doesn't require segmented memory. ...
    (comp.lang.c)
  • Re: Cannot use DEBUG_NEW to trace this leak.
    ... line-at-a-time is slower but uses less memory. ... priori and in the absence of any substantiating data that the cost of the array is the ... and manage it yourself with pointers and pointers arithmetic. ...
    (microsoft.public.vc.mfc)