Thread: What's wrong with the following assignment?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    What's wrong with the following assignment?

    Code:
    int main()
    {
    	int *x;
    	
    	x= malloc(sizeof(char));
    	
    	*x=500000000;
    	printf("\n%d, %d, %d, size-char=%d, size-int=%d\n",*x, x, x+1, sizeof(char), sizeof(int));
    
    }
    malloc should allocate only 1 byte of memory and hence we shouldnt be able to store an integer in it..but my program works..how?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by arunikgp
    malloc should allocate only 1 byte of memory and hence we shouldnt be able to store an integer in it..but my program works..how?
    Undefined behaviour. You are probably overwriting adjacent memory, and may have to pay for your crimes when it is least convenient.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by arunikgp View Post
    malloc should allocate only 1 byte of memory and hence we shouldnt be able to store an integer in it..but my program works..how?
    C doesn't have any built-in bounds-checking facilities. For better or for worse, it just assumes you know what you're doing, even when it's blatantly obvious that you don't.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by arunikgp View Post
    Code:
    int main()
    {
    	int *x;
    	
    	x= malloc(sizeof(char));
    	
    	*x=500000000;
    	printf("\n%d, %d, %d, size-char=%d, size-int=%d\n",*x, x, x+1, sizeof(char), sizeof(int));
    
    }
    malloc should allocate only 1 byte of memory and hence we shouldnt be able to store an integer in it..but my program works..how?
    Pure dumb luck... As laserlight points out you are playing with undefined behaviour. The only reason it appears to work is that an int is 4 bytes, your number will fit in 4 bytes, memory alligns to int boundaries and there are no other variables defined. If you did something like this...
    Code:
    int main (void)
      {
        char a;
        unsigned int b = 0xFFFFFFFF; 
    
        char a = "This is a test";
    
        printf ("%X",b);
        return 0; 
    }
    It is very likely you would see that the value of b was changed by what you put at the address allocated to a.

    As gardhr points out C has no facilities for bounds checking --and in fact has almost no run time checking of any kind-- that is up to you as a programmer...
    But, the good news is that you now know this and can govern yourself accordingly.

  5. #5
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by CommonTater View Post
    It is very likely you would see that the value of b was changed by what you put at the address allocated to a.
    In that case, the address of the literal would simply be truncated before being copied into a single byte value. No overflow possible there, so no harm done...

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by arunikgp View Post
    Code:
    int main()
    {
    	int *x;
    	
    	x= malloc(sizeof(char));
    	
    	*x=500000000;
    	printf("\n%d, %d, %d, size-char=%d, size-int=%d\n",*x, x, x+1, sizeof(char), sizeof(int));
    
    }
    malloc should allocate only 1 byte of memory and hence we shouldnt be able to store an integer in it..but my program works..how?
    Quote Originally Posted by CommonTater View Post
    Pure dumb luck... As laserlight points out you are playing with undefined behaviour. The only reason it appears to work is that an int is 4 bytes, your number will fit in 4 bytes, memory alligns to int boundaries and there are no other variables defined. If you did something like this...
    Code:
    int main (void)
      {
        char a;
        unsigned int b = 0xFFFFFFFF; 
    
        char a = "This is a test";
    
        printf ("%X",b);
        return 0; 
    }
    An additional note is to take a look at how Tater defined main and the return value he gave. This is how to properly write a C program.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by AndrewHunter View Post
    An additional note is to take a look at how Tater defined main and the return value he gave. This is how to properly write a C program.
    Aren't these two exactly the same for function definitions?
    Code:
    int main()
    int main(void)
    I was taught that the empty parameter list = unknown/any arguments only applies to declarations.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    This is a big argument for C; per the standard both are currently acceptable however only for main(). For any other function an empty parameter list means that the function could take an unknown number of arguments (for the declarations as you pointed out). You could consider the main definition for parameters as a personal preference.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by _Mike
    Aren't these two exactly the same for function definitions?
    Other than spelling, yes. The bad part is that the former is regarded in the grammar as part of the old identifier list syntax, so it is technically obsolescent although due to adoption by C++ I doubt it will ever be removed from C.

    Quote Originally Posted by AndrewHunter
    This is a big argument for C; per the standard both are currently acceptable however only for main(). For any other function an empty parameter list means that the function could take an unknown number of arguments (for the declarations as you pointed out).
    There is no special case for main here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    @Andrew: Thank you. I wasn't sure if I remembered it correctly and your post made me a bit more unsure.

    And speaking of the standard.. I was trying to find where this behavior was mentioned and I found some sections which seem to be conflicting. (But I'm pretty sure they actually aren't, and I'm just misunderstanding them)
    Quote Originally Posted by C99 6.7.5.3.14
    An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.
    But then we have
    Quote Originally Posted by C99 6.7.5.3.3
    An identifier list in a function declarator that is not part of a definition of that function shall be empty.
    So now a function declaration has to have an empty identifier list? Doesn't that imply that all functions where the declaration isn't part of the definition should be able to accept a variable number of arguments?

    @Laserlight:
    You posted while I was writing this so I didn't see it until now. Since you mentioned "the old identifier list syntax" (emphasis mine) I guess that identifier lists are not the argument lists I think they are?
    Last edited by _Mike; 09-02-2011 at 02:22 AM.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by laserlight View Post
    Other than spelling, yes. The bad part is that the former is regarded in the grammar as part of the old identifier list syntax, so it is technically obsolescent although due to adoption by C++ I doubt it will ever be removed from C.
    I don't think this is necessarily a "bad part". <Enter style argument..>

    Quote Originally Posted by laserlight View Post
    There is no special case for main here.
    So where do you declare main before you define it? We had this argument before and you, yourself have decided that just sticking with the "old way" was the best. I like you Laser but let's be consistent
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by _Mike
    So now a function declaration has to have an empty identifier list?
    Hmm... looks like it. This compiles perfectly fine for me:
    Code:
    #include <stdio.h>
    
    void foo();
    
    int main(void)
    {
        foo(1, 2.5);
        return 0;
    }
    
    void foo(x, y)
    int x; double y;
    {
        printf("%d %f\n", x, y);
    }
    But when I change the declaration to:
    Code:
    void foo(x, y);
    gcc 4.4.4 warns me that it detected "parameter names (without types) in function declaration". Of course, if you provide the types it then becomes a parameter list instead of an identifier list.

    Quote Originally Posted by AndrewHunter
    I don't think this is necessarily a "bad part".
    It is, because it means that for practical reasons the identifier list form cannot be removed from C without making yet another exception to the rule.

    Quote Originally Posted by AndrewHunter
    So where do you declare main before you define it?
    As far as I can tell, a declaration of main that is not a definition is allowed, so if you wanted to do that you could. That said, my statement was with reference to your claim that "both are currently acceptable however only for main()". In other words, you claim that for functions other than main, only one form is acceptable.

    Quote Originally Posted by AndrewHunter
    We had this argument before and you, yourself have decided that just sticking with the "old way" was the best. I like you Laser but let's be consistent
    I have not made any suggestion of which form to choose. I like you, AndrewHunter, but let's be honest.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by laserlight View Post
    gcc 4.4.4 warns me that it detected "parameter names (without types) in function declaration". Of course, if you provide the types it then becomes a parameter list instead of an identifier list.
    I see, so that was the main source of my confusion. I thought identifier lists and parameter lists were the same thing.
    I had actually never seen the identifier list version of declarations before now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  4. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  5. Assignment
    By cYou in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2003, 07:57 AM