Solved

How to free memory assigned using "memalign"?

Posted on 2000-02-24
7
1,324 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

825 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