Thread: Malloc access violation

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Malloc access violation

    I get a runtime error when I debug my program. The error is: unhandled exception myprogram.exe 0x0000005: Access Violation. I use VS 6.0 and windows xp. Here is the line when I get the error:
    Code:
    lintren.dades_parades = (parada*)malloc(sizeof(parada)*lintren.n_parades);

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That seems like a very unlikely line to CAUSE such an error. Most likely, you are either:
    1. Using memory after you've freed it.
    2. Writing over the end of other previously allocated memory.

    Bear in mind that when malloc splits a large portion of memory, it will keep track of where in the memory the next block starts, so there may be something valuable at the memory just after a block that you have allocated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    That seems like a very unlikely line to CAUSE such an error. Most likely, you are either:
    1. Using memory after you've freed it.
    2. Writing over the end of other previously allocated memory.
    I still can't found the cause of the error.

    This is the whole code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_NOM 20
    #define TOTAL_LINIES 2
    
    typedef struct
    {
       char nom_parada[MAX_NOM];
       int distancia_anterior;
       int temps_anterior;
    } parada;
    
    typedef struct
    {
       char nom_linia[MAX_NOM];
       int n_parades;
       parada *dades_parades;
    } linia;
    
    
    void CalculaDisTemps(parada *parades, int longitud, float *distancia, float *temps){
    	int i;
        *distancia = 0;
    	*temps = 0;
    	i=0;
    
        do{
    	   *distancia = *distancia + (*parades).distancia_anterior;
           parades++;
           i++;
      	}while (i<longitud);
    	
    	
    	parades = parades - i;
    	i=0;
    
    	do{
    	   *temps = *temps + (*parades).temps_anterior;
           parades++;
           i++;
    	}while (i<longitud);
    
    
    
    }
    
    void OmplirDades (parada *parades, int nparades){
        int i;
           
    	for(i=0;i<nparades;i++){
    	   fflush(stdin);
    	   printf("Introdueixi el nom de la parada:\n");
    	   gets(parades->nom_parada);
    	   printf("Introdueix la distància que hi ha des de la parada anterior:\n");
    	   scanf("%d", &parades->distancia_anterior);
    	   printf("Introdueix el temps que ha passat des de la parada anterior:\n");
    	   scanf("%d", &parades->temps_anterior);
    	   parades++;
        }
           parades = (parades - nparades);
    }
    
    
    void OmplirDadesLinia(linia lintren){
    	printf("Introdueix el nom de la línia:\n");
    	gets (lintren.nom_linia);
    	printf("Introdueix el nombre de parades de la línia:\n");
    	scanf("%d", lintren.n_parades);
    	lintren.dades_parades = (parada*)malloc(sizeof(parada)*lintren.n_parades);
    	OmplirDades (lintren.dades_parades, lintren.n_parades);
    
    }
    
    
    
    
    int main(){
    
       float distancia;
       float temps;
       linia lintren;
          
       
       OmplirDadesLinia(lintren);
    
       CalculaDisTemps(lintren.dades_parades, lintren.n_parades, &distancia, &temps);
    
       printf("%f\n", distancia);
       printf("%f\n", temps);
    
       free(lintren.dades_parades);
       
       
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps it's the line just before, where you are trying to read a value in with scanf, without passing an address of the value? [Found by compiling with -Wall and gcc - I have not tested your code, as I have no idea what it's supposed to do and what to enter on the various questions].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You'd need to pass lintren by pointer into OmplirDadesLinia if you want to allocate memory in there and have it used outside that function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You also need to get rid of gets: http://cpwiki.sourceforge.net/Gets
    And stop using fflush(stdin): http://cpwiki.sourceforge.net/fflush_stdin
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    Perhaps it's the line just before, where you are trying to read a value in with scanf, without passing an address of the value? [Found by compiling with -Wall and gcc - I have not tested your code, as I have no idea what it's supposed to do and what to enter on the various questions].
    The code is just a practice about pointers and it has just a learning purpose.

    You'd need to pass lintren by pointer into OmplirDadesLinia if you want to allocate memory in there and have it used outside that function
    Yes, the program was wrong due to both errors thank you!

    Thanks to Elysia too, I'll try to stop using fflush and gets.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, more diagnostics for you:
    Warning 1 warning C6202: Buffer overrun for 'parades->nom_parada', which is possibly stack allocated, in call to 'gets': length '4294967295' exceeds buffer size '20' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 55
    Warning 2 warning C6031: Return value ignored: 'gets' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 55
    Warning 3 warning C6031: Return value ignored: 'scanf' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 57
    Warning 4 warning C6031: Return value ignored: 'scanf' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 59
    Warning 5 warning C6386: Buffer overrun: accessing 'argument 1', the writable size is '20' bytes, but '4294967295' bytes might be written: Lines: 50, 52, 53, 54, 55 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 55
    Warning 6 warning C6202: Buffer overrun for 'lintren.nom_linia', which is possibly stack allocated, in call to 'gets': length '4294967295' exceeds buffer size '20' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 68
    Warning 7 warning C6031: Return value ignored: 'gets' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 68
    Warning 8 warning C6031: Return value ignored: 'scanf' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 70
    Warning 9 warning C6066: Non-pointer passed as parameter '2' when pointer is required in call to 'scanf' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 70
    Warning 10 warning C6386: Buffer overrun: accessing 'argument 1', the writable size is '20' bytes, but '4294967295' bytes might be written: Lines: 67, 68 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 68
    Warning 11 warning C6001: Using uninitialized memory 'lintren': Lines: 81, 82, 83, 86 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 86
    Warning 12 warning C4700: uninitialized local variable 'lintren' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 86
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 10:56 PM
  2. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 10:25 AM
  3. access violation
    By bonkey in forum C++ Programming
    Replies: 15
    Last Post: 11-20-2003, 10:22 AM
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM
  5. gluBuild2DMipmaps & access violation
    By glUser3f in forum Game Programming
    Replies: 2
    Last Post: 10-03-2003, 02:31 PM