Solved

free() = Memory fault

Posted on 1997-04-10
6
260 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
ID: 1249877
Well namn_in_striipped is never malloced I think
0
 
LVL 1

Expert Comment

by:flisakow
ID: 1249879
Edited text of question
0
 

Author Comment

by:matlar
ID: 1249880

0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:matlar
ID: 1249881
Edited text of question
0
 

Author Comment

by:matlar
ID: 1249882
> 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
ID: 1249883
And the tenth case ?
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

863 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

25 Experts available now in Live!

Get 1:1 Help Now