J
Jean Pierre Daviau
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
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