By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

gcc compile problem

P: n/a
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] = 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[i]);
}
return 0;
}
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Ehud,

Try, instead, to declare test as:

genRandT *test;

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

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

cheers,
Itzhak
"@(none)" <""ehud\"@(none)"> wrote in message news:<3f********@news.barak.net.il>...
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] = 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[i]);
}
return 0;
}

Nov 13 '05 #2

P: n/a
On Tue, 18 Nov 2003 00:11:52 -0800, be******@wicc.weizmann.ac.il wrote:
Hi Ehud,
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

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 13 '05 #3

P: n/a

<be******@wicc.weizmann.ac.il> schrieb im Newsbeitrag
news:5d*************************@posting.google.co m...
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

news:<3f********@news.barak.net.il>...
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] = 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[i]);
}
return 0;
}


HTH
Robert
Nov 13 '05 #4

P: n/a
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" <pc******@netway.at> wrote in message news:<3f***********************@newsreader02.highw ay.telekom.at>...
<be******@wicc.weizmann.ac.il> schrieb im Newsbeitrag
news:5d*************************@posting.google.co m...
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

news:<3f********@news.barak.net.il>...
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] = 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[i]);
} return 0; }


HTH
Robert

Nov 13 '05 #5

P: n/a
be******@wicc.weizmann.ac.il wrote:
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 */
}
}

--
Martin Ambuhl

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.