Thread: allocating memory for structure

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    allocating memory for structure

    hi, i have a simple question and would apprichiate if someone could give me a detailed explanation. so what i have is a structure like this:

    Code:
    
    typedef struct tuple{
    	int x;
    	struct tuple *childTuple;
    }Tuple;
    and the code that follows look like this:
    Code:
    int ntop = -1;
    
    void fpush (Tuple *newTuple, Tuple **top,int topSize){
    	if ( ntop== topSize){
    		printf( "stack overflow");
    		return;
    	}
    	ntop++;
    	top[ntop] = newTuple;
    }
    
    int main(){
            Tuple *lastInterval = (Tuple *)malloc(100*sizeof(Tuple));
    	Tuple **top = (Tuple **)malloc(sizeof(Tuple *));
    	top[0] =(Tuple *)malloc(sizeof(Tuple));
    	top[0]->childTuple = (Tuple *)malloc(100*sizeof(Tuple));
    	lastInterval = NULL;
    	top[0] = NULL;
    
    	for (i=0;i<=100;i++){
    		lastInterval = fcreateTup(i,i+1,i+2,lastInterval);
    		fpush(lastInterval,top,200);
    	}
    
    }
    so my question now is : do i need to allocate 100 times Tuple for:

    Code:
    Tuple **top = (Tuple **)malloc(sizeof(Tuple *));
    top[0] =(Tuple *)malloc(sizeof(Tuple));
    or not , because it seams to work both ways. and why does it work if it works and it is not just my imagination ?

    cheers

    baxy

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not really clear on what your code is trying to do, and you don't show us fcreateTup. I'm guessing your after something like a 2-d array, but dynamically allocated. See if this link helps: Question 6.16. Otherwise, you will have to do lots more explaining.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Tuple **top = (Tuple **)malloc(sizeof(Tuple *));
    You should avoid casting the return result of malloc (see the FAQ).

    Also, if you write malloc calls in this form, there is minimal chance of screwing things up.
    p = malloc( numNeeded * sizeof(*p) );

    So for example
    Tuple **top = malloc(sizeof(*top));



    Code:
    	top[0] =(Tuple *)malloc(sizeof(Tuple));
    	top[0]->childTuple = (Tuple *)malloc(100*sizeof(Tuple));
    	lastInterval = NULL;
    	top[0] = NULL;
    This just leaked some memory.
    It'll also cause a segfault if you try and use top[0] now for something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocating some memory
    By supaman in forum C Programming
    Replies: 7
    Last Post: 03-09-2006, 10:34 PM
  2. allocating memory?
    By SamuraiDave in forum C Programming
    Replies: 2
    Last Post: 09-21-2005, 02:45 PM
  3. Allocating Memory for a Structure
    By surfxtc79 in forum C Programming
    Replies: 4
    Last Post: 06-05-2003, 11:40 AM
  4. Allocating memory for a structure member
    By dalek in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2003, 06:56 AM
  5. Allocating memory...
    By PsychoBrat in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2003, 11:09 PM