Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to free memory assigned using "memalign"?

Posted on 2000-02-24
7
Medium Priority
?
1,395 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 150 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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 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

670 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