Thread: Question about malloc()

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Question about malloc()

    Hello.
    I'm new in this forum and it looks a nice site for me to get helped and maybe help others .


    I'm new on the C laguage and i want to know if i can do this with malloc:


    Allocating a memory for an int array of n*sizeof(int) and and changing the array size just with changing the value of n, just like that example:

    Code:
    void main(){
    int *array
    int n=5; //n is the size of the array.
    
    //allocate the memorie for the array
    array = (int *) malloc(n*sizeof(int));
    
    n++; //when i change n value does the size of the array will change also ?
    
    array[5]=7; //giving a value to the new array square without realloc() or other thing...
    
    }
    The compiler don't show any error and it works, but it seems to be not logic...

    Thank you for helping.
    Regards.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ALa Rihane
    when i change n value does the size of the array will change also ?
    No, not unless you also do a realloc or something similiar to "expand" the dynamic array.

    Quote Originally Posted by ALa Rihane
    The compiler don't show any error and it works, but it seems to be not logic...
    You are accessing the array out of bounds, resulting in undefined behaviour. It seems to work, but it might not.

    Other things to note:
    • #include <stdlib.h> for malloc.
    • void main should be int main
    • Remember your semi-colons.
    • Do not cast the return value of malloc unless you need the code to be compilable as C++.
    • free what you malloc.
    • Indent your code properly.
    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
    Join Date
    Feb 2012
    Posts
    2
    thank you for reply, but works because of compiler do some changes on the compilation ? or what ? because also when i do this code it work also:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include<conio.h>
    
    
    void main(){
        int tab[0];
        printf("sizeof tab %d\n",sizeof(tab));
        tab[5] = 6;
        tab[7] = 8;
        tab[6] = 7;
        printf("%d, %d, %d,",tab[5], tab[7], tab[6]);
        printf("\nsizeof tab %d\n",sizeof(tab));
        getch();
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ALa Rihane
    thank you for reply, but works because of compiler do some changes on the compilation ? or what ?
    Because you were "lucky". In real life, your program could appear to work until your boss demonstrates it to the client, shortly after which you will find yourself looking for a new job. Don't write code that accesses arrays out of bounds. Just don't.

    Quote Originally Posted by ALa Rihane
    because also when i do this code it work also:
    Zero sized arrays are not allowed in standard C. I compiled and ran your program, and the process crashed. Even if there was no crash, it is still wrong.
    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

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Zero sized arrays are not allowed in standard C.
    I don't have my copy of the C standard handy, but don't recall anything in the standard that actually disallows zero-sized arrays.

    Except the fact that accessing array elements out of bounds is always undefined. Even if creation of a zero-sized array is valid, any operation that accesses its elements will yield undefined behaviour. But "undefined behaviour" is a different concept than "not allowed".

    I also seem to recall that zero sized arrays are allowed by most implementations (albeit with a warning, if enabled) because some automatic code generators routinely produce such constructs (but do not subsequently access elements in an invalid way).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    C99 6.7.5.1, under "Constraints": "If the expression is a constant expression, it shall have a value greater than zero." So it's a constraint violation. The same holds for C89, with different wording (because C89 allowed only constant expressions).

    For VLAs, a size of 0 or smaller is undefined, because of course it cannot (generally) be detected at compile time if an object has the value 0.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Zero-sized arrays may be allowed on some compilers as a language extension, but it's certainly not standard or portable.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. question about malloc
    By bored_guy in forum C Programming
    Replies: 3
    Last Post: 10-27-2009, 06:53 AM
  3. malloc question..
    By transgalactic2 in forum C Programming
    Replies: 19
    Last Post: 04-15-2009, 04:13 PM
  4. malloc question
    By adrive in forum C Programming
    Replies: 11
    Last Post: 07-24-2008, 09:27 AM
  5. malloc question
    By Ian in forum Linux Programming
    Replies: 3
    Last Post: 10-13-2001, 01:31 AM