Thread: malloc ... WHY ?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    malloc ... WHY ?

    hi all ,

    i hope you can help me with the following :
    in types.h :
    Code:
    /* Structure of input buffer for each switch */
    typedef struct InputBuffer *InputBufferPtr;
    typedef struct {
    	struct List **fifo; 	/* Input fifos: one per output. */
      
    	void *traffic; 			/* Placeholder for traffic stats for this input */
    	int (*trafficModel)();  /* Traffic generating function */
    	Stat bufferStats;		/* Aggregate occupancy statistics for this input. */
    	struct Histogram bufferHistogram;	/* Agg occupancy statistics for this input. */
    	int  numOverflows;		/* Total number of cells dropped at this input. */
      
    	struct List **mcastFifo;		/* Multicast queue for this input. */
      
    	// #ifdef use_gbvoq
    	struct List *inputPriorityList ;	/* IPL queue for this input. */
    	// #endif
    
    } InputBuffer;
    in create.c :
    Code:
    #ifdef use_gbvoq
    		inputBuffer->inputPriorityList = (struct List *)malloc( sizeof(struct List)) ;
    		if( inputBuffer->inputPriorityList == NULL )	FatalError("createInputBuffer(): Malloc failed.\n") ;
    		inputBuffer->inputPriorityList = createList(inputBufferName) ;
    		assert( inputBuffer->inputPriorityList != NULL ) ;
    	#endif
    but, when i am going to make use of the linked list inputPriorityList in defaultInputAction.c :
    Code:
    /* GBVOQ : All cells belong to a VOQ and to an (per input port) IPL. */
    			if( use_gbvoq )
    			{
    				// assert( aSwitch->inputBuffer[input] != NULL ) ;
    				assert( aSwitch->inputBuffer[input]->inputPriorityList != NULL ) ;
    				/* Add cell to IPL. */
    				if( aSwitch->inputBuffer[input]->fifo[pri]->number == 1 ) /* Cell has already been added to a VOQ. = empty VOQ. */
    					addElementAtHead(aSwitch->inputBuffer[input]->inputPriorityList, anElement) ;
    the assert fails and if i remove it, i get a bus error in
    addElementAtHead fuction

    Basically, the aSwitch->inputBuffer[input]->inputPriorityList is NULL and i do NOT know WHY ...

    i would be thankful if you help me overcome this problem
    thanx

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Technically, this is a guess, but I feel good about it: You probably know that the value of x cannot be changed by the following function:
    Code:
    int no_changing_x(int x) {}
    because things are passed by value. That's always true, regardless of the type of x:
    Code:
    int no_changing_inputBuffer(InputBufferPtr inputBuffer)
    What inputBuffer points to can be changed, but if you do, say,
    Code:
    inputBuffer = malloc(*inputBuffer);
    inside that function, that's just too bad, because that memory has been leaked when you exit the function (the value of the pointer in the calling function can't change).

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You have one malloc and you check if it fails or not. Provide more code.
    Ooo. Guessing time: I put all my money on what tabstop said

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem - malloc, pointers and vector
    By DrSnuggles in forum C++ Programming
    Replies: 18
    Last Post: 08-14-2009, 10:28 AM
  2. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM