Solved

How to free memory assigned using "memalign"?

Posted on 2000-02-24
7
1,289 Views
Last Modified: 2008-02-01
I'm using memalign to allocate a memory. if i want to free the memory allcated , how can i do it ?
Pls reply soon
Arun
0
Comment
Question by:goel_ar
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 2554197
Well, with free(), like:

  int *buf = NULL;

  if((buf = (int *)memalign(4, 100)) == NULL)
  {
    printf("Failed to allocate\n");
  }

  free(buf);

Are you doing that and it's failing?
0
 

Author Comment

by:goel_ar
ID: 2554318
I'm using memalign to allocate memory at a boundry of 4 bytes. I am using free to deallocate the memory once when I am done with it. This happens in a loop and after a couple of times say 5 or 6 times, I get a memory allocation failure(SGV signal).

But the code works fine with malloc instead of memalign for any number of iterations.

Could somebody give me possible reasons for this???..
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2554987
What OS/compiler, how big is each allocation, and are you positive that your code isn't ever writing past the end of allocated region
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:goel_ar
ID: 2556674
The OS/compiler is (Unix) Sun Solaris, each allocation is 196*sizeof(void *) ie.,784 bytes. I am very much sure that I am not writing past the allocated region b'coz, the structure that I am allocating is nothing but a structure of char pointers(196 char pointers) and for each(or some) of the char pointers in the structure I allocate memory basing on the mapping rules.

ex:

struct record
{
    char *first;
    char *second;
    .
    .
    .
    char *195elem;
}rec*;

Here rec* is being allocated memory using memalign, and each(or some) of the char *in
rec* are allocated memory(using malloc) in each iteration.

Hope I am clear now....
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2556689
Yeah, that's clear and I sort of recall something that might be related... Let me check tomorrow.

BTW: I had to ask about the overflow possibility 'cause it's a great way to cause a memory leak...
0
 
LVL 40

Accepted Solution

by:
jlevie earned 50 total points
ID: 2561162
Okay, there isn't anything fundamentally wrong wrong with the process. I thought that I had remembered something about a compiler problem, but it's clearly not related to this.

Consider the following code:

#include <stdio.h>
struct record
{
  char *a[196];
};
#define NREC 196
#define PTRSIZ sizeof(char *)
#define RECSIZ sizeof(struct record)
main()
{
  int i, ii;
  struct record *rec;
  for(i=0; i<1000; i++)
  {
    if( (rec = (struct record *)memalign(PTRSIZ, RECSIZ)) == NULL)
    {
      printf("Failed\n");
      exit(1);
    }
    for(ii=0; ii<NREC; ii++)
    {
      rec->a[ii] = (char *)NULL;
    }
    /* Processing now takes place, mallocing memory for the pointers. */
    for(ii=0; ii<NREC; ii++)
    {
      if((rec->a[ii] = (char *)malloc(10*(ii+1))) == NULL)
      {
        printf("Buffer allocation failed\n");
        exit(1);
      }
    }
    /* Okay, all done. Free each of the buffers and free the record. */
    for(ii=0; ii<NREC; ii++)
    {
      if(rec->a[ii] != NULL) free(rec->a[ii]);
    }
    free(rec);
  }
}

This works fine on Solaris with with either the 4.0 or 5.0 compilers. Since you code is failing after only a few iterations, my suspicion is that someplace in the code one of the malloc'd pointers is being abused. Possible problems include; one or more of the pointers within the structure being being overwritten by a buffer overflow, failing to free one or more of the malloc'd regions within the strucure before freeing the structure, simply running out of memory for other reasons. If there is a memory leak caused by failure to properly free everything the program will eventually exhaust available memory as it can't reuse free'd areas.

I'd go over the code carefully to see that the allocated regions are being managed properly. Also there are some good tools in Workshop (if you have it available) for looking at memory problems.
0
 

Author Comment

by:goel_ar
ID: 2563729
Thanks jlevie for your suggestions.
Actually it was a minor mistake that I made, which was causing the problem. The parametres for memalign were interchanged probably b'coz of our callousness. It works fine after the changes.

Thanks
Ashwin
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now