Initialising

This is a discussion on Initialising within the C Programming forums, part of the General Programming Boards category; Hi, I am new to this board and rubbish at C so please be kind! Im trying to initialise a ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    13

    Initialising

    Hi, I am new to this board and rubbish at C so please be kind!
    Im trying to initialise a hash table but am having trouble allocating memory for it. Can you please have a look and see where I've slipped up?
    Code:
    void Initialize(Table T)
    {
    
         int i,j;
    
    	 T = calloc(TableSize,sizeof(TableEntry));
    
       for (i=0;i<TableSize;i++)
       {
    	   T[i].Info.Actor = calloc(1, sizeof(TableSize));
    		T[i].Info.Films = calloc(1, sizeof(TableSize));
          strcpy(T[i].Key,"XXX");
          T[i].Info.NumberFilms=0;
          strcpy(T[i].Info.Actor,"nnn");
          for (j=0;j<50;j++)
    	  {
    		 T[i].Info.Films[j] = calloc(1, sizeof(TableSize));
             strcpy(T[i].Info.Films[j],"hhh");
    	  }
       }
    }
    THe info structure is defined as
    Code:
    typedef struct
    {
    	int  NumberFilms;
    	char* Actor;
    	char** Films;
    } InfoType;
    And the table entry
    Code:
    typedef struct
    {
       KeyType  Key;
       InfoType Info;
    } TableEntry;
    
    typedef TableEntry Table[TableSize];
    Thanks in advance
    eeeeej

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	   T[i].Info.Actor = calloc(1, sizeof(TableSize));
    		T[i].Info.Films = calloc(1, sizeof(TableSize));

    That's probably supposed to be:
    Code:
    	   T[i].Info.Actor = calloc(1, <some size that suits actor>);
    	   T[i].Info.Films = calloc(1, sizeof(char *));
    And later on:
    Code:
    	 T[i].Info.Films[j] = calloc(1, sizeof(TableSize));
    should be:
    Code:
    	 T[i].Info.Films[j] = calloc(1, <some size that suits a film (title?)>);
    --
    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
    Jan 2008
    Posts
    13
    thanks for the reply. Tried that and yeh it did make more sense when I read it! I think I had been experimenting and forgot to go back. The debugger holds up on

    Code:
    		 T[i].Info.Films[j] = calloc(1, 200);
    ANything obvious. Im trying to clear this for the double pointer used in the structure.

    eeeeej

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, that should be:
    Code:
    T[i].Info.Films = calloc(50, sizeof(char *));
    --
    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
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    for (j=0;j<50;j++)
    {
    T[i].Info.Films[j] = calloc(50, 200);
    strcpy(T[i].Info.Films[j],"hhh");
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Nah, up above where you are initializing the "array" of Films, you need to create 50 objects of size char *.

    Once you get to the "50" loop, you only need one entry per character, so calloc(1, 200) should do the job.

    By the way, both 50 and 200 should really be constants (using #define or enum) so that you only have one place to change your value, and you don't have to hunt around and change several places.

    --
    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.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Thanks for your replies so far you're being very helpful. I've stared at this for so long now it just isn't going in.
    Code:
    void Initialize(Table T)
    {
    
    // Declare variables
         int i,j;
    
    // Allocate structure memory
    	 T = calloc(TableSize,sizeof(TableEntry));
    
    //Allocate memory for all pointers in the structure
       for (i=0;i<TableSize;i++)
       {
    	   T[i].Info.Actor = calloc(1, 200);
    	   T[i].Info.Films = calloc(50, sizeof(char *));
          strcpy(T[i].Key,"XXX");
          T[i].Info.NumberFilms=0;
          strcpy(T[i].Info.Actor,"nnn");
          for (j=0;j<50;j++)
    	  {
    		 T[i].Info.Films[j] = calloc(1, 200);
             strcpy(T[i].Info.Films[j],"hhh");
    	  }
       }
    }
    thats what I have so far but its still erroring out?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything wrong in the parts I can see - maybe someone else does

    What is your definition of "KeyType"? You haven't shown that bit, so I have no idea if you are "doing that right" or not.

    --
    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.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Code:
    typedef char KeyType[200];
    
    typedef struct
    {
    	int  NumberFilms;
    	char* Actor;
    	char** Films;
    } InfoType;
    
    typedef struct
    {
       KeyType  Key;
       InfoType Info;
    } TableEntry;
    
    typedef TableEntry Table[TableSize];
    up there!

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, looking closer:
    Code:
    typedef TableEntry Table[TableSize];
    ...
    void Initialize(Table T)
    So you are passing a table of TableSize number of entries,

    Then you do:
    Code:
    	 T = calloc(TableSize,sizeof(TableEntry));
    I don't think you really want to do that, but you would have to post the code calling Initialize for me to be really sure.

    --
    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.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    well when I call it I just do
    Code:
    	Initialize(T);
    as its a function....?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by eeeeej View Post
    well when I call it I just do
    Code:
    	Initialize(T);
    as its a function....?
    Right, so what is T, where is it declared?

    If it's declared as Table T, then you don't need the allocation of memory for T itself - it's already been allocated.

    --
    Mats
    Last edited by matsp; 01-22-2008 at 03:38 PM.
    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.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    I decalre Table T;

    then call the function
    Initialize(T);

    and have the allocated memory in the header file
    typedef TableEntry Table[TableSize];

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, so you don't need a line like this one:
    Code:
    	 T = calloc(TableSize,sizeof(TableEntry));
    I'm not entirely sure that will FIX anything, but it certainly won't break anything to remove that line - it's just allocating ANOTHER lot of memory for your T variable.

    --
    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.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Yeah I tried removing that. still erroring out. I'll try and give you some more code to see if it sheds any light as its saying stack overflow.

    Code:
    	for (j=0;j<num_entries;j++)
    	{
    
    		if (j%1 == 0)
    		{
    				
    			for (i=0;i<IMDBSIZE;i++)
    			{
    				I.Actor = (char *) calloc(strlen(IMDB[i].actor),sizeof(char));
    
    				strcpy(I.Actor, IMDB[i].actor);
    				strcpy(K, IMDB[i].actor);
    
    				I.NumberFilms=IMDB[i].num_films;
    
    				I.Films = (char **) calloc(strlen(IMDB[i].films[j]),sizeof(char*));
    				for (j=0;j<IMDB[i].num_films;j++)
    				{
    					I.Films[j] = (char *) calloc(strlen(IMDB[i].films[j]),sizeof(char));
    					strcpy(I.Films[j], IMDB[i].films[j]);
    				}
    
    				HInsert(K,I,T);
    					getch();
    			}
    					
    		}
    	}
    thats for passing into the table

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initialising data when application initialises?
    By Davros in forum C++ Programming
    Replies: 4
    Last Post: 01-02-2007, 11:05 AM
  2. Initialising pointers
    By patch1024 in forum C Programming
    Replies: 14
    Last Post: 05-27-2005, 03:23 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 05:34 AM
  4. initialising
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 12-12-2001, 05:36 PM
  5. initialising an associative container
    By sayz0 in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2001, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21