Solved

free() = Memory fault

Posted on 1997-04-10
6
263 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I avoid pointer to integer casting errors in C programming? 4 225
sameEnds challenge 3 160
An API detour question 7 85
delete-remove 14 87
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

809 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