Is this valid program

V

vashwath

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


int main()
{
typedef union
{
int i;
char *s;
}union_t;


typedef struct
{
union_t union1;
int j;
}struct_t;


struct_t struct1;


struct1.union1.s = malloc(5);


strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]='\0';


printf("%s\n",struct1.union1.s);
return 0;
}

I am getting the expected output(i.e. ASHW) for the above program.

However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.

The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.

The question is, Can we allocate memory for a pointer declared inside a
union?
 
J

jat

the return type of malloc is (void *)
that's why ur testing tool may be giving an error.
u can explicitly cast it (char *) malloc(..) and then check out.
there is nothing wrong in assigning memory to a pointer of union
because only the value of the pointer is getting changed, the size of
union will be same.
 
V

vashwath

In the function that is under test, explicit type casting is done. In
the example program I have not shown it.
 
I

Irrwahn Grausewitz

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


int main()

int main(void)
{
typedef union
{
int i;
char *s;
}union_t;


typedef struct
{
union_t union1;
int j;
}struct_t;


struct_t struct1;


struct1.union1.s = malloc(5);

You want to check the return value of malloc here.
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]='\0';

Undefined behaviour. You write to memory you don't own, and
completely unnecessarily so - strcpy already appended a
terminating '\0'.
printf("%s\n",struct1.union1.s);
return 0;
}

I am getting the expected output(i.e. ASHW) for the above program.

Just bad luck.
However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.

The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.

The question is, Can we allocate memory for a pointer declared inside a
union?

Of course. Your RTRT tool is broken, ditch it.

Best regards.
 
F

Flash Gordon

jat wrote:


Provide CONTEXT. There is absolutely NO guarantee that people have seen
the message you are replying to, news groups don't work that way. If you
read yesterdays posts you will see that, like most other days, this was
stated yesterday and the instructions for using that horrible interface
known as Google Groups were posted.
the return type of malloc is (void *)
that's why ur testing tool may be giving an error.

Please don't use stupid abbreviations like ur and u, it just makes it
harder for *everyone* to read your posts.
u can explicitly cast it (char *) malloc(..) and then check out.

If you are using a C compiler then you do *not* need to cast the return
value of malloc since pointers to void are automatically converted to
pointers to other types. If you get a warning (or error) when you don't
provide the cast then you have made one of two errors:

1) You have not included stdlib.h
2) You are compiling the code as C++ where different rules apply

The OP has almost certainly committed one of these two errors in his
"similar kind of function" but seeing as the OP did not post the code
producing the error we can't say what the actual problem was.
there is nothing wrong in assigning memory to a pointer of union
because only the value of the pointer is getting changed, the size of
union will be same.

Indeed.
 
I

Irrwahn Grausewitz

Please preserve context and attribution lines when posting replies.
[Context restored]

In the function that is under test, explicit type casting is done. In
the example program I have not shown it.

Never, ever, cast the return value of malloc to suppress diagnostics.
Instead, write correct C code and translate it with a C compiler, not
something else.

Best regards
 
F

Flash Gordon

(e-mail address removed) wrote:

Provide context, posts don't arrive in order and don't always arrive at
all. Check CBFalconers sig or any one of the hundreds of posts where
this has been explained and instructions given on how to work around
Googles stupidity.
In the function that is under test, explicit type casting is done. In
the example program I have not shown it.

Explicit casting is almost always wrong, especially casting from void*
(the return type of malloc) to some other pointer type. If you put in a
cast to get rid of a compiler warning without knowing *exactly* why the
compiler complained and *exactly* why a cast is needed, then that is a
good indication that all you are doing is shutting the compiler up and
creating an even bigger problem.
 
F

Flash Gordon

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

int main()
{
typedef union
{
int i;
char *s;
}union_t;

typedef struct
{
union_t union1;
int j;
}struct_t;

struct_t struct1;

struct1.union1.s = malloc(5);

Here you allocate 5 bytes.
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]='\0';

Here you write to the non-existent byte number 6.

1) strcpy has already written the null termination in to the
destination. That is part of it's job!
2) Indexing in C starts from 0, so if you allocate 5 elements those are
elements 0 to 4 and writing beyond that invokes undefined behaviour and
leads to strange things happening, like your computer exploding when you
try to show the program to your boss.
printf("%s\n",struct1.union1.s);
return 0;
}

I am getting the expected output(i.e. ASHW) for the above program.

That's unfortunate. If you had been lucky it would have visibly crashed
due to writing past the end of your buffer.
However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.

Since you have not shown us the code that actually exhibits the problem,
how do you expect us to properly diagnose it?

My *guess* is that either you have not included stdlib.h in your *real*
code or you are compiling it as C++. Nothing you've posted should cause
that kind of warning.
The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.

The question is, Can we allocate memory for a pointer declared inside a
union?

Of course.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

jat said:
the return type of malloc is (void *)
that's why ur testing tool may be giving an error.
u can explicitly cast it (char *) malloc(..) and then check out.
And since it returns a void* you don't need to cast it.

Infact, doing so may suppress compiler diagnostics.
(e.g. if you forget to include stdlib.h)
 
M

Martin Ambuhl

However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "

That's not useful. "Similar kinds" of functions might have any number
of things wrong with them. Post code that gives you a problem, not code
that is of a "similar kind."
The question is, Can we allocate memory for a pointer declared inside a
union?

Yes.
 
M

Martin Ambuhl

jat said:
the return type of malloc is (void *)
that's why ur testing tool may be giving an error.
u can explicitly cast it (char *) malloc(..) and then check out.

The above is very bad advice. Please ignore it.
 
M

Martin Ambuhl

In the function that is under test, explicit type casting is done.

This suggests that you may have another error you have not told us
about. There is no excuse for type casting the return from malloc().
Ignore ERT when he stupidly writes that there is: He hates C and thinks
all C programs should be badly-written C++ programs.
 
C

Christopher Benson-Manica

Martin Ambuhl said:
This suggests that you may have another error you have not told us
about. There is no excuse for type casting the return from malloc().
Ignore ERT when he stupidly writes that there is: He hates C and thinks
all C programs should be badly-written C++ programs.

I have ERT killfiled, but I have not seen any evidence of his
presence in this thread.
 
M

Martin Ambuhl

Christopher said:
I have ERT killfiled, but I have not seen any evidence of his
presence in this thread.

Not yet, but every thread I can remember in which casting the return
from malloc() ends up with ERT chiming in with his insistence that the
only good C program is a bad C++ program. My warning was meant to be a
prophylactic.
 

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,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top