C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 2nd, 2013, 06:16 PM
spdpsin spdpsin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 spdpsin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 35 sec
Reputation Power: 0
Linker error???

i am building a program that finds the longest path to calculate the solution...

this is the code:





#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define _GNU_SOURCE

int startIndex = 0;

int countOneBranchLength(int** mtrx, int rowcount, int curRow){
int res = 1;
int i = startIndex;
while (i < rowcount) {
if (mtrx[i][1] == mtrx[curRow][2]){
int nodesFollowing = mtrx[i][4];
if ( nodesFollowing > 0) {
int presStartIndex = startIndex;
startIndex = 0;
res += countLength(mtrx, rowcount, i, nodesFollowing) -2;
startIndex = presStartIndex;
}
startIndex = i+1;
}
i++;
}
return res + 1;
}

int countLength(int** mtrx, int rowcount, int curRow, int nodesFound){
int l = 0;
int lt = 0;
int j = 0;

while (j < nodesFound) {

lt = countOneBranchLength(mtrx,rowcount,curRow) + 1;
if (lt > l)
l = lt;
j++;
}
return l;
}

int main(int argc, const char * argv[])
{


int m,k,i,j,s1,s2,res = 0,currlength;
FILE *fp,*fp2;
fp=fopen("scidinner.in","r");
res = 0;


fscanf(fp,"%d %d",&m,&k);



int** mtrx;
mtrx=(int**)malloc(k*sizeof(int*));
for (i=0; i<k; i++)
mtrx[i]=(int*)malloc(4*sizeof(int));



i = 0;
while(!feof(fp) && i < k)
{
fscanf(fp,"%d %d",&s1,&s2);

mtrx[i][1] = s1;
mtrx[i][2] = s2;
mtrx[i][3] = -1;
mtrx[i][4] = 0;

i++;
}
fclose(fp);




i=0;
while (i < k) {
s2 = mtrx[i][2];


j=0;
while (j < k) {
if (mtrx[j][1] == s2){

mtrx[j][3] = i;


mtrx[i][4]++;
}
j++;
}
i++;
}




i=0;
while (i < k) {
if (mtrx[i][4] > 0){

startIndex = 0;
currlength = countLength(mtrx, k, i, mtrx[i][4]) ;
if (currlength > res)
res = currlength;
}
i++;
}


fp2=fopen("scidinner.out","w");

fprintf(fp2,"%d", res);
fclose(fp2);





for (i=0; i<k; i++)
free((void*)mtrx[i]);
free((void*)mtrx);

return res;
}




But my debug is giving me these error reports:

In function `main':
[Linker error] undefined reference to `countLength'
ld returned 1 exit status

and I can't find my error...Please Heeelp!!!

Reply With Quote
  #2  
Old February 2nd, 2013, 06:28 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,130 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 43 m 21 sec
Reputation Power: 1949
Code not formatted, rendering it unreadable. So I didn't bother to even try to read it. Nor will anybody else want to bother.

This was your sixth post here, so you have been told this already: use code tags! Here is what it looks like when you do; compare this with your opening post and see the difference.

Code:
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define _GNU_SOURCE

int startIndex = 0;

int countOneBranchLength(int** mtrx, int rowcount, int curRow){
    int res = 1;
    int i = startIndex;
       while (i < rowcount) {
        if (mtrx[i][1] == mtrx[curRow][2]){
            int nodesFollowing = mtrx[i][4];
            if ( nodesFollowing > 0) {
                int presStartIndex = startIndex;
                startIndex = 0;
                res += countLength(mtrx, rowcount, i, nodesFollowing) -2;
                startIndex = presStartIndex;
            }
            startIndex = i+1;
        }
        i++;
    }
    return res + 1;
}

int countLength(int** mtrx, int rowcount, int curRow, int nodesFound){
    int l = 0;
    int lt = 0;
    int j = 0;
    
    while (j < nodesFound) {
        
        lt = countOneBranchLength(mtrx,rowcount,curRow) + 1;
        if (lt > l)
            l = lt;
        j++;
    }
    return l;
}

int main(int argc, const char * argv[])
{
    
    
    int m,k,i,j,s1,s2,res = 0,currlength;
    FILE *fp,*fp2;
    fp=fopen("scidinner.in","r");
    res = 0;
    
  
    fscanf(fp,"%d %d",&m,&k);
    
    
   
    int** mtrx;
    mtrx=(int**)malloc(k*sizeof(int*));
    for (i=0; i<k; i++)
        mtrx[i]=(int*)malloc(4*sizeof(int));
    
   
   
    i = 0;
    while(!feof(fp) && i < k)
    {
        fscanf(fp,"%d %d",&s1,&s2);
        
        mtrx[i][1] = s1;
        mtrx[i][2] = s2;
        mtrx[i][3] = -1;
        mtrx[i][4] = 0;
        
        i++;
    }
    fclose(fp);
   
    
    

    i=0;
    while (i < k) {
        s2 =  mtrx[i][2];
        

        j=0;
        while (j < k) {
            if (mtrx[j][1] == s2){
 
                mtrx[j][3] = i;
                
  
                mtrx[i][4]++;
            }
            j++;
        }
        i++;
    }
    

    

    i=0;
    while (i < k) {
        if (mtrx[i][4] > 0){
  
            startIndex = 0;
            currlength = countLength(mtrx, k, i, mtrx[i][4]) ;
            if (currlength > res)
                res = currlength;
        }
        i++;
    }
    
    
    fp2=fopen("scidinner.out","w");

    fprintf(fp2,"%d", res);
    fclose(fp2);
    
    

    

    for (i=0; i<k; i++)
        free((void*)mtrx[i]);
    free((void*)mtrx);
    
    return res;
}


In the first function, countOneBranchLength, you call another function, countLength, which the compiler doesn't know anything about. countLength shows up later, but that doesn't do any good. You need to add a function prototype for countLength that is placed above countOneBranchLength.

Normally, this kind of mistake does not result in linker error, but rather in a "undefined reference" compiler warning.

Also, if this a C++, then because of function overloading you may need to check that you always give it the right argument types.

Reply With Quote
  #3  
Old February 3rd, 2013, 03:28 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
Code:
mtrx[i][1] = s1;
mtrx[i][2] = s2;
mtrx[i][3] = -1;
mtrx[i][4] = 0;

As far as I can tell, every single reference to the minor dimension of this matrix is 1-based rather than 0-based.

So EVERY reference to mrtx[x][4] (really, what did you save by not saying matrix everywhere) is an out of bounds access.

> int m,k,i,j,s1,s2,res = 0,currlength;
Use meaningful variable names.
i,j are fine, when they're just the loop control variables.
But m,k - what are those?
How about something like numRows and numCols.

As well as being readable, it's a lot harder to make a mistake (and a lot easier to spot a mistake) than a scattering of single letter identifiers.

A single letter variable takes only ONE key press to make the mistake, and the resulting bug is almost invisible to spot.
__________________
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Linker error???

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap