Thread: bus error when assigning value to member of struct

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

    bus error when assigning value to member of struct

    I am getting a bus error with the following line of code (and presumably the 3 similar lines following it):

    new->f1 = u;

    this is my struct:

    Code:
    struct node												// node for linked list
    	{
    	int f1;
    	float f2;
    	char f3;										
    	struct node *next;									// PTR to next node
    	};
    and this is the function where it is being filled. The function takes in a line out of a file which contains an integer, a float, then a character and it is trying to capture the individual tokens and put them into the struct node.


    Code:
    in main: new = tokenize(line);	
    
    
    struct node* tokenize(char line[])
    	{		
    	int search = 0;									// will search each line
    	int new2 = 0;										// will track temp field
    	int tempINT[10];
    	int tempFLOAT[10];
    	char tempSTRING;
    	
    	while (line[search] != ' ')
    		{
    		tempINT[new2] = line[search]-48;				// -48 will convert to integer
    		new2++;
    		search++;
    		}
    	new2 = 0;	
    	search++;
    	while (line[search] != ' ')						// digits before '.'
    		{
    		tempFLOAT[new2] = line[search]-48;
    		new2++;
    		search++;
    		if (line[search] == '.')					// if float has a decimal place
    			{
    			search++;	
    			while (line[search] != ' ')				// digits after '.'
    				{
    				tempFLOAT[new2] = line[search]-48;
    				new2++;
    				search++;
    				}
    			}	
    		}
    	search++;
    	tempSTRING = line[search];						// next value is char
    
    
    
    
    //////// important stuff:
    	int u = (tempINT[0]*10 + tempINT[1]);
    
    	fprintf(stderr, "Here in tokenize u is %d\n", u);
    
    	new->f1 = u;	                     //ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //	new->f1 = (tempINT[0]*10 + tempINT[1]);
    	
    	fprintf(stderr, "further in tokenize\n");
    
    	new->f2 = (tempFLOAT[0] + tempFLOAT[1]/10.0);	
    	new->f3 = tempSTRING;	
    	new->next = NULL;
    
    	return new;
    	}
    the printf before the problem statement works, but the one after it does not.

    i thought maybe changing it to new.f1 = u; but this does not work either.

    i also tried redefining the function to return *new, and this does not work either.


    i am very puzzled as to why this will not assign correctly. thanks for taking a look
    Last edited by popapez; 09-23-2009 at 07:32 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You lost your malloc call again, or did you just forget to post it?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    hey you

    its right before the call to tokenize

    new = (struct node*)malloc(sizeof(struct node*));
    new = tokenize(line);



    btw- i figured out the issue yesterday, thanks for your help.

    this part of my program was working yesterday, so who knows what i did to mess it up. changed a lot of things i guess...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by popapez View Post
    hey you

    its right before the call to tokenize

    new = (struct node*)malloc(sizeof(struct node*));
    new = tokenize(line);
    It is extremely difficult (requiring some poor programming judgment throughout the rest of the code) for those two lines of code to ever possibly work. You create a new node on the first line, and then you immediately throw it away with the second line. The only handle you have to that piece of memory is the pointer new, so if you assign some other pointer to it, it is gone. The job of your tokenize function is to create a new node; so let it do its job.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    OMG SO DUMB!

    thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM