Solved

free() = Memory fault

Posted on 1997-04-10
6
257 Views
Last Modified: 2012-06-21
When I try to free allocated memory I get "Memory fault".
The program look for a filename in an env-variable, and
opens the file via fopen.

Call to function :

FILE *sk_dd_fopen(char *namn_in, char *mode, char **filnamn_ut)

namn_in = name on env-variable (or real filename if variable don't  exist). Will be prefixed with "dd_". Only part up to first "." (if any) is used for env-name.
mode = mode as to fopen.
filnamn_ut = pointer to actual programname.
 
I'm running the program in UNIX on HP-UX.

This is the actual call to the function :
25  static FILE *l1lista = NULL;
   /* Globally declared within this file */

54   char filnamnÄ10Å="";
55   strncpy(filnamn, pnamn,7);
56   *(filnamn+7)='Ö0';
57   strcat(filnamn, "l1");
58   time(&tid);
62   if ((l1lista=sk_dd_fopen(filnamn,"w", NULL)) == NULL)
63       return (NULL);

The code of the funvcyion is provided below, and the line giving the problem is marked with comment
/* bailing out here */.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sk_konst.h>
#include <zz_medd_skriv.h>
#include "sk_gen_funk.h"


FILE *sk_dd_fopen(char *namn_in, char *mode, char **filnamn_ut)
{
    FILE *ret;
    char *dd_name = NULL;
    char *filename = NULL;
    char *namn_in_stripped = NULL;
    char *suffix = NULL;
    char *envname;
    char *anvmedd = "";


    /*  Plocka bort eventella suffix */
    namn_in_stripped = (char *)strdup((const char *)namn_in);
    if (strchr(namn_in_stripped, '.') != NULL)
       {
       suffix = strchr(namn_in_stripped, '.') + 1;
       *(strchr(namn_in_stripped, '.')) = '\0';
       }

    /* Reservera utrymme för namn på environmentvariabel  */
    if ( !(dd_name=malloc(strlen(namn_in_stripped + 4))) )
               /* fyra extra tecken för "dd_" och nullavslutning */
       {
       zz_medd_skriv(
                      ZZ_MEDD_MINDRE
             , __FILE__
               , __LINE__
               , ZZ_MEDD_APPL
               , "Fel vid allokering av minne"
               , anvmedd);
       return(NULL);
       }

    /* Hämta namn från variabel. Om saknas använd namn_in  */
    sprintf(dd_name,"dd_%s",namn_in_stripped);

    if ((envname=getenv(dd_name))== NULL )
       {
       envname = namn_in_stripped;
       }

    /* Reservera utrymme för fullständigt filnamn */
      filename = malloc(
         strlen(envname)
         + (suffix==NULL?0:strlen(suffix))
         + 2); /* 2 extra tecken för '.' och nullavslutning */

    /* Skapa fullständigt filnamn */
    if (suffix == NULL)
       strcpy(filename, envname);
    else
       if ( strlen(suffix) > 0)
          sprintf(filename, "%s.%s", envname, suffix);
       else
          strcpy(filename, envname);

    ret = ( fopen(filename, mode) );
    /* Follwing lines for debug only */
    printf("dd_name %d,%s\n",&dd_name,dd_name);
    printf("filename %d,%s\n",&filename,filename);
    printf("namn_in_stripped %d,%s\n"
           ,&namn_in_stripped,namn_in_stripped);
    printf("suffix %d,%s\n",&suffix,suffix);
    printf("envname %d,%s\n",&envname,envname);
    /* Returnera fullständigt filnamn  */

    if (filnamn_ut == NULL)
       free(filename);     /* bailing out here !! */
    else
       *filnamn_ut = filename;

    free (dd_name);
    free (namn_in_stripped);

    return (ret);
}


This is how it looks when I'm running a testprogram using
the funktion sk_dd_fopen.

dd_name 2063838844,dd_skib001l1
filename 2063838848,skib001l1
namn_in_stripped 2063838852,skib001l1
suffix 2063838856,
envname 2063838860,skib001l1
Memory fault(coredump)
/
0
Comment
Question by:matlar
6 Comments
 
LVL 1

Expert Comment

by:raky
Comment Utility
Well namn_in_striipped is never malloced I think
0
 
LVL 1

Expert Comment

by:flisakow
Comment Utility
Edited text of question
0
 

Author Comment

by:matlar
Comment Utility

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:matlar
Comment Utility
Edited text of question
0
 

Author Comment

by:matlar
Comment Utility
> When I try to free allocated memory I get "Memory fault".

In 9 of 10 cases it this caused because you access non allocated memory, e.g. access more memory than allocated.

It can also occur if you try to free not allocated memory.

It is not necessary in the function where the fail occour you access the unallocated memory, it can be in a different part of the program.

0
 
LVL 2

Accepted Solution

by:
tovergaard earned 100 total points
Comment Utility
And the tenth case ?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

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

16 Experts available now in Live!

Get 1:1 Help Now