struct X
{
int x,y;
char c;
};
main()
{
struct X x,*px;
px=&x;
printf("%d",*px);
}
I think the above piece of code printf produces a UB since it is expecting and we are providing it with a variable of type struct X.
i just need a confirmation on this so that i can post some more doubt on UB.
11 2268 since it is expecting a typo
actually i wanted to write since it is expecting an int.
Banfa 9,065
Recognized Expert Moderator Expert
I think you are correct because the compiler is free to pass parameters in pretty much anyway it chooses and I know that some compilers I have used use completely separate methods to pass basic types (like int) and compound types (such as structs). If the compiler was expecting an int and was passed a struct then it would look in the wrong place and quite possibly at an invalid memory location for the data.
donbock 2,426
Recognized Expert Top Contributor
Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (of if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.
A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer.
The function call in question is printf(). This function is something of a special case, its prototype only specifies the type of the first argument. All other arguments go through the default argument promotions. I couldn't find a clear statement of precisely what the default argument promotion is for pointers, but odds are it "erases" the distinction between a pointer-to-struct and a pointer-to-int.
Although ill-advised, I believe the code posted by the OP is not undefined behavior.
Tell me if these are UB or not
printf("%d",9.9);//expecting int iam passing float.
printf("%f",5);//expecting float iam passing int.
printf("%c",3.3);//expecting character iam passing float.
I think default arguement promotion will take place in the above statement.
Tell me whether default argument promotion will take place or not.If no then why??
Considering that printf has only two arguments and the one for your variable is ..., then how do you expect promoitions to take place?
The code for printf() is public domain. Look at the code yourself and answer your question.
The code for printf() is public domain.
Could not understand what did you mean?
That means the code for printf is probably on your computer right now as part of your compiler installation. When you make debug builds, come compilers have the coode for the C library avaiable so tio can be compiled with debugger info inserted.
The code varies by compiler.
Here is the code from Visual Studio.NET 2008: - /***
-
*printf.c - print formatted
-
*
-
* Copyright (c) Microsoft Corporation. All rights reserved.
-
*
-
*Purpose:
-
* defines printf() - print formatted data
-
*
-
*******************************************************************************/
-
-
#include <cruntime.h>
-
#include <stdio.h>
-
#include <dbgint.h>
-
#include <stdarg.h>
-
#include <file2.h>
-
#include <internal.h>
-
#include <mtdll.h>
-
#include <stddef.h>
-
#include <process.h>
-
-
/***
-
*int printf(format, ...) - print formatted data
-
*
-
*Purpose:
-
* Prints formatted data on stdout using the format string to
-
* format data and getting as many arguments as called for
-
* Uses temporary buffering to improve efficiency.
-
* _output does the real work here
-
*
-
*Entry:
-
* char *format - format string to control data format/number of arguments
-
* followed by list of arguments, number and type controlled by
-
* format string
-
*
-
*Exit:
-
* returns number of characters printed
-
*
-
*Exceptions:
-
*
-
*******************************************************************************/
-
-
int __cdecl printf (
-
const char *format,
-
...
-
)
-
/*
-
* stdout 'PRINT', 'F'ormatted
-
*/
-
{
-
va_list arglist;
-
int buffing;
-
int retval;
-
-
_VALIDATE_RETURN( (format != NULL), EINVAL, -1);
-
-
va_start(arglist, format);
-
-
_lock_str2(1, stdout);
-
__try {
-
-
buffing = _stbuf(stdout);
-
-
retval = _output_l(stdout,format,NULL,arglist);
-
-
_ftbuf(buffing, stdout);
-
-
}
-
__finally {
-
_unlock_str2(1, stdout);
-
}
-
-
return(retval);
-
}
-
-
int __cdecl _printf_l (
-
const char *format,
-
_locale_t plocinfo,
-
...
-
)
-
{
-
va_list arglist;
-
-
va_start(arglist, plocinfo);
-
-
return _vprintf_l(format, plocinfo, arglist);
-
}
-
-
-
int __cdecl _printf_s_l (
-
const char *format,
-
_locale_t plocinfo,
-
...
-
)
-
{
-
va_list arglist;
-
-
va_start(arglist, plocinfo);
-
-
return _vprintf_s_l(format, plocinfo, arglist);
-
}
-
-
int __cdecl printf_s (
-
const char *format,
-
...
-
)
-
{
-
va_list arglist;
-
-
va_start(arglist, format);
-
-
return _vprintf_s_l(format, NULL, arglist);
-
}
-
-
int __cdecl _printf_p_l (
-
const char *format,
-
_locale_t plocinfo,
-
...
-
)
-
{
-
va_list arglist;
-
-
va_start(arglist, plocinfo);
-
-
return _vprintf_p_l(format, plocinfo, arglist);
-
}
-
-
int __cdecl _printf_p (
-
const char *format,
-
...
-
)
-
{
-
va_list arglist;
-
-
va_start(arglist, format);
-
-
return _vprintf_p_l(format, NULL, arglist);
-
}
-
-
static UINT_PTR __enable_percent_n = 0;
-
-
/***
-
*int _set_printf_count_output(int)
-
*
-
*Purpose:
-
* Enables or disables %n format specifier for printf family functions
-
*
-
*Internals:
-
* __enable_percent_n is set to (__security_cookie|1) for security reasons;
-
* if set to a static value, an attacker could first modify __enable_percent_n
-
* and then provide a malicious %n specifier. The cookie is ORed with 1
-
* because a zero cookie is a possibility.
-
******************************************************************************/
-
int __cdecl _set_printf_count_output(int value)
-
{
-
int old = (__enable_percent_n == (__security_cookie | 1));
-
__enable_percent_n = (value ? (__security_cookie | 1) : 0);
-
return old;
-
}
-
-
/***
-
*int _get_printf_count_output()
-
*
-
*Purpose:
-
* Checks whether %n format specifier for printf family functions is enabled
-
******************************************************************************/
-
int __cdecl _get_printf_count_output()
-
{
-
return ( __enable_percent_n == (__security_cookie | 1));
-
}
donbock 2,426
Recognized Expert Top Contributor
The prototype for printf() is part of the C Standard, hence it should be readily available to you.
There are several open-source C compilers, gcc is the obvious example. You can obtain the source code for their implementation of printf.
On the other hand, that may not be necessary. The default argument conversions are controlled solely by the prototype and your invocation of the function. It is a safe bet that printf uses stdarg.h to obtain its parameters. Refer to the stdarg documentation to try and infer what happens for the type mismatches you propose.
What is prompting you to ask about these pathological cases?
Banfa 9,065
Recognized Expert Moderator Expert
I couldn't find a clear statement of precisely what the default argument promotion is for pointers, but odds are it "erases" the distinction between a pointer-to-struct and a pointer-to-int.
Examine the code more closely, it is not a pointer that is being passed but a structure. I know some compilers would put a pointer on the stack to a location allocated for the function call but there are several other options that compilers might use as well as actually putting the entire structure on the stack.
Whatever happens, undefined behaviour or not, I think this is generally a bad thing to do.
donbock
What is prompting you to ask about these pathological cases?
Well i know one is discouraged to use such non-sense in a code. But just for fun i tried to run the code and wanted to know what is exactly happening. So i thought of getting an expert advice on such cases.
Anyways thanx everyone for looking into my query.
donbock 2,426
Recognized Expert Top Contributor
Examine the code more closely, it is not a pointer that is being passed but a structure.
You are quite right. I missed the asterix (*) that dereferenced px. Definitely undefined behavior.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: fabio de francesco |
last post by:
Hi
what do you think of the following? Why are we permitted to do that?
And why the C++ Library doesn't stop someone willing to perfom that
assignement (*a = 20)?
#include <iostream>
...
|
by: Nitin Bhardwaj |
last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a
student and have been doing & studying C programming for the last 4
years).....and also a regular reader of "comp.lang.c"
I...
|
by: maadhuu |
last post by:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char * a= "bcd";
clrscr();
strcpy(a,"hello");
a = "fgh";
a = 't';
|
by: rahul8143 |
last post by:
hello,
1) First how following program get executed i mean how output is
printed and also why following program gives different output in Turbo
C++ compiler and Visual c++ 6 compiler?
void main()...
|
by: maadhuu |
last post by:
hello everybody,
i have 2 doubts .
1. is this always defined ??
int i =10;
int a = i++ + i++;
and also, i tried this in gcc, answer was 20, so what the sequence points
for evaluation of...
| |
by: lovecreatesbeauty |
last post by:
Hello experts,
I have seen following the code snippet given by Marc Boyer (with slight
changes by me for a better format), and have doubts on it. I am so
grateful if you can give me your kindly...
|
by: lovecreatesbeauty |
last post by:
1. In the following code, is the code (line 11) legal? Is there a
notice in the document to tell callers that the parameter s1 should
receive an array variable, i.e. type char, but not a variable...
|
by: temper3243 |
last post by:
Hi,
Many people have used this code in my project. It works because b is
using the extra memory for other 4 variables.I access obj max.
well here are a few questions
1) Where does it fail. I...
|
by: sophia.agnes |
last post by:
1)can any one give an example for a C program that can be written
using goto only ? i.e a program in which we cannot avoid the keyword
goto
2)what exactly is the purpose of tmpfile() ? will it...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |