C program with two results.


 
Thread Tools Search this Thread
Top Forums Programming C program with two results.
# 1  
Old 12-17-2001
Question C program with two results.

I have this program that I execute into UNIX UX and after into UNIX AIX. The program are just below:

#include <stdio.h>

struct A
{
double d;
char a[1];
};

struct B
{
char a[1];
double d;
};

void main()
{
struct A Va;
struct B Vb;

printf("%d", sizeof(Va));
printf("%d", sizeof(Vb));
}

I got the following results for this program:

HP UX
12
12

AIX
16
12


Why this diference ?

Last edited by Gandalfcgb; 12-17-2001 at 07:21 AM..
# 2  
Old 12-17-2001
The sizeof operator returns the number of bytes required to store an object of type of its operand. When applied to a structure or a union the result is not necessarilly the sum of the size of the members of the union. Padding is also required to make the object tile an array.
# 3  
Old 12-17-2001
Question Doubt.

Yes, sizeof return the bytes that the struct allocate in memory but how two struct's with the same description, the diference is only in the order of fields, can have diferent sizes?
How a double and a char[] can have a diferent sizeof a char[] and a double. That's my doubt.Smilie
# 4  
Old 12-17-2001
I have to agree that 16 looks like a screwy value. I suspect that the compiler erred as it allocated the structure. This error may be causing it to waste some space in memory. Perhaps you file a bug report to the AIX people.

But remember that the standards to not require a compiler to be efficient. Legal C code will work fine even if this is a bug in the compiler. And it's possible that there is some good reason for this behavior.
# 5  
Old 12-17-2001
It's not an error; the compliler just left some memory between the array and the double. You can check that there is not a problem if instead of array of chars of size 1, as you have in your programme, you put just a char, the size of the structs would be the same in both machines.
# 6  
Old 12-24-2001
I would say that this behavior is probably "normal".
The problem is often encountered with one of element alignment
within structures. Compilers may pad structures with "invisible"
elements to allow each "visible" element to align on a 2- or 4-
byte address boundary. This is done for efficiency in accessing
the element while in memory. Padding may also be added to the
end of the structure to bring it's total length to an even number
of bytes. This is done so the data following the structure in
memory will also align on a proper address boundary.
# 7  
Old 01-18-2002
The behaviour observed is very normal.
It's the system behaviour which will allocate memory to store the structure data.
While doing so, the system will compute the member having highest size and then accordingly will reallocate memory segments for other members of structure.

If you want to avoid this behaviour you can define a structure as a packed structure.
In that case system will allocate memory only as actually required and you can get same result on both the systems.

Also one interesting point to note is the order in which elements are defined in the structure also impact the o/p of sizeof operator
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies

2. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

3. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

4. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

5. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies

6. Shell Programming and Scripting

Calling C program from cron results in no output

I can call a C program from the shell and results are outputted as normal. The C program processes some files and spits out a .csv file. If I scheduled it in cron, there is no output. If their a special way to schedule C programs in cron? thanks & regards (1 Reply)
Discussion started by: hazno
1 Replies

7. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

8. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

9. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

10. Shell Programming and Scripting

ksh program run with different results by different users

Hi, I wrote a ksh program on Unix. One thing I don't understand: some users run it with different results. I suspect it's either "cat" or "grep" command. Basically, with one group of user, the 'cat' or 'grep' command is not getting the data I need and that changed the result. Is the above... (2 Replies)
Discussion started by: cin2000
2 Replies
Login or Register to Ask a Question