malloc? I am lost again

Discussion in 'C Programming' started by Jean Pierre Daviau, Mar 11, 2005.

  1. I am a newbe not only in C but in programming. I am actually reading K&R.
    This is why I tried to run your application.

    It seems not to be running on my system. There is maybe a copy/paste
    mistake somewhere. As I know everybody on this newsgroup cares about
    portability I thought about writing this.


    From a previous thread
    ================

    /*
    * You clearly have a conceptual problem. Let's try to address it. The
    * text says that we should *assume* that each element of b points to an
    * array of 20 ints, not that the declaration makes it so. How might
    * this happen?
    */
    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    int *b[10];
    size_t i, j;

    for (i = 0; i < 10; i++)
    if (!(b = malloc(20 * sizeof *b))) {
    fprintf(stderr, "problem allocating b[%u]\n", (unsigned) i);
    for (j = 0; j < i; j++)
    free(b);
    }
    /* Each element of b now points to a region of memory whose contents
    is an array[20] of int. Indeed, each element of b points to an
    array[20] of int */

    for (i = 0; i < 10; i++)
    for (j = 0; j < 20; j++)
    b[j] = 100 * i + j;
    for (j = 0; j < 20; j++)
    for (i = 0; i < 10; i++)
    printf("%3d%c", b[j], (i == 9) ? '\n' : ' ');

    for (i = 0; i < 10; i++)
    free(b);
    return 0;
    }

    [output]
    0 100 200 300 400 500 600 700 800 900
    1 101 201 301 401 501 601 701 801 901
    2 102 202 302 402 502 602 702 802 902
    3 103 203 303 403 503 603 703 803 903
    4 104 204 304 404 504 604 704 804 904
    5 105 205 305 405 505 605 705 805 905
    6 106 206 306 406 506 606 706 806 906
    7 107 207 307 407 507 607 707 807 907
    8 108 208 308 408 508 608 708 808 908
    9 109 209 309 409 509 609 709 809 909
    10 110 210 310 410 510 610 710 810 910
    11 111 211 311 411 511 611 711 811 911
    12 112 212 312 412 512 612 712 812 912
    13 113 213 313 413 513 613 713 813 913
    14 114 214 314 414 514 614 714 814 914
    15 115 215 315 415 515 615 715 815 915
    16 116 216 316 416 516 616 716 816 916
    17 117 217 317 417 517 617 717 817 917
    18 118 218 318 418 518 618 718 818 918
    19 119 219 319 419 519 619 719 819 919

    Martin Ambuhl
    =============================
    Compiled with
    MicrosoftVisualC++
    Compiling...
    chap5_9.c
    Linking...

    chap5_9.exe - 0 error(s), 0 warning(s)
    debugging:
    First-chance exception in chap5.9.exe: 0xC0000005: Access Violation.
    The thread 0xF0 has exited with code -1073741510 (0xC000013A).

    ==========
    compiled with borland bcc32
    ---------- BCC32 ----------
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    chap5_9.c:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000
    running:
    problem allocating b[0]
    problem allocating b[1]
    problem allocating b[2]
    problem allocating b[3]
    problem allocating b[4]
    =======================

    C:/Dev-Cpp/include/stdio.h:213: warning: conflicting types for built-in
    function `snprintf'
    C:/Dev-Cpp/include/stdio.h:216: warning: conflicting types for built-in
    function `vsnprintf'
    In file included from C:/Dev-Cpp/include/stddef.h:6,
    from C:/Dev-Cpp/include/stdlib.h:22,
    from C:/Documents and Settings/Jean
    Pierre/Bureau/chap5_9.c:13:
    F:/Dev-Cpp/clib/mingwacr/include/stddef.h:17: error: redefinition of
    `size_t'
    F:/Dev-Cpp/clib/mingwacr/include/stddef.h:17: error: `size_t' previously
    declared here
    F:/Dev-Cpp/clib/mingwacr/include/stddef.h:21: error: redefinition of
    `wchar_t'
    F:/Dev-Cpp/clib/mingwacr/include/stddef.h:21: error: `wchar_t' previously
    declared here
    =============
    I tried this (int*) malloc(20 * sizeof *b))
    and (int*) malloc (sizeof (int) * 20))

    Jean Pierre Daviau
    --
    DEVC++, borland 5.5
    windows Xp
    asus p4 s533/333/133
    http://www.jeanpierredaviau.com

    --
    Jean Pierre Daviau
    --
    http://jeanpierredaviau.com
     
    Jean Pierre Daviau, Mar 11, 2005
    #1
    1. Advertising

  2. On Fri, 11 Mar 2005 09:38:57 -0500, in comp.lang.c , "Jean Pierre Daviau"
    <> wrote:

    >I am a newbe not only in C but in programming. I am actually reading K&R.
    >This is why I tried to run your application.
    >
    >It seems not to be running on my system. There is maybe a copy/paste
    >mistake somewhere.


    The code you posted works perfectly for me. Maybe you've mistyped something
    somewhere, or invoked your compiler incorrectly. On the other hand, these...

    >C:/Dev-Cpp/include/stdio.h:213: warning: conflicting types for built-in
    >function `snprintf'
    >C:/Dev-Cpp/include/stdio.h:216: warning: conflicting types for built-in
    >function `vsnprintf'
    >In file included from C:/Dev-Cpp/include/stddef.h:6,
    > from C:/Dev-Cpp/include/stdlib.h:22,
    > from C:/Documents and Settings/Jean
    >Pierre/Bureau/chap5_9.c:13:
    >F:/Dev-Cpp/clib/mingwacr/include/stddef.h:17: error: redefinition of
    >`size_t'
    >F:/Dev-Cpp/clib/mingwacr/include/stddef.h:17: error: `size_t' previously
    >declared here
    >F:/Dev-Cpp/clib/mingwacr/include/stddef.h:21: error: redefinition of
    >`wchar_t'
    >F:/Dev-Cpp/clib/mingwacr/include/stddef.h:21: error: `wchar_t' previously
    >declared here


    ..... errors you're getting indicate that your compiler is BADLY misinstalled. It
    looks to me like you're compiling with one compiler but using the headers from
    a different one. You cannot do that, each compiler MUST use its own standard
    headers and libraries.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Mar 11, 2005
    #2
    1. Advertising

  3. I took off everything. Rebooth etc.

    Reinstalled Borland commandlinetool
    bcc32.cfg
    -I";c:\Borland\Bcc55\Include;C:\Borland\BCC55\w32api-2.0\include;C:\jdk1.1.8\include\win32"
    -L".;c:\Borland\Bcc55\Lib;C:\Borland\BCC55\w32api-2.0\lib;C:\Borland\Bcc55\lib\PSDK;C:\jdk1.1.8\lib"
    ilink.cfg
    -L".;c:\Borland\Bcc55\Lib;C:\Borland\Bcc55\lib\PSDK;C:\Borland\BCC55\w32api-2.0\lib"

    compiled other applications: OK

    but the fprintf(stderr, "problem allocating b[%u]\n", (unsigned) i);
    still prints
    problem allocating b[0]
    problem allocating b[1]
    problem allocating b[2]
    problem allocating b[3]
    problem allocating b[4]
    debug

    =======

    I compiled it with djgpp (dos)

    problem allocating b[0]
    problem allocating b[1]
    problem allocating b[2]
    problem allocating b[3]
    problem allocating b[4]
    problem allocating b[5]
    problem allocating b[6]
    problem allocating b[7]
    problem allocating b[8]
    problem allocating b[9]
    900 900 900 900 900 900 900 900 900 900
    901 901 901 901 901 901 901 901 901 901
    902 902 902 902 902 902 902 902 902 902
    903 903 903 903 903 903 903 903 903 903
    904 904 904 904 904 904 904 904 904 904
    905 905 905 905 905 905 905 905 905 905
    906 906 906 906 906 906 906 906 906 906
    907 907 907 907 907 907 907 907 907 907
    908 908 908 908 908 908 908 908 908 908
    909 909 909 909 909 909 909 909 909 909
    910 910 910 910 910 910 910 910 910 910
    911 911 911 911 911 911 911 911 911 911
    912 912 912 912 912 912 912 912 912 912
    913 913 913 913 913 913 913 913 913 913
    914 914 914 914 914 914 914 914 914 914
    915 915 915 915 915 915 915 915 915 915
    916 916 916 916 916 916 916 916 916 916
    917 917 917 917 917 917 917 917 917 917
    918 918 918 918 918 918 918 918 918 918
    919 919 919 919 919 919 919 919 919 919
    ==========

    Why my beloved Borland dont compile?

    Jean Pierre
     
    Jean Pierre Daviau, Mar 11, 2005
    #3
  4. I found it!
    If I ignore the warning, it works.

    if (!(b = malloc(20 * sizeof *b))) {

    borland says:
    Warning W8060 Martin.c 13: Possibly incorrect assignment in function main

    I putted = =

    no complains
    crash

    Why that warning?

    Jean
     
    Jean Pierre Daviau, Mar 11, 2005
    #4
  5. Jean Pierre Daviau wrote on 11/03/05 :
    > I am a newbe not only in C but in programming. I am actually reading K&R.
    > This is why I tried to run your application.
    >
    > It seems not to be running on my system. There is maybe a copy/paste mistake
    > somewhere. As I know everybody on this newsgroup cares about portability I
    > thought about writing this.


    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > int *b[10];
    > size_t i, j;
    >
    > for (i = 0; i < 10; i++)
    > if (!(b = malloc(20 * sizeof *b))) {
    > fprintf(stderr, "problem allocating b[%u]\n", (unsigned) i);
    > for (j = 0; j < i; j++)
    > free(b);
    > }
    > /* Each element of b now points to a region of memory whose contents
    > is an array[20] of int. Indeed, each element of b points to an
    > array[20] of int */
    >
    > for (i = 0; i < 10; i++)
    > for (j = 0; j < 20; j++)
    > b[j] = 100 * i + j;
    > for (j = 0; j < 20; j++)
    > for (i = 0; i < 10; i++)
    > printf("%3d%c", b[j], (i == 9) ? '\n' : ' ');
    >
    > for (i = 0; i < 10; i++)
    > free(b);
    > return 0;
    > }


    Neat code. Works fine to me. (Dev-C++, BC++ 3.1)

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Mar 11, 2005
    #5
  6. Jean Pierre Daviau wrote on 11/03/05 :
    > I found it!
    > If I ignore the warning, it works.
    >
    > if (!(b = malloc(20 * sizeof *b))) {
    >
    > borland says:
    > Warning W8060 Martin.c 13: Possibly incorrect assignment in function main
    >
    > I putted = =


    No kidding ? You meant ==, of course...

    > no complains
    > crash
    >
    > Why that warning?


    This is due to an combined assignment and test. Usually, the parens
    around the assignment (like in M.A.'s code) stop the warning, but
    Borland seems to be very picky. Try that instead :

    if ((b = malloc(20 * sizeof *b)) != NULL) {

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Mal nommer les choses c'est ajouter du malheur au
    monde." -- Albert Camus.
     
    Emmanuel Delahaye, Mar 11, 2005
    #6
  7. Jean Pierre Daviau wrote:
    > I am a newbe not only in C but in programming. I am actually reading K&R.
    > This is why I tried to run your application.
    >
    > It seems not to be running on my system. There is maybe a copy/paste
    > mistake somewhere. As I know everybody on this newsgroup cares about
    > portability I thought about writing this.


    Among other things, in

    > for (i = 0; i < 10; i++)
    > if (!(b = malloc(20 * sizeof *b))) {
    > fprintf(stderr, "problem allocating b[%u]\n", (unsigned) i);
    > for (j = 0; j < i; j++)
    > free(b);
    > }
    > /* Each e


    I should have provided an exit from the program if the allocation failed.
     
    Martin Ambuhl, Mar 11, 2005
    #7
  8. Jean Pierre Daviau wrote:
    > I found it!
    > If I ignore the warning, it works.
    >
    > if (!(b = malloc(20 * sizeof *b))) {
    >
    > borland says:
    > Warning W8060 Martin.c 13: Possibly incorrect assignment in function main
    >
    > I putted = =


    You mean "" (a single token), not = = (two tokens)
    >
    > no complains
    > crash
    >
    > Why that warning?


    There is no excuse for the warning, but let me explain where it could
    come from.

    The line of code
    if (!(b = malloc(20 * sizeof *b))) {
    tests for the failure of malloc. Suppose instead that I had tested for
    the success of malloc. I might (but would not) write that as
    if (b = malloc(20 * sizeof *b)) {
    Now this has the form
    if (expr1 = expr2)
    but 'if' statements normally have a comparison (==) rather than
    assignments (=). A compiler might warn you of a common typing error:
    that you might have meant to type "==" and not "=". Most compilers
    would not emit such a warning if this were typed
    if ((b = malloc(20 * sizeof *b))) {
    since (b = malloc(20 * sizeof *b) is an expression and the form
    of the 'if' statement is no longer
    if (expr1 = expr2)
    but simply
    if (expr)
    But it is possible that a compiler is overzealous and despite the fact
    that it should know better still emits a warning. In that case, to be
    rid of the warning, we would need
    if ((b = malloc(20 * sizeof *b)) != NULL) {

    In the actual case, where I test for failure,
    if (!(b = malloc(20 * sizeof *b))) {
    the form is
    if (!expr)
    with no '=' visible at the level of the condition that 'if' tests, so
    even the helpful compiler should not emit a warning. However, the
    overzealous one might, for no good reason, emit the warning. To be rid
    of that warning, we would need
    if ((b = malloc(20 * sizeof *b)) == NULL) {
     
    Martin Ambuhl, Mar 11, 2005
    #8

  9. >> I putted = =

    >
    > No kidding ? You meant ==, of course...

    yes, but printed like that == looks like a wider assignment operator
     
    Jean Pierre Daviau, Mar 11, 2005
    #9
  10. There is a simple reason to that.
    It's not mine :)
     
    Jean Pierre Daviau, Mar 11, 2005
    #10
  11. Jean Pierre Daviau

    CBFalconer Guest

    Jean Pierre Daviau wrote:
    >
    > I took off everything. Rebooth etc.
    >
    > Reinstalled Borland commandlinetool bcc32.cfg
    >-I";c:\Borland\Bcc55\Include;C:\Borland\BCC55\w32api-2.0\include;C:\jdk1.1.8\include\win32"
    >-L".;c:\Borland\Bcc55\Lib;C:\Borland\BCC55\w32api-2.0\lib;C:\Borland\Bcc55\lib\PSDK;C:\jdk1.1.8\lib"
    > ilink.cfg
    > -L".;c:\Borland\Bcc55\Lib;C:\Borland\Bcc55\lib\PSDK;C:\Borland\BCC55\w32api-2.0\lib"
    >
    > compiled other applications: OK
    >
    > but the fprintf(stderr, "problem allocating b[%u]\n", (unsigned) i);
    > still prints
    > problem allocating b[0]


    Look to your environment. My guess is you have something set
    defining include and/or lib. You should be asking this in a
    Borland newsgroup.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Mar 11, 2005
    #11
  12. On Fri, 11 Mar 2005 14:00:26 -0500, in comp.lang.c , "Jean Pierre Daviau"
    <> wrote:

    >I found it!
    >If I ignore the warning, it works.
    >
    >if (!(b = malloc(20 * sizeof *b))) {
    >
    >borland says:
    >Warning W8060 Martin.c 13: Possibly incorrect assignment in function main
    >
    >I putted = =
    >
    >no complains
    >crash
    >
    >Why that warning?


    The warning is trying to be helpful and guard against mistakes like

    if (i = 2)
    when you meant compare i with two, rather than set it to two.

    In this case, the code was right, teh warning was wrong. You uncorrected the
    right code....

    Moral: DO NOT blindly correct 'mistakes' your compiler flags up, understand them
    first.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Mar 11, 2005
    #12
  13. Jean Pierre Daviau

    Default User Guest

    Jean Pierre Daviau wrote:
    > There is a simple reason to that.
    > It's not mine :)


    Please quote the relevant portion of the previous message when
    replying.

    Merci.




    Brian
     
    Default User, Mar 11, 2005
    #13
  14. > I should have provided an exit from the program if the allocation failed.

    very kind of you

    Jean
     
    Jean Pierre Daviau, Mar 11, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    716
  2. ravi
    Replies:
    0
    Views:
    461
  3. Peter
    Replies:
    34
    Views:
    1,993
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    487
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    333
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page