473,503 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

some doubts on undefined behaviour

34 New Member
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.
Dec 11 '09 #1
11 2268
destination
34 New Member
since it is expecting a typo
actually i wanted to write since it is expecting an int.
Dec 11 '09 #2
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.
Dec 11 '09 #3
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.
Dec 11 '09 #4
destination
34 New Member
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??
Dec 11 '09 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Dec 11 '09 #6
destination
34 New Member
The code for printf() is public domain.
Could not understand what did you mean?
Dec 11 '09 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:

Expand|Select|Wrap|Line Numbers
  1. /***
  2. *printf.c - print formatted
  3. *
  4. *       Copyright (c) Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines printf() - print formatted data
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <dbgint.h>
  14. #include <stdarg.h>
  15. #include <file2.h>
  16. #include <internal.h>
  17. #include <mtdll.h>
  18. #include <stddef.h>
  19. #include <process.h>
  20.  
  21. /***
  22. *int printf(format, ...) - print formatted data
  23. *
  24. *Purpose:
  25. *       Prints formatted data on stdout using the format string to
  26. *       format data and getting as many arguments as called for
  27. *       Uses temporary buffering to improve efficiency.
  28. *       _output does the real work here
  29. *
  30. *Entry:
  31. *       char *format - format string to control data format/number of arguments
  32. *       followed by list of arguments, number and type controlled by
  33. *       format string
  34. *
  35. *Exit:
  36. *       returns number of characters printed
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. int __cdecl printf (
  43.         const char *format,
  44.         ...
  45.         )
  46. /*
  47.  * stdout 'PRINT', 'F'ormatted
  48.  */
  49. {
  50.         va_list arglist;
  51.         int buffing;
  52.         int retval;
  53.  
  54.         _VALIDATE_RETURN( (format != NULL), EINVAL, -1);
  55.  
  56.         va_start(arglist, format);
  57.  
  58.         _lock_str2(1, stdout);
  59.         __try {
  60.  
  61.         buffing = _stbuf(stdout);
  62.  
  63.         retval = _output_l(stdout,format,NULL,arglist);
  64.  
  65.         _ftbuf(buffing, stdout);
  66.  
  67.         }
  68.         __finally {
  69.             _unlock_str2(1, stdout);
  70.         }
  71.  
  72.         return(retval);
  73. }
  74.  
  75. int __cdecl _printf_l (
  76.         const char *format,
  77.         _locale_t plocinfo,
  78.         ...
  79.         )
  80. {
  81.     va_list arglist;
  82.  
  83.     va_start(arglist, plocinfo);
  84.  
  85.     return _vprintf_l(format, plocinfo, arglist);
  86. }
  87.  
  88.  
  89. int __cdecl _printf_s_l (
  90.         const char *format,
  91.         _locale_t plocinfo,
  92.         ...
  93.         )
  94. {
  95.     va_list arglist;
  96.  
  97.     va_start(arglist, plocinfo);
  98.  
  99.     return _vprintf_s_l(format, plocinfo, arglist);
  100. }
  101.  
  102. int __cdecl printf_s (
  103.         const char *format,
  104.         ...
  105.         )
  106. {
  107.     va_list arglist;
  108.  
  109.     va_start(arglist, format);
  110.  
  111.     return _vprintf_s_l(format, NULL, arglist);
  112. }
  113.  
  114. int __cdecl _printf_p_l (
  115.         const char *format,
  116.         _locale_t plocinfo,
  117.         ...
  118.         )
  119. {
  120.     va_list arglist;
  121.  
  122.     va_start(arglist, plocinfo);
  123.  
  124.     return _vprintf_p_l(format, plocinfo, arglist);
  125. }
  126.  
  127. int __cdecl _printf_p (
  128.         const char *format,
  129.         ...
  130.         )
  131. {
  132.     va_list arglist;
  133.  
  134.     va_start(arglist, format);
  135.  
  136.     return _vprintf_p_l(format, NULL, arglist);
  137. }
  138.  
  139. static UINT_PTR __enable_percent_n = 0;
  140.  
  141. /***
  142.  *int _set_printf_count_output(int)
  143.  *
  144.  *Purpose:
  145.  *   Enables or disables %n format specifier for printf family functions
  146.  *
  147.  *Internals:
  148.  *   __enable_percent_n is set to (__security_cookie|1) for security reasons;
  149.  *   if set to a static value, an attacker could first modify __enable_percent_n
  150.  *   and then provide a malicious %n specifier.  The cookie is ORed with 1
  151.  *   because a zero cookie is a possibility.
  152.  ******************************************************************************/
  153. int __cdecl _set_printf_count_output(int value)
  154. {
  155.     int old = (__enable_percent_n == (__security_cookie | 1));
  156.     __enable_percent_n = (value ? (__security_cookie | 1) : 0);
  157.     return old;
  158. }
  159.  
  160. /***
  161.  *int _get_printf_count_output()
  162.  *
  163.  *Purpose:
  164.  *   Checks whether %n format specifier for printf family functions is enabled
  165.  ******************************************************************************/
  166. int __cdecl _get_printf_count_output()
  167. {
  168.     return ( __enable_percent_n == (__security_cookie | 1));
  169. }
Dec 11 '09 #8
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?
Dec 11 '09 #9
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.
Dec 11 '09 #10
destination
34 New Member
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.
Dec 11 '09 #11
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.
Dec 11 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

10
1871
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> ...
25
3054
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...
22
1582
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';
70
2733
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()...
13
1912
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...
8
1766
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...
17
3028
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...
24
1799
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...
28
1645
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...
0
7074
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...
0
7273
Oralloy
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,...
0
7322
jinu1996
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...
1
6982
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...
1
5000
isladogs
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...
0
4667
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...
0
3161
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...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.