473,503 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc? I am lost again

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
Nov 14 '05 #1
13 2278
On Fri, 11 Mar 2005 09:38:57 -0500, in comp.lang.c , "Jean Pierre Daviau"
<On**@WasEno.ugh> 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>
Nov 14 '05 #2
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
Nov 14 '05 #3
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"

Nov 14 '05 #4
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.

Nov 14 '05 #5
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
Nov 14 '05 #6
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.
Nov 14 '05 #7
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) {


Nov 14 '05 #8
I putted = =


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

yes, but printed like that == looks like a wider assignment operator
Nov 14 '05 #9
There is a simple reason to that.
It's not mine :-)
Nov 14 '05 #10
Jean Pierre Daviau wrote:

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]


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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #11
On Fri, 11 Mar 2005 14:00:26 -0500, in comp.lang.c , "Jean Pierre Daviau"
<On**@WasEno.ugh> 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 = =

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>
Nov 14 '05 #12

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

Nov 14 '05 #13
> I should have provided an exit from the program if the allocation failed.

very kind of you

Jean
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
3447
by: Hal Styli | last post by:
Hello, Can someone please help. I need to use a large array and I'm not sure when one should use (A) and when one should use (B) below:- #define MAX 10000000 (A) int x;
7
2336
by: Fatted | last post by:
I'm trying to learn how to create arrays dynamically. But its just not happening. Have a look at code below and point and laugh where appropriate... First part of program, I'm using an array of...
36
2229
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program...
24
362
by: Hrv'uljak | last post by:
Anybody has a better solution? How to avoid memory allocation in main function? Thanks! -------- #include <stdio.h> #include <conio.h> #include <string.h> #include <malloc.h>
17
2404
by: Johs32 | last post by:
When I make a pointer I have read that if I would like to use it in another function, I need to malloc it first else it will disapear after the function returns. In this code I do not use malloc,...
71
19035
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
5
3091
by: caicai | last post by:
Recently I'm doing a project on microcontroller, and I want use queue structure, but I found that phisical ram isn't enough for malloc() func, I hope to find a way using queue without malloc(), is...
11
1820
by: Jason | last post by:
I've inherited some old C code that I've been tasked to convert to C++. I've been running into some problems with memory allocation. First of all, is there any problems using "malloc" in C++? I...
14
3002
by: karthikbalaguru | last post by:
Hi, In the case of heap , to keep track of a single chunk of memory it requires 8 bytes of information. That is, it requires 4 bytes to hold the size, and 4 bytes to hold the pointer to the next...
0
7093
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
7287
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,...
1
7008
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...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5022
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
4688
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
3177
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
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.