gcc compile problem

@

@(none)

I have the following file:
bugtest.c

I compile it on a RedHat Linux machine, using gcc

ehud@localhost ~>gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

the compilation command line is:
gcc -ggdb -O0 -o bug_test bug_test.c

When looking in kdbg at the "test" variable, it has no valid address,
and I can not view its contents.
The file bugtest.out is however OK...

Any help?
Thanks
Ehud
=================================================================================

#include <stdio.h>

#define GRAND_N 624

typedef struct {
unsigned long state[GRAND_N]; /*!< the array for the state vector
*/
int left; /*!< can *next++ this many times before
reloading */
unsigned long *next; /*!< next random value is computed from here */
} genRandT;


int main() {

genRandT test;
genRandT test2;
int i;
FILE *fp;

for (i=0; i<GRAND_N; i++) {
test.state = i;
}
test.left = 1;
test.next = &(test.state[50]);

fp = fopen("bugtest.out","w");
for (i=0;i<GRAND_N;i++) {
fprintf(fp, "%d ", test.state);
}
return 0;
}
 
B

benakiva

Hi Ehud,

Try, instead, to declare test as:

genRandT *test;

test = (genRandT *)malloc(sizeof(genRandT));

if (test != NULL) {
/* your code here */
}

cheers,
Itzhak
 
N

Nils Petter Vaskinn


Please put your reply below the post you are answering and quote only the
parts you respond to. Makes threads easier to follow for everyone.
Try, instead, to declare test as:

genRandT *test;

test = (genRandT *)malloc(sizeof(genRandT));

casting the return from malloc is unnessesary and a bad idea since it can
hide the error of failing to include stdlib.h
if (test != NULL) {

if(test) {
is shorter, though if it's far from the declaration the comparison to NULL
makes it clear that it's a pointer we're dealing with. I usually prefer
the short form.
/* your code here */
}

hth
 
R

Robert Stankowic

Hi Ehud,

Try, instead, to declare test as:

genRandT *test;

test = (genRandT *)malloc(sizeof(genRandT));

better:
#include <stdlib.h>
.....
test = malloc(sizeof genRandT);

But as far as I can see, Ehud's code is OK
6.5.3.2 Address and indirection operators
Constraints
1 The operand of the unary & operator shall be either a function designator,
the result of a
[] or unary * operator, or an lvalue that designates an object that is not a
bit-field and is
not declared with the register storage-class specifier.
test is neither a bitfield nor a register variable :)

Ehud, see below..
if (test != NULL) {
/* your code here */
}

cheers,
Itzhak


"@(none)" <""ehud\"@(none)"> wrote in message
I have the following file:
bugtest.c

I compile it on a RedHat Linux machine, using gcc

ehud@localhost ~>gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

the compilation command line is:
gcc -ggdb -O0 -o bug_test bug_test.c

When looking in kdbg at the "test" variable, it has no valid address,
and I can not view its contents.
The file bugtest.out is however OK...

Any help?
Thanks
Ehud
============================================================================
=====
#include <stdio.h>

#define GRAND_N 624

typedef struct {
unsigned long state[GRAND_N]; /*!< the array for the state vector
*/
int left; /*!< can *next++ this many times before
reloading */
unsigned long *next; /*!< next random value is computed from here */
} genRandT;


int main() {

genRandT test;
genRandT test2;
int i;
FILE *fp;

for (i=0; i<GRAND_N; i++) {
test.state = i;
}
test.left = 1;
test.next = &(test.state[50]);


The braces are unnecessary.
Try here:
printf("%p\n", (void *)&test.state[50]);
printf("%p\n", (void *)test.next);

if this shows the correct result (which it should) then there is a problem
or restriction in kdbg.
Have a look at the documentation.
fp = fopen("bugtest.out","w");
for (i=0;i<GRAND_N;i++) {
fprintf(fp, "%d ", test.state);
}
return 0;
}


HTH
Robert
 
E

Ehud Reshef

Hi
This is Ehud again,
I tried to debug with ddd, and it seems to be OK...
Since both work with gdb underneath, I really do not understand this problem...

Ehud

Robert Stankowic said:
Hi Ehud,

Try, instead, to declare test as:

genRandT *test;

test = (genRandT *)malloc(sizeof(genRandT));

better:
#include <stdlib.h>
....
test = malloc(sizeof genRandT);

But as far as I can see, Ehud's code is OK
6.5.3.2 Address and indirection operators
Constraints
1 The operand of the unary & operator shall be either a function designator,
the result of a
[] or unary * operator, or an lvalue that designates an object that is not a
bit-field and is
not declared with the register storage-class specifier.
test is neither a bitfield nor a register variable :)

Ehud, see below..
if (test != NULL) {
/* your code here */
}

cheers,
Itzhak


"@(none)" <""ehud\"@(none)"> wrote in message
I have the following file:
bugtest.c

I compile it on a RedHat Linux machine, using gcc

ehud@localhost ~>gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

the compilation command line is:
gcc -ggdb -O0 -o bug_test bug_test.c

When looking in kdbg at the "test" variable, it has no valid address,
and I can not view its contents.
The file bugtest.out is however OK...

Any help?
Thanks
Ehud
============================================================================
=====
#include <stdio.h>

#define GRAND_N 624

typedef struct {
unsigned long state[GRAND_N]; /*!< the array for the state vector
*/
int left; /*!< can *next++ this many times before
reloading */
unsigned long *next; /*!< next random value is computed from here */
} genRandT;


int main() {

genRandT test;
genRandT test2;
int i;
FILE *fp;

for (i=0; i<GRAND_N; i++) {
test.state = i;
}
test.left = 1;
test.next = &(test.state[50]);


The braces are unnecessary.
Try here:
printf("%p\n", (void *)&test.state[50]);
printf("%p\n", (void *)test.next);

if this shows the correct result (which it should) then there is a problem
or restriction in kdbg.
Have a look at the documentation.
fp = fopen("bugtest.out","w");
for (i=0;i<GRAND_N;i++) {
fprintf(fp, "%d ", test.state);
} return 0;
}


HTH
Robert
 
M

Martin Ambuhl

Hi Ehud,

Try, instead, to declare test as:

genRandT *test;

test = (genRandT *)malloc(sizeof(genRandT));

if (test != NULL) {
/* your code here */
}

Or just:
{
genRandT *test;
if ((test = malloc(sizeof *test))) {
/* your code here */
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,077
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top