Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc? I am lost again

Reply
Thread Tools

malloc? I am lost again

 
 
Jean Pierre Daviau
Guest
Posts: n/a
 
      03-11-2005
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[i] = malloc(20 * sizeof *b[i]))) {
fprintf(stderr, "problem allocating b[%u]\n", (unsigned) i);
for (j = 0; j < i; j++)
free(b[i]);
}
/* 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[i][j] = 100 * i + j;
for (j = 0; j < 20; j++)
for (i = 0; i < 10; i++)
printf("%3d%c", b[i][j], (i == 9) ? '\n' : ' ');

for (i = 0; i < 10; i++)
free(b[i]);
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[i]))
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


 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      03-11-2005
On Fri, 11 Mar 2005 09:38:57 -0500, in comp.lang.c , "Jean Pierre Daviau"
<(E-Mail Removed)> 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>
 
Reply With Quote
 
 
 
 
Jean Pierre Daviau
Guest
Posts: n/a
 
      03-11-2005
I took off everything. Rebooth etc.

Reinstalled Borland commandlinetool
bcc32.cfg
-I";c:\Borland\Bcc55\Include;C:\Borland\BCC55\w32ap i-2.0\include;C:\jdk1.1.8\include\win32"
-L".;c:\Borland\Bcc55\Lib;C:\Borland\BCC55\w32ap i-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


 
Reply With Quote
 
Jean Pierre Daviau
Guest
Posts: n/a
 
      03-11-2005
I found it!
If I ignore the warning, it works.

if (!(b[i] = malloc(20 * sizeof *b[i]))) {

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

I putted = =

no complains
crash

Why that warning?

Jean


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      03-11-2005
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[i] = malloc(20 * sizeof *b[i]))) {
> fprintf(stderr, "problem allocating b[%u]\n", (unsigned) i);
> for (j = 0; j < i; j++)
> free(b[i]);
> }
> /* 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[i][j] = 100 * i + j;
> for (j = 0; j < 20; j++)
> for (i = 0; i < 10; i++)
> printf("%3d%c", b[i][j], (i == 9) ? '\n' : ' ');
>
> for (i = 0; i < 10; i++)
> free(b[i]);
> 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"

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      03-11-2005
Jean Pierre Daviau wrote on 11/03/05 :
> I found it!
> If I ignore the warning, it works.
>
> if (!(b[i] = malloc(20 * sizeof *b[i]))) {
>
> 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[i] = malloc(20 * sizeof *b[i])) != 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.

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-11-2005
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[i] = malloc(20 * sizeof *b[i]))) {
> fprintf(stderr, "problem allocating b[%u]\n", (unsigned) i);
> for (j = 0; j < i; j++)
> free(b[i]);
> }
> /* Each e


I should have provided an exit from the program if the allocation failed.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-11-2005
Jean Pierre Daviau wrote:
> I found it!
> If I ignore the warning, it works.
>
> if (!(b[i] = malloc(20 * sizeof *b[i]))) {
>
> 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[i] = malloc(20 * sizeof *b[i]))) {
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[i] = malloc(20 * sizeof *b[i])) {
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[i] = malloc(20 * sizeof *b[i]))) {
since (b[i] = malloc(20 * sizeof *b[i]) 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[i] = malloc(20 * sizeof *b[i])) != NULL) {

In the actual case, where I test for failure,
if (!(b[i] = malloc(20 * sizeof *b[i]))) {
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[i] = malloc(20 * sizeof *b[i])) == NULL) {




 
Reply With Quote
 
Jean Pierre Daviau
Guest
Posts: n/a
 
      03-11-2005

>> I putted = =

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

yes, but printed like that == looks like a wider assignment operator


 
Reply With Quote
 
Jean Pierre Daviau
Guest
Posts: n/a
 
      03-11-2005
There is a simple reason to that.
It's not mine


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
lost again Bill Cunningham C Programming 6 06-18-2009 07:15 AM
lost owner on boot up again thanks Jimmy Computer Support 7 12-07-2006 06:43 AM
...dammit!!...i lost it again... CheechWizard Computer Support 7 01-14-2006 04:09 PM
Method Scope (again b/c google groups lost it) Trans Ruby 2 07-28-2005 09:58 PM
Recovering lost files AGAIN! dperez@juno_nospam.com Digital Photography 8 02-14-2005 10:02 PM



Advertisments