?
Solved

free() = Memory fault

Posted on 1997-04-10
6
Medium Priority
?
275 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
[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
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
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: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 200 total points
ID: 1249883
And the tenth case ?
0

Featured Post

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.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 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