d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Keeps Printing Memory Address?
12Next
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Mar 31 2013 11:26pm
When ever I try to print the flags out, or use them in code (this_goo->flag1 or this_goo->flag2) it prints some random number (I assume it's the mem address)

Why isn't it printing out 1...?

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

typedef struct goo{

 char dir_name[80];
 struct goo *parent;
 struct goo *sub_dir;
 struct goo *next;
 struct goo *prev;
 struct job *first_job;
 int flag1;
 int flag2;

}Goo;

typedef struct job{
 struct job *next;
}Job;

void make(Goo *goo){

 char name[100] = "start";
 goo = malloc(sizeof(struct goo));
 goo->parent = NULL;
 goo->next = NULL;
 goo->prev = NULL;
 goo->first_job = NULL;
 goo->sub_dir = NULL;
 strcpy(goo->dir_name, name);
 goo->flag1 = 1;
 printf("%d", goo->flag1); /* returns random negative number */

if(goo->flag1 == 1){ /*doesn't work correctly what gives?*/
printf("success");
}
 goo->flag2 = 1;

}

int main() {
 Goo this_goo;
 make(&this_goo);
}


This post was edited by lopelurag on Mar 31 2013 11:31pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 1 2013 01:26am
its working fine when i was debugging it and compiling it for release.

prints

1success

gcc compiler for windows using migw
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Apr 1 2013 07:41am
Quote (AbDuCt @ Apr 1 2013 03:26am)
its working fine when i was debugging it and compiling it for release.

prints

1success

gcc compiler for windows using migw


Guess I fixed it without realizing it, the first problem was just a substep I was taking to solve my seg fault issues

When I try to create and insert jobs after making my first one, it seg faults when trying to insert it into the list and I can't figure out why?

The segfault seems to be occuring in the while loop, when trying to insert a job after there is at least 1 already in the linked list.

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

typedef struct goo{

 char dir_name[80];
 struct goo *parent;
 struct goo *sub_dir;
 struct goo *next;
 struct goo *prev;
 struct job *first_job;

}Goo;

typedef struct job{
 struct job *next;
 char job_name[80];
 struct job *prev;
 struct goo *parent_dir;
}Job;

void make(Goo *goo){

 char name[100] = "start";
 goo = malloc(sizeof(struct goo));
 goo->parent = NULL;
 goo->next = NULL;
 goo->prev = NULL;
 goo->first_job = NULL;
 goo->sub_dir = NULL;
 strcpy(goo->dir_name, name);
}

int main() {
 Goo this_goo;
 make(&this_goo);
 make_job(&this_goo, "rancher");
 make_job(&this_goo, "farmer");
 make_job(&this_goo, "exec");
 make_job(&this_goo, "cook");
 return 0;
}

int make_job(Goo *goo, const char arg[]){

 Job *job_1, *curr_job;

   job_1 = malloc(sizeof(struct job));
   strcpy(job_1->job_name, arg);
   job_1->prev = NULL;
   job_1->next = NULL;
   job_1->parent_dir = goo;

   /*insert file into linked list*/
   if(goo->first_job == NULL){
     goo->first_job = job_1;
     job_1->next = NULL;
     return 0;
   }else{
   curr_job = goo->first_job;
     while(curr_job->next != NULL){
       if(strcmp(curr_job->job_name, arg) > 0){
         job_1->next = curr_job->next;
         job_1->prev = curr_job;
         curr_job->next = job_1;
         return 0;
       }
      curr_job = curr_job->next;
     }
       job_1->next = NULL;
       job_1->prev = curr_job;
       curr_job->next = job_1;
      return 0;
   }
 return 0;
}


This post was edited by lopelurag on Apr 1 2013 08:04am
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Apr 1 2013 08:39am
When I set the while loop to while(curr_job->next == NULL), it doesn't seg fault... :cry:

Honestly no idea why this is happening.

Then it segfaults on the line curr_job->next = job_1;

Code
}else{
  curr_job = goo->first_job;
    while(curr_job->next == NULL){
      if(strcmp(curr_job->job_name, arg) > 0){
        job_1->next = curr_job->next;
        job_1->prev = curr_job;
        curr_job->next = job_1;
        return 0;
      }
     curr_job = curr_job->next;
    }
      job_1->next = NULL;
      job_1->prev = curr_job;
      curr_job->next = job_1;
     return 0;
  }


This post was edited by lopelurag on Apr 1 2013 08:42am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 1 2013 11:20am
Quote (lopelurag @ Apr 1 2013 10:39am)
When I set the while loop to while(curr_job->next == NULL), it doesn't seg fault...  :cry:

Honestly no idea why this is happening.

Then it segfaults on the line curr_job->next = job_1;

Code
}else{
  curr_job = goo->first_job;
    while(curr_job->next == NULL){
      if(strcmp(curr_job->job_name, arg) > 0){
        job_1->next = curr_job->next;
        job_1->prev = curr_job;
        curr_job->next = job_1;
        return 0;
      }
     curr_job = curr_job->next;
    }
      job_1->next = NULL;
      job_1->prev = curr_job;
      curr_job->next = job_1;
     return 0;
  }

eventually you reach the end if jobname isn't found, which means cur_job-> next is null, so curr_job becomes null.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Apr 1 2013 11:57am
Quote (carteblanche @ Apr 1 2013 01:20pm)
eventually you reach the end if jobname isn't found, which means cur_job-> next is null, so curr_job becomes null.


Yeah actually think it's my make method, it's supposed to initialize Goo and all it's pointers to NULL.

It doesn't seem to initialize the pointer's with NULL so not sure what, if anything, is wrong there.

Quote (carteblanche @ Apr 1 2013 01:20pm)
eventually you reach the end if jobname isn't found, which means cur_job-> next is null, so curr_job becomes null.


Also the loop should never reach cur->job = NULL b/c it exits when curr->job->next = NULL?

The code never goes into the if statement, so I'm assuming something is wrong when I intialize this_goo in make.

Code
/*insert file into linked list*/
  if(goo->first_job == NULL){
    goo->first_job = job_1;
    job_1->next = NULL;
    return 0;
  }


This post was edited by lopelurag on Apr 1 2013 12:04pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 1 2013 04:31pm
what ide/debugger are you using?

i use codeblocks and debugging is easy in it because i can step line by line and setup watches for specific variables and structs and pointers to structs so i can watch their values change as i step through the application.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Apr 1 2013 10:07pm
Quote (AbDuCt @ Apr 1 2013 06:31pm)
what ide/debugger are you using?

i use codeblocks and debugging is easy in it because i can step line by line and setup watches for specific variables and structs and pointers to structs so i can watch their values change as i step through the application.


Yeah I just rewrote the code and it works now

Got another question tho

Say you take in a parameter that is a pointer to a specific node in a tree.

I want to return the parent node of the current node and set the original pointer to that location

I'm assuming you can't just say curr_node (the parameter pointer) = curr_node->parent?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 1 2013 10:39pm
Quote (lopelurag @ Apr 2 2013 12:07am)
Yeah I just rewrote the code and it works now

Got another question tho

Say you take in a parameter that is a pointer to a specific node in a tree.

I want to return the parent node of the current node and set the original pointer to that location

I'm assuming you can't just say curr_node (the parameter pointer) = curr_node->parent?

if you design it in such a way that every node has a parent, then it works fine. but if you didn't design it that way, then no.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Apr 1 2013 10:55pm
Quote (carteblanche @ Apr 2 2013 12:39am)
if you design it in such a way that every node has a parent, then it works fine. but if you didn't design it that way, then no.


Yea I did design it that way, some reason wasn't working so I just wanted to make sure.

I'll keep tweaking ty
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll