Solved

How to free memory assigned using "memalign"?

Posted on 2000-02-24
7
1,377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.
Suggested Courses

622 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