Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Seg fault even though address space is accessible

Reply
Thread Tools

Seg fault even though address space is accessible

 
 
Ajay
Guest
Posts: n/a
 
      07-29-2008
Hi All,

I got a segmentation fault while accessing a structure element.
I pasted the output from gdb and the backtrace.

However if I am able successfully access the structure using the
address from the gdb output
after it seg faulted. Output from gdb also give below.

Any idea y this is happening?
yes I am trying to access the structure element from where it seg
faulted.

I am surely missing something.
Any help is appreciated!

Thanks,
Ajay.

$$$$$$$$$$$$$ SEG FAULT $$$$$$$$$$$$$$$$$

Program received signal SIGSEGV, Segmentation fault.
checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
at compute_sticky_IP_per_ASN.c:776
776 vector = vector->nextVector;

$$$$$$$$$$$$$$$$ BT $$$$$$$$$$$$$$$$$$$$

(gdb) bt
#0 checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
at compute_sticky_IP_per_ASN.c:776
#1 0x08051bb3 in buildPeerDetailStructFromLine (line=0x8418dd0 "566",
peerStruct=0x8047124) at compute_sticky_IP_per_ASN.c:752
#2 0x080522c1 in main (argc=11, argv=0x804734
at compute_sticky_IP_per_ASN.c:179

$$$$$$$$$$$$$$$$$$$$ ACCESSING STRUCTURE $$$$$$$$$$$$$$$$$$$

(gdb) p *(bitfieldItemStruct *) 0x84187a0
$1 = {BFVector = 3309413121, nextVector = 0x84187b0}
(gdb) p ((bitfieldItemStruct *) 0x84187a0)->nextVector
$2 = (struct bitfieldItem *) 0x84187b0
(gdb) p ((struct bitfieldItem *) 0x84187a0)->nextVector
$3 = (struct bitfieldItem *) 0x84187b0
 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 2:52*pm, Ajay <(E-Mail Removed)> wrote:
> Hi All,
>
> I got a segmentation fault while accessing a structure element.
> I pasted the output from gdb and the backtrace.
>
> However if I am able successfully access the structure using the
> address from the gdb output
> after it seg faulted. Output from gdb also give below.
>
> Any idea y this is happening?
> yes I am trying to access the structure element from where it seg
> faulted.
>
> I am surely missing something.
> Any help is appreciated!
>
> Thanks,
> Ajay.
>
> $$$$$$$$$$$$$ SEG FAULT $$$$$$$$$$$$$$$$$
>
> Program received signal SIGSEGV, Segmentation fault.
> checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> * * at compute_sticky_IP_per_ASN.c:776
> 776 * * * * * * * * * * vector = vector->nextVector;
>
> $$$$$$$$$$$$$$$$ BT $$$$$$$$$$$$$$$$$$$$
>
> (gdb) bt
> #0 *checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> * * at compute_sticky_IP_per_ASN.c:776
> #1 *0x08051bb3 in buildPeerDetailStructFromLine (line=0x8418dd0 "566",
> * * peerStruct=0x8047124) at compute_sticky_IP_per_ASN.c:752
> #2 *0x080522c1 in main (argc=11, argv=0x804734
> * * at compute_sticky_IP_per_ASN.c:179
>
> $$$$$$$$$$$$$$$$$$$$ ACCESSING STRUCTURE $$$$$$$$$$$$$$$$$$$
>
> (gdb) p *(bitfieldItemStruct *) 0x84187a0
> $1 = {BFVector = 3309413121, nextVector = 0x84187b0}
> (gdb) p ((bitfieldItemStruct *) 0x84187a0)->nextVector
> $2 = (struct bitfieldItem *) 0x84187b0
> (gdb) p ((struct bitfieldItem *) 0x84187a0)->nextVector
> $3 = (struct bitfieldItem *) 0x84187b0


Maybe an alignment problem.
Did you try the GNU groups?
http://www.nabble.com/Gnu---gdb-f1618.html
news:gnu.gdb.bug
 
Reply With Quote
 
 
 
 
Ajay
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 6:01*pm, user923005 <(E-Mail Removed)> wrote:
> On Jul 29, 2:52*pm, Ajay <(E-Mail Removed)> wrote:
>
>
>
> > Hi All,

>
> > I got a segmentation fault while accessing a structure element.
> > I pasted the output from gdb and the backtrace.

>
> > However if I am able successfully access the structure using the
> > address from the gdb output
> > after it seg faulted. Output from gdb also give below.

>
> > Any idea y this is happening?
> > yes I am trying to access the structure element from where it seg
> > faulted.

>
> > I am surely missing something.
> > Any help is appreciated!

>
> > Thanks,
> > Ajay.

>
> > $$$$$$$$$$$$$ SEG FAULT $$$$$$$$$$$$$$$$$

>
> > Program received signal SIGSEGV, Segmentation fault.
> > checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> > * * at compute_sticky_IP_per_ASN.c:776
> > 776 * * * * * * * * * * vector = vector->nextVector;

>
> > $$$$$$$$$$$$$$$$ BT $$$$$$$$$$$$$$$$$$$$

>
> > (gdb) bt
> > #0 *checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> > * * at compute_sticky_IP_per_ASN.c:776
> > #1 *0x08051bb3 in buildPeerDetailStructFromLine (line=0x8418dd0 "566",
> > * * peerStruct=0x8047124) at compute_sticky_IP_per_ASN.c:752
> > #2 *0x080522c1 in main (argc=11, argv=0x804734
> > * * at compute_sticky_IP_per_ASN.c:179

>
> > $$$$$$$$$$$$$$$$$$$$ ACCESSING STRUCTURE $$$$$$$$$$$$$$$$$$$

>
> > (gdb) p *(bitfieldItemStruct *) 0x84187a0
> > $1 = {BFVector = 3309413121, nextVector = 0x84187b0}
> > (gdb) p ((bitfieldItemStruct *) 0x84187a0)->nextVector
> > $2 = (struct bitfieldItem *) 0x84187b0
> > (gdb) p ((struct bitfieldItem *) 0x84187a0)->nextVector
> > $3 = (struct bitfieldItem *) 0x84187b0

>
> Maybe an alignment problem.
> Did you try the GNU groups?http://www.nabble.com/Gnu---gdb-f1618.html
> news:gnu.gdb.bug


Hmmm! I haven't, Let me try that.


But, how can I find if its an alignment problem?

However, I checked the address space which are function parameters.
And it appears it is aligned.

-Ajay.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-29-2008
Ajay <(E-Mail Removed)> writes:

> I got a segmentation fault while accessing a structure element.
> I pasted the output from gdb and the backtrace.


The usual advice is to post code. Ideally code you have cut right
down but which still exhibits the problem. Doing this often isolates
the issue anyway, but without code to see it is tough...

> However if I am able successfully access the structure using the
> address from the gdb output
> after it seg faulted. Output from gdb also give below.
>
> Any idea y this is happening?
> yes I am trying to access the structure element from where it seg
> faulted.
>
> I am surely missing something.
> Any help is appreciated!
>
> Thanks,
> Ajay.
>
> $$$$$$$$$$$$$ SEG FAULT $$$$$$$$$$$$$$$$$
>
> Program received signal SIGSEGV, Segmentation fault.
> checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> at compute_sticky_IP_per_ASN.c:776
> 776 vector = vector->nextVector;
>
> $$$$$$$$$$$$$$$$ BT $$$$$$$$$$$$$$$$$$$$
>
> (gdb) bt
> #0 checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> at compute_sticky_IP_per_ASN.c:776
> #1 0x08051bb3 in buildPeerDetailStructFromLine (line=0x8418dd0 "566",
> peerStruct=0x8047124) at compute_sticky_IP_per_ASN.c:752
> #2 0x080522c1 in main (argc=11, argv=0x804734
> at compute_sticky_IP_per_ASN.c:179
>
> $$$$$$$$$$$$$$$$$$$$ ACCESSING STRUCTURE $$$$$$$$$$$$$$$$$$$
>
> (gdb) p *(bitfieldItemStruct *) 0x84187a0
> $1 = {BFVector = 3309413121, nextVector = 0x84187b0}
> (gdb) p ((bitfieldItemStruct *) 0x84187a0)->nextVector
> $2 = (struct bitfieldItem *) 0x84187b0
> (gdb) p ((struct bitfieldItem *) 0x84187a0)->nextVector
> $3 = (struct bitfieldItem *) 0x84187b0


To me, this does not show anything interesting. The fault is with
'vector' not 'BFVecList' which is the only thing that has the value
0x84187a0 (as far as I can see). Did you miss out the 'print vector'
output showing it to be equal to 'BFVecList'? Why not just try to
print vector->nextVector rather than using some mysterious (to me)
address?

--
Ben.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-29-2008
Ajay <(E-Mail Removed)> wrote:
> I got a segmentation fault while accessing a structure element.
> I pasted the output from gdb and the backtrace.


Wich is rather useless without seeing the code. Without it the
gdb output is mostly meaningless.

> However if I am able successfully access the structure using the
> address from the gdb output
> after it seg faulted. Output from gdb also give below.


> $$$$$$$$$$$$$ SEG FAULT $$$$$$$$$$$$$$$$$


> Program received signal SIGSEGV, Segmentation fault.
> checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> at compute_sticky_IP_per_ASN.c:776
> 776 vector = vector->nextVector;


What value has 'vector here? Did you check that it's an
address you can legally use as a pointer? Without seeing
your code there's nothing that indicates that 'vector'
has anything to do with the address 0x84187a0.

> $$$$$$$$$$$$$$$$ BT $$$$$$$$$$$$$$$$$$$$


> (gdb) bt
> #0 checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> at compute_sticky_IP_per_ASN.c:776
> #1 0x08051bb3 in buildPeerDetailStructFromLine (line=0x8418dd0 "566",
> peerStruct=0x8047124) at compute_sticky_IP_per_ASN.c:752
> #2 0x080522c1 in main (argc=11, argv=0x804734
> at compute_sticky_IP_per_ASN.c:179


> $$$$$$$$$$$$$$$$$$$$ ACCESSING STRUCTURE $$$$$$$$$$$$$$$$$$$


> (gdb) p *(bitfieldItemStruct *) 0x84187a0
> $1 = {BFVector = 3309413121, nextVector = 0x84187b0}
> (gdb) p ((bitfieldItemStruct *) 0x84187a0)->nextVector
> $2 = (struct bitfieldItem *) 0x84187b0
> (gdb) p ((struct bitfieldItem *) 0x84187a0)->nextVector


I don't understand what you're doing here. Doing

p ((bitfieldItemStruct *) 0x84187a0)->nextVector
p ((struct bitfieldItem *) 0x84187a0)->nextVector

looks suspicious. Note that you use the same address in
both cases, just casting it to a different type - or is
it the same? If yes, why do you think it's relevant to
check the same thing twice?

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Ajay
Guest
Posts: n/a
 
      07-29-2008
Hi All,

Thanks for all your replays.

I am posting the code here.


typedef struct bitfieldItem {
uint32_t BFVector;
const struct bitfieldItem *nextVector;
} bitfieldItemStruct, *bitfieldItemStructPtr;

main()
{
***
buildBFVectorList(currPeerDetailItem->bitfieldStr,
&(currPeerDetailItem->bitfieldVector));
checkBFVectorList(currPeerDetailItem->bitfieldVector,
currPeerDetailItem->bitfieldLength);

***

}


int checkBFVectorList(bitfieldItemStruct **BFVecList, unsigned int
BFlen)
{
bitfieldItemStruct *vector = *BFVecList;
float numVectors = ceil((float)BFlen/32.0);
float vector_id = 0;
printf ("Verifying BF vector\n");
printf ("BFlen = %d, numvectors = %f\n", BFlen, numVectors);
printf ("The BF vector is: ");
while (vector != NULL)
{
printf ("%d ", vector->BFVector);
vector = vector->nextVector;
vector_id++;
}
printf ("\nvector_id = %d\n", vector_id);
assert (vector_id == numVectors);
return 1;
}

int
buildBFVectorList(char *BFStr, bitfieldItemStruct **BFVecList)
{
bitfieldItemStruct *BFVecHead, *currBFVec, *tmpBFVec;
char * bit;
unsigned int num_bits_pushed = 0;
unsigned int total_num_bits_pushed = 0;
unsigned int num_vectors = 0;

if (verboseLevel >= 7) printf ("The BF string is %s\n", BFStr);
bit = (char *) strtok(BFStr, ",");
BFVecHead = (bitfieldItemStruct *) calloc(1,
sizeof(bitfieldItemStruct *));
BFVecHead->BFVector = 0;
BFVecHead->nextVector = NULL;
currBFVec = BFVecHead;
num_vectors = 1;
printf ("Before %u\n", BFVecHead->BFVector);

while (bit != NULL)
{
if (verboseLevel >= 9) printf ("%d: Current bit is %s\n",
total_num_bits_pushed+1, bit);
if (num_bits_pushed >= 32) {
printf ("\nAfter %u\n", currBFVec->BFVector);
if (tmpBFVec = (bitfieldItemStruct *) calloc(1,
sizeof(bitfieldItemStruct *)) == NULL) {
printf ("Memory reallocation failed for object of type
'bitfieldItemStruct*'.\n");
exit(-1);
}
tmpBFVec->BFVector= 0;
tmpBFVec->nextVector = NULL;
currBFVec->nextVector = tmpBFVec;
currBFVec = tmpBFVec;
num_bits_pushed = 0;
num_vectors++;
printf ("Before %u\n", currBFVec->BFVector);
}

currBFVec->BFVector = currBFVec->BFVector << 1;
printf ("%u", atoi(bit));
currBFVec->BFVector += atoi(bit);
num_bits_pushed++;
total_num_bits_pushed++;
bit = (char *) strtok(NULL, ",");
}
printf ("\nAfter %u\n", currBFVec->BFVector);
*BFVecList = BFVecHead;
printf ("The total number of bits in the BFvector is %d and num
vectors is %d.\n", total_num_bits_pushed, num_vectors);
return 1;
}



On Jul 29, 6:18*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> Ajay <(E-Mail Removed)> writes:
> > I got a segmentation fault while accessing a structure element.
> > I pasted the output from gdb and the backtrace.

>
> The usual advice is to post code. *Ideally code you have cut right
> down but which still exhibits the problem. *Doing this often isolates
> the issue anyway, but without code to see it is tough...
>
>
>
> > However if I am able successfully access the structure using the
> > address from the gdb output
> > after it seg faulted. Output from gdb also give below.

>
> > Any idea y this is happening?
> > yes I am trying to access the structure element from where it seg
> > faulted.

>
> > I am surely missing something.
> > Any help is appreciated!

>
> > Thanks,
> > Ajay.

>
> > $$$$$$$$$$$$$ SEG FAULT $$$$$$$$$$$$$$$$$

>
> > Program received signal SIGSEGV, Segmentation fault.
> > checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> > * * at compute_sticky_IP_per_ASN.c:776
> > 776 * * * * * * * * * * vector = vector->nextVector;

>
> > $$$$$$$$$$$$$$$$ BT $$$$$$$$$$$$$$$$$$$$

>
> > (gdb) bt
> > #0 *checkBFVectorList (BFVecList=0x84187a0, BFlen=699)
> > * * at compute_sticky_IP_per_ASN.c:776
> > #1 *0x08051bb3 in buildPeerDetailStructFromLine (line=0x8418dd0 "566",
> > * * peerStruct=0x8047124) at compute_sticky_IP_per_ASN.c:752
> > #2 *0x080522c1 in main (argc=11, argv=0x804734
> > * * at compute_sticky_IP_per_ASN.c:179

>
> > $$$$$$$$$$$$$$$$$$$$ ACCESSING STRUCTURE $$$$$$$$$$$$$$$$$$$

>
> > (gdb) p *(bitfieldItemStruct *) 0x84187a0
> > $1 = {BFVector = 3309413121, nextVector = 0x84187b0}
> > (gdb) p ((bitfieldItemStruct *) 0x84187a0)->nextVector
> > $2 = (struct bitfieldItem *) 0x84187b0
> > (gdb) p ((struct bitfieldItem *) 0x84187a0)->nextVector
> > $3 = (struct bitfieldItem *) 0x84187b0

>
> To me, this does not show anything interesting. *The fault is with
> 'vector' not 'BFVecList' which is the only thing that has the value
> 0x84187a0 (as far as I can see). *Did you miss out the 'print vector'
> output showing it to be equal to 'BFVecList'? *Why not just try to
> print vector->nextVector rather than using some mysterious (to me)
> address?
>
> --
> Ben.


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-29-2008
Ajay <(E-Mail Removed)> writes:

> I am posting the code here.


Makes all the difference... BTW, top-posting is not in favour in the
group.

> typedef struct bitfieldItem {
> uint32_t BFVector;
> const struct bitfieldItem *nextVector;
> } bitfieldItemStruct, *bitfieldItemStructPtr;

<snip>
> int
> buildBFVectorList(char *BFStr, bitfieldItemStruct **BFVecList)
> {
> bitfieldItemStruct *BFVecHead, *currBFVec, *tmpBFVec;
> char * bit;
> unsigned int num_bits_pushed = 0;
> unsigned int total_num_bits_pushed = 0;
> unsigned int num_vectors = 0;
>
> if (verboseLevel >= 7) printf ("The BF string is %s\n", BFStr);
> bit = (char *) strtok(BFStr, ",");
> BFVecHead = (bitfieldItemStruct *) calloc(1,
> sizeof(bitfieldItemStruct *));


This is wrong. There is no need for calloc (you set the memory
immediately), the cast is unnecessary, and you don't check the result.
However, the reason things go really wrong is the size is not right.

The comp.lang.c approved method is to write:

BFVecHead = malloc(sizeof *BFVecHead);

This way you can see the size is right (provided the type of the
pointer is correct) and you will get an error message if you have
forgotten to include stdlib.h.

> BFVecHead->BFVector = 0;
> BFVecHead->nextVector = NULL;
> currBFVec = BFVecHead;
> num_vectors = 1;
> printf ("Before %u\n", BFVecHead->BFVector);
>
> while (bit != NULL)
> {
> if (verboseLevel >= 9) printf ("%d: Current bit is %s\n",
> total_num_bits_pushed+1, bit);
> if (num_bits_pushed >= 32) {
> printf ("\nAfter %u\n", currBFVec->BFVector);
> if (tmpBFVec = (bitfieldItemStruct *) calloc(1,
> sizeof(bitfieldItemStruct *)) == NULL) {


Ditto.

> printf ("Memory reallocation failed for object of type
> 'bitfieldItemStruct*'.\n");
> exit(-1);
> }
> tmpBFVec->BFVector= 0;
> tmpBFVec->nextVector = NULL;
> currBFVec->nextVector = tmpBFVec;
> currBFVec = tmpBFVec;
> num_bits_pushed = 0;
> num_vectors++;
> printf ("Before %u\n", currBFVec->BFVector);
> }
>
> currBFVec->BFVector = currBFVec->BFVector << 1;
> printf ("%u", atoi(bit));
> currBFVec->BFVector += atoi(bit);
> num_bits_pushed++;
> total_num_bits_pushed++;
> bit = (char *) strtok(NULL, ",");
> }
> printf ("\nAfter %u\n", currBFVec->BFVector);
> *BFVecList = BFVecHead;
> printf ("The total number of bits in the BFvector is %d and num
> vectors is %d.\n", total_num_bits_pushed, num_vectors);
> return 1;
> }


--
Ben.
 
Reply With Quote
 
Ajay
Guest
Posts: n/a
 
      07-30-2008
On Jul 29, 7:25*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> Ajay <(E-Mail Removed)> writes:
> > I am posting the code here.

>
> Makes all the difference... *BTW, top-posting is not in favour in the
> group.
>
>
>
>
>
> > typedef struct bitfieldItem {
> > * * uint32_t * *BFVector;
> > * * const struct bitfieldItem *nextVector;
> > } bitfieldItemStruct, *bitfieldItemStructPtr;

> <snip>
> > int
> > buildBFVectorList(char *BFStr, bitfieldItemStruct **BFVecList)
> > {
> > * * bitfieldItemStruct *BFVecHead, *currBFVec, *tmpBFVec;
> > * * char * bit;
> > * * unsigned int num_bits_pushed = 0;
> > * * unsigned int total_num_bits_pushed = 0;
> > * * unsigned int num_vectors = 0;

>
> > * * if (verboseLevel >= 7) printf ("The BF string is %s\n", BFStr);
> > * * bit = (char *) strtok(BFStr, ",");
> > * * BFVecHead = (bitfieldItemStruct *) calloc(1,
> > sizeof(bitfieldItemStruct *));

>
> This is wrong. *There is no need for calloc (you set the memory
> immediately), the cast is unnecessary, and you don't check the result.
> However, the reason things go really wrong is the size is not right.
>
> The comp.lang.c approved method is to write:
>
> * * BFVecHead = malloc(sizeof *BFVecHead);
>
> This way you can see the size is right (provided the type of the
> pointer is correct) and you will get an error message if you have
> forgotten to include stdlib.h.
>
> > * * BFVecHead->BFVector = 0;
> > * * BFVecHead->nextVector = NULL;
> > * * currBFVec = BFVecHead;
> > * * num_vectors = 1;
> > * * printf ("Before %u\n", BFVecHead->BFVector);

>
> > * * while (bit != NULL)
> > * * {
> > * * * * if (verboseLevel >= 9) printf ("%d: Current bit is %s\n",
> > total_num_bits_pushed+1, bit);
> > * * * * if (num_bits_pushed >= 32) {
> > * * * * * * printf ("\nAfter %u\n", currBFVec->BFVector);
> > * * * * * * if (tmpBFVec = (bitfieldItemStruct *) calloc(1,
> > sizeof(bitfieldItemStruct *)) == NULL) {

>
> Ditto.
>
>
>
> > * * * * * * * * printf ("Memory reallocation failed for object of type
> > 'bitfieldItemStruct*'.\n");
> > * * * * * * * * exit(-1);
> > * * * * * * }
> > * * * * * * tmpBFVec->BFVector= 0;
> > * * * * * * tmpBFVec->nextVector = NULL;
> > * * * * * * currBFVec->nextVector = tmpBFVec;
> > * * * * * * currBFVec = tmpBFVec;
> > * * * * * * num_bits_pushed = 0;
> > * * * * * * num_vectors++;
> > * * * * * * printf ("Before %u\n", currBFVec->BFVector);
> > * * * * }

>
> > * * * * currBFVec->BFVector = currBFVec->BFVector << 1;
> > * * * * printf ("%u", atoi(bit));
> > * * * * currBFVec->BFVector += atoi(bit);
> > * * * * num_bits_pushed++;
> > * * * * total_num_bits_pushed++;
> > * * * * bit = (char *) strtok(NULL, ",");
> > * * }
> > * * printf ("\nAfter %u\n", currBFVec->BFVector);
> > * * *BFVecList = BFVecHead;
> > * * printf ("The total number of bits in the BFvector is %d and num
> > vectors is %d.\n", total_num_bits_pushed, num_vectors);
> > * * return 1;
> > }

>
> --
> Ben.


Hi Ben,

Thanks for your replay!
I was using malloc but after the error appeared, I tried using calloc.
I changed back the code to using malloc.
The Seg. fault error still persists.
The new code is posted below:

Thanks,
Ajay.



int
checkBFVectorList(bitfieldItemStruct **BFVecList, unsigned int BFlen)
{
bitfieldItemStruct *vector = *BFVecList;
float numVectors = ceil((float)BFlen/32.0);
float vector_id = 0;
printf ("Verifying BF vector\n");
printf ("BFlen = %d, numvectors = %f\n", BFlen, numVectors);
printf ("The BF vector is: ");
while (vector != NULL)
{
/* if (vector->BFVector != NULL) printf ("%u ", vector-
>BFVector);

else { printf ("Error occured\n"); exit(-1); } */
/* printf ("%d ", vector->BFVector);*/
vector = vector->nextVector;
vector_id++;
}
printf ("\nvector_id = %d\n", vector_id);
assert (vector_id == numVectors);
return 1;
}

int
buildBFVectorList(char *BFStr, bitfieldItemStruct **BFVecList)
{
bitfieldItemStruct *BFVecHead, *currBFVec, *tmpBFVec;
char * bit;
unsigned int num_bits_pushed = 0;
unsigned int total_num_bits_pushed = 0;
unsigned int num_vectors = 0;

if (verboseLevel >= 7) printf ("The BF string is %s\n", BFStr);
bit = (char *) strtok(BFStr, ",");
BFVecHead = malloc(sizeof(*BFVecHead));
if (BFVecHead == NULL) {
perror("Error: Memory could not be allocated:");
exit(-1);
}
BFVecHead->BFVector = 0;
BFVecHead->nextVector = NULL;
currBFVec = BFVecHead;
num_vectors = 1;
printf ("Before %u\n", BFVecHead->BFVector);

while (bit != NULL)
{
if (verboseLevel >= 9) printf ("%d: Current bit is %s\n",
total_num_bits_pushed+1, bit);
if (num_bits_pushed >= 32) {
printf ("\nAfter %u\n", currBFVec->BFVector);
tmpBFVec = malloc(sizeof(*tmpBFVec));
if (tmpBFVec == NULL) {
perror("Error: Memory could not be allocated:");
exit(-1);
}
tmpBFVec->BFVector= 0;
tmpBFVec->nextVector = NULL;
currBFVec->nextVector = tmpBFVec;
currBFVec = tmpBFVec;
num_bits_pushed = 0;
num_vectors++;
printf ("Before %u\n", currBFVec->BFVector);
}

currBFVec->BFVector = currBFVec->BFVector << 1;
printf ("%u", atoi(bit));
currBFVec->BFVector += atoi(bit);
num_bits_pushed++;
total_num_bits_pushed++;
bit = (char *) strtok(NULL, ",");
}
printf ("\nAfter %u\n", currBFVec->BFVector);
*BFVecList = BFVecHead;
printf ("The total number of bits in the BFvector is %d and num
vectors is %d.\n", total_num_bits_pushed, num_vectors);
return 1;
}

 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      07-30-2008
In article <(E-Mail Removed)>,
Ajay <(E-Mail Removed)> wrote:
>{
> bitfieldItemStruct *BFVecHead, *currBFVec, *tmpBFVec;
> [snip]
> BFVecHead = (bitfieldItemStruct *) calloc(1,
>sizeof(bitfieldItemStruct *));


This looks suspicious.
You allocate room for one pointer instead of room for one struct.
Also, the cast is unnecessary.

Regards,
Ike
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-30-2008
Ajay <(E-Mail Removed)> writes:

> Thanks for your replay!
> I was using malloc but after the error appeared, I tried using calloc.
> I changed back the code to using malloc.
> The Seg. fault error still persists.
> The new code is posted below:


Are you sure you are posting code you have compiled?

> int
> checkBFVectorList(bitfieldItemStruct **BFVecList, unsigned int BFlen)


> int
> buildBFVectorList(char *BFStr, bitfieldItemStruct **BFVecList)


elsewhere you posted:

| main()
| {
| ***
| buildBFVectorList(currPeerDetailItem->bitfieldStr,
| &(currPeerDetailItem->bitfieldVector));
| checkBFVectorList(currPeerDetailItem->bitfieldVector,
| currPeerDetailItem->bitfieldLength);
|
| ***
|
| }

The types here do not match up -- the compiler should reject it.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CPU Usage frequently 100% even though it seems not even close tototal memory Newcomer Computer Support 3 11-15-2009 06:51 AM
CPU Usage frequently 100% even though it seems not even close tototal memory Newcomer Computer Support 0 11-14-2009 11:21 PM
Wireless adapter losing network address even though signal is strong... Howard Woodard Wireless Networking 2 07-11-2009 07:21 PM
seg fault because stdexcept.cc not found? Corrine C++ 1 11-27-2003 06:15 AM
Suprising seg fault!!! Vinod C++ 3 09-27-2003 02:43 PM



Advertisments