Memory allocation and realloc

Memory allocation and realloc

Post by Jim Bucciferr » Thu, 16 Oct 1997 04:00:00



--MimeMultipartBoundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I am trying to use realloc to allocate a growing block of memory.  I
have been able to use it with other O/S's , particularly Windows NT 4.0,
but with VxWorks, I get a mempartfree error on the second realloc.

Here is the code:
...
    syncFile *files;
    size = 0;
    count = 0;
    while(readfile)
    {
      size++;
      files = (syncFile *) realloc( files, sizeof(syncFile) * size);
      count++;
    }
...
syncFile is a basic structure.

The idea is to grow the files array with each item read from the file.
On the second realloc I get a memPartRealloc: invalid block xxxx in
partition xxxx.

Is this the proper way to allocate memory with VxWorks or do I need to
create a memory partition first.

Thanks.
JB

--MimeMultipartBoundary--

 
 
 

Memory allocation and realloc

Post by M. Haym » Fri, 17 Oct 1997 04:00:00


On Wed, 15 Oct 1997 12:18:57 -0400, Jim Bucciferro

I've seen exactly the same thing and gotten the same exact error.
My estimation:
realloc() doesn't work.

M. Hayman

Quote:>--MimeMultipartBoundary
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit

>I am trying to use realloc to allocate a growing block of memory.  I
>have been able to use it with other O/S's , particularly Windows NT 4.0,
>but with VxWorks, I get a mempartfree error on the second realloc.

>Here is the code:
>...
>    syncFile *files;
>    size = 0;
>    count = 0;
>    while(readfile)
>    {
>      size++;
>      files = (syncFile *) realloc( files, sizeof(syncFile) * size);
>      count++;
>    }
>...
>syncFile is a basic structure.

>The idea is to grow the files array with each item read from the file.
>On the second realloc I get a memPartRealloc: invalid block xxxx in
>partition xxxx.

>Is this the proper way to allocate memory with VxWorks or do I need to
>create a memory partition first.

>Thanks.
>JB

>--MimeMultipartBoundary--


 
 
 

Memory allocation and realloc

Post by Philip Quine » Fri, 17 Oct 1997 04:00:00


Hi JB,

I tried this bit of code on a vme147 card and it worked fine. I think your
problem could be not setting the files variable to NULL before calling realloc
for the first time. It is the only difference I could spot.

Regards

Phil Q

#include <stdlib.h>
#include <stdio.h>

typedef struct {
int fred;
int bill;
char *jim;

Quote:} syncFile;

void re (void)
{
    syncFile *files = (syncFile *)NULL;
    int size = 0;
    int count = 0;

    while (count<5)
    {
        size++;
        files = (syncFile *) realloc(files, sizeof(syncFile) * size);
        count++;
        printf ("SyncFile ptr is %x, size is %d\n", (int) files, size);
    }

    printf ("Done\n");
    free (files);

Quote:}
> I am trying to use realloc to allocate a growing block of memory.  I
> have been able to use it with other O/S's , particularly Windows NT 4.0,
> but with VxWorks, I get a mempartfree error on the second realloc.

> Here is the code:
> ...
>     syncFile *files;
>     size = 0;
>     count = 0;
>     while(readfile)
>     {
>       size++;
>       files = (syncFile *) realloc( files, sizeof(syncFile) * size);
>       count++;
>     }
> ...
> syncFile is a basic structure.

> The idea is to grow the files array with each item read from the file.
> On the second realloc I get a memPartRealloc: invalid block xxxx in
> partition xxxx.

> Is this the proper way to allocate memory with VxWorks or do I need to
> create a memory partition first.

> Thanks.
> JB

===============================================================================

GEC Marconi         _/    _/        _/  _/    _/     tel : +44 1245 353221x3914
Communications Ltd _/_/_/_/  _/    _/  _/  _/_/      fax : +44 1245 275448
Chelmsford        _/        _/_/_/_/  _/_/_/_/
England CM1 1PL                              _/                            
===============================================================================
 
 
 

Memory allocation and realloc

Post by Michael Morriso » Fri, 17 Oct 1997 04:00:00



> --MimeMultipartBoundary
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit

> I am trying to use realloc to allocate a growing block of memory.  I
> have been able to use it with other O/S's , particularly Windows NT 4.0,
> but with VxWorks, I get a mempartfree error on the second realloc.

> Here is the code:
> ...
>     syncFile *files;
>     size = 0;
>     count = 0;
>     while(readfile)
>     {
>       size++;
>       files = (syncFile *) realloc( files, sizeof(syncFile) * size);
>       count++;
>     }
> ...
> syncFile is a basic structure.

Looks like the 'files' variable is not initialized.  If 'files' is NULL,
then realloc is supposed to work like malloc.  If 'files' is pointing
to some random garbage, realloc will probably get confused.
 
 
 

Memory allocation and realloc

Post by Jason Garnha » Fri, 17 Oct 1997 04:00:00


Have you tried the vx...() memory functions?
Remember to use the vxfree() at the end.

Good Luck.

 
 
 

Memory allocation and realloc

Post by M. Haym » Sun, 19 Oct 1997 04:00:00


On Thu, 16 Oct 1997 09:31:26 +0100, Philip Quiney


>Hi JB,

>I tried this bit of code on a vme147 card and it worked fine. I think your
>problem could be not setting the files variable to NULL before calling realloc
>for the first time. It is the only difference I could spot.

>Regards

>Phil Q

>#include <stdlib.h>
>#include <stdio.h>

>typedef struct {
>int fred;
>int bill;
>char *jim;
>} syncFile;

>void re (void)
>{
>    syncFile *files = (syncFile *)NULL;
>    int size = 0;
>    int count = 0;

>    while (count<5)
>    {
>        size++;
>        files = (syncFile *) realloc(files, sizeof(syncFile) * size);
>        count++;
>        printf ("SyncFile ptr is %x, size is %d\n", (int) files, size);
>    }

>    printf ("Done\n");
>    free (files);
>}

OK,
In my case we initialize the equivalent of "files" by calling malloc()
first, and _then_ calling realloc().  We get the same problem
described before.  Any idea why this shouldn't work (on an
MVME1603-011).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
M. Hayman

 
 
 

1. Memory fragmentation with realloc()

I have found that excessive use of realloc() causes memory
fragmentation.

As an example, I have a program that uses a rotary buffer (about 400
elements deep). Each element starts at 32 bytes, but more memory
is realloc()ed as needed. This realloc happens slowly (sometimes 2
bytes at a time), and the buffers can sometimes exceed 1024 bytes.
Since I'm using a rotary buffer, each element would eventually get
flushed and restart at 32 bytes. Therefore the program should
eventually reach a "high water mark" in memory usage, and never
go beyond that. Unfortunately, after adding fragmentation to the
equation, this ends in a program that keeps chewing more memory until
the OS crashes!

The best solution I've been able to come up with is to replace the
realloc()s with a malloc, memcpy, and free. So instead of just
trying to expand, or shrink the memory block in its spot, the block
will actually be reallocated, hopefully filling in the memory gaps.

I've been told the QNX4.24 will have better memory management.

Has anyone else come up against this problem? Does anyone have a
better solution?

--
Emil Zanettin
"If you're looking for trouble, you've come to the right place!"

www:    http://www.anca.com.au, http://www.netlink.com.au/~zanettin

2. CAPI 2.0 w/ VB 5.0

3. realloc freed memory

4. Help needed, buying OS/2 apps...

5. Please Help - HP Scanjet 4c ands NT

6. What is ATI doing these days?

7. OOPic memory allocation

8. do I need CiscoSecure?

9. standard memory allocation method for image

10. Memory Allocation Question....

11. Contigous Memory Allocation

12. memory allocation

13. Memory Allocation and Deallication in VxWorks question