Re: similar Perl data structure?



jacob navia wrote:
Ian Collins wrote:
jacob navia wrote:
The problem with C is not the language itself.

In this example, part of the problem is the language.

One big difference between the two versions is the user of your C function has to remember to free all the allocated memory (which the example fails to do). The C++ version takes care of this though those (in your opinion) horrible destructors.


Not if you use lcc-win and use the GC.

Does that automatically close files for you, or release locks? Memory is just one of the resources a programme has to manage.

But there is an important point in your objection. Either a function
to free the memory should be supplied or a SINGLE call to free
should be required. I am rewriting the program to use a different
algorithm. Instead of reading line by line I read it in a single
read, then count the lines, and allocate a single buffer with
all the pointers AND the data.

That should be more efficient and gives an easier interface.

In standard C? Probably not. Using platform specifics (file mapping) probably yes.

But as you see if you use the GC, you do not need destructors
in this case!

You still have to close the file.

--
Ian Collins
.



Relevant Pages

  • Re: Interprocess Communication & Performance
    ... I would go for a shared file mapping - you eliminate lots of memcpy's that ... might be paged out if the system is under memory pressure or it becomes ... allocated memory myself faster then using the ... file mapping stuf or is not worth the trouble. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to take in a string of any size?
    ... >the contents into the allocated memory. ... nobody said the string started at the beginning of the file. ... >number of calls to realloc() isn't going to be excessive). ...
    (comp.lang.c)
  • Re: Interprocess Communication & Performance
    ... For instance if the data items are small and ... memory the performance hit from the synchronization will overwhelm things. ... to map the allocated memory in both processes. ... memcpy's then using shared file mapping I think. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: will I get Memory leak..
    ... dynamically assigned memory area will i get memory leak.. ... The problem with your code, on the other hand, is that your call to strcpywrites a null character to fp, which is one position past the end of the allocated memory. ... As a result of that error, the behavior of your entire program is undefined, which means that anything could go wrong A memory leak is very definitely a possibility from making that kind of mistake, but most of the other possible consequences of that mistake are much worse than memory leaks. ...
    (comp.lang.c)
  • Re: show disassembly
    ... C++ defines three types of storage: ... The memory they occupy ... int* pOut = NULL; ... // Free allocated memory elsewhere. ...
    (microsoft.public.vc.language)