Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to free memory assigned using "memalign"?

Posted on 2000-02-24
7
Medium Priority
?
1,411 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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

877 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