Thread: Memory error

  1. #1
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72

    Memory error

    Hi guys.

    Check out the following code:

    Code:
    typedef struct{
        int jugador;
        int cartes[19];
    }Jugada;
    
    
    
     void iniciarPartida(int numbaralles, Pila *baralla,Oponent *oponents,Jugador *jugador,int noponents){
    
    Jugada *jugades;
    int tot;
    
    tot = noponents + 1;
    jugades = (Jugada*)malloc(sizeof(Jugada)*tot);
    if(jugades == NULL){
        printf("Memory error!\n");
    }
    else{
    //Here I run my code
    }
    }
    The problem is that ALWAYS appears the "Memory error!" message.
    What am I doing wrong?

    Thanks!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Not including the correct header might give this error. Normally it would give a linking error, instead.

    I suggest posting code that can compile and run!

    Edit: Related FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Tim S.
    Last edited by stahta01; 07-17-2014 at 10:37 AM. Reason: Added linking error
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Thanks for the answer, stahta01.

    The headers seem OK. Seems like the problem appears while I'm trying to multiply sizeof(Jugada) * tot
    I deleted tot and it works.

    I'm using the libraries:

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>


    EDIT: I found the error! noponents is not what I was expecting. My fault! Thanks!
    Last edited by catasturslykid; 07-17-2014 at 11:25 AM. Reason: Error found

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by catasturslykid View Post
    The headers seem OK. Seems like the problem appears while I'm trying to multiply sizeof(Jugada) * tot
    I deleted tot and it works.
    That's suspicious, and it means you can only allocate a single Jugada, not an array of them. Is that what you really want? What value does tot have right before the malloc? Are you sure you're getting a valid value passed in via noponents? Try running your code through a debugger or using printf to display important values at important points in your program.
    Quote Originally Posted by catasturslykid View Post
    I'm using the libraries:

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    You have stdlib.h which is required for malloc, so that's good. As mentioned in the link Tim posted, you should not cast the return value of malloc.

    Also, why are you using conio.h? It's outdated, and unless there's a good reason for you to use it, I highly recommend removing it and changing your code to use (nearly) equivalent standard functions where possible, or more modern functions if you need special console features.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The header <stdlib.h> is the correct header (Note header; an header is NOT a library!!!).

    Since you are using the non standard conio.h; I would at least move it to the end of the compiler includes.

    Could be a compiler bug; but, more likely user error.

    Edit: If you can NOT find a fix you might try calloc() function and see if the problem goes away.
    Edi2: http://www.cplusplus.com/reference/cstdlib/calloc/

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory error
    By darren78 in forum C++ Programming
    Replies: 18
    Last Post: 10-27-2009, 03:53 AM
  2. memory error
    By joeata in forum C++ Programming
    Replies: 16
    Last Post: 09-02-2009, 01:22 PM
  3. HELP!!! Memory allocation error!
    By killiansman in forum C Programming
    Replies: 6
    Last Post: 06-08-2007, 11:47 AM
  4. memory error
    By john_murphy69 in forum C Programming
    Replies: 9
    Last Post: 03-02-2003, 07:28 PM
  5. Memory Error
    By Zaarin in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2001, 05:40 AM

Tags for this Thread