PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > mystery of calloc with #define
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
mystery of calloc with #define

Réponse
 
LinkBack Outils de la discussion
Vieux 29/11/2007, 17h50   #1
a
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut mystery of calloc with #define

By James Kuyper insightful suggestion, I identified my program error sources
from calloc.

I just don't know why for the following small program, I try different
JUDGEMENTDAY from 1,2,... and when it is >= 13, segmentation fault appears
if I don't force the program to exit by setting k to <= 11 in the last but 4
line. I guess the program is accessing something invalid but just don't know
why this happens.

#define SIZE 12
#define JUDGEMENTDAY 13

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv) {

int i=0;

int **chair_assg;
// int chair_assg[JUDGEMENTDAY+1][SIZE];
//int chair_assg[JUDGEMENTDAY][SIZE];
int t=0,k,kk,j,jj,mid,did;
double cost;

///*
i = JUDGEMENTDAY + 1;
printf("i: %d",i);
//return;
chair_assg = calloc(i,sizeof(int*));
if(chair_assg == NULL)
return;
//chair_assg = calloc((JUDGEMENTDAY),sizeof(int*));
for(i=0;i<SIZE;i++) {
chair_assg[i] = calloc(SIZE,sizeof(int));
}

for(k=0;k<JUDGEMENTDAY;k++) {
printf("k: %d", k);
for(i=0;i<SIZE;i++) {
chair_assg[k][i] = 0;
}
if (k==11) // <-------------- HERE
return;
}
return 0;
}


  Réponse avec citation
Vieux 29/11/2007, 18h09   #2
Richard Tobin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: mystery of calloc with #define

In article <fimu5g$h0t$1@justice.itsc.cuhk.edu.hk>, a <a@a.com> wrote:
>I just don't know why for the following small program, I try different
>JUDGEMENTDAY from 1,2,... and when it is >= 13, segmentation fault appears
>if I don't force the program to exit by setting k to <= 11 in the last but 4
>line. I guess the program is accessing something invalid but just don't know
>why this happens.


Here's your program with the commented-out bits removed, and indented
for readability:

#define SIZE 12
#define JUDGEMENTDAY 13

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv) {
int i=0;
int **chair_assg;
int t=0,k,kk,j,jj,mid,did;
double cost;

i = JUDGEMENTDAY + 1;
printf("i: %d",i);

chair_assg = calloc(i,sizeof(int*));
if(chair_assg == NULL)
return;

for(i=0;i<SIZE;i++) {
chair_assg[i] = calloc(SIZE,sizeof(int));
}

for(k=0;k<JUDGEMENTDAY;k++) {
printf("k: %d", k);
for(i=0;i<SIZE;i++) {
chair_assg[k][i] = 0;
}
}

return 0;
}

You seem to be confused about which dimension is SIZE and which is
JUDGEMENTDAY.

You use JUDGEMENTDAY+1 (=14) for the first dimension (rows) when
calloc()ing it (what was the +1 for?). Then you count up to SIZE (12)
when assigning calloc()ed space for each row. So you have only
allocated space for 12 of the 14 rows.

The you assign 0 to SIZE (12) columns in each of JUDGEMENTDAY (13)
rows - but only 12 of those rows have been allocated.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
  Réponse avec citation
Vieux 29/11/2007, 18h21   #3
Marco Manfredini
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: mystery of calloc with #define

a wrote:

> i = JUDGEMENTDAY + 1;
> chair_assg = calloc(i,sizeof(int*));


chair_assg has JUDGEMENTDAY+1 elements..

> for(i=0;i<SIZE;i++) {
> chair_assg[i] = calloc(SIZE,sizeof(int));


Here's the error: you assign the elements 0-(SIZE-1), but "SIZE" has
nothing to do with the number of elements in chair_assign. Actually the
for statements should be

for(i=0;i<JUDGEMENTDAY+1;i++) // The +1 is from your code.
{
chair_assg[i] = calloc(SIZE,sizeof(int));
if (chair_assg[i]==0) return 1;
}

You mixed up the array dimensions.

> for(k=0;k<JUDGEMENTDAY;k++) {
> printf("k: %d", k);
> for(i=0;i<SIZE;i++) {
> chair_assg[k][i] = 0;
> }
> if (k==11) // <-------------- HERE

This breaks for k==12 here, because chair_assg[12] has never been
assigned.

--
IYesNo yes=YesNoFactory.getFactoryInstance().YES;
yes.getDescription().equals(array[0].toUpperCase());
  Réponse avec citation
Vieux 29/11/2007, 23h33   #4
Dik T. Winter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: mystery of calloc with #define

In article <fimu5g$h0t$1@justice.itsc.cuhk.edu.hk> "a" <a@a.com> writes:
> I just don't know why for the following small program, I try different
> JUDGEMENTDAY from 1,2,... and when it is >= 13, segmentation fault appears
> if I don't force the program to exit by setting k to <= 11 in the last but 4
> line. I guess the program is accessing something invalid but just don't know
> why this happens.
>
> #define SIZE 12
> #define JUDGEMENTDAY 13

....
> for(i=0;i<SIZE;i++) {
> chair_assg[i] = calloc(SIZE,sizeof(int));


Read this piece of code, there is the error.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
  Réponse avec citation
Vieux 02/12/2007, 06h30   #5
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: mystery of calloc with #define

On Fri, 30 Nov 2007 01:50:38 +0800, "a" <a@a.com> wrote:

>By James Kuyper insightful suggestion, I identified my program error sources
>from calloc.
>
>I just don't know why for the following small program, I try different
>JUDGEMENTDAY from 1,2,... and when it is >= 13, segmentation fault appears
>if I don't force the program to exit by setting k to <= 11 in the last but 4
>line. I guess the program is accessing something invalid but just don't know
>why this happens.
>
>#define SIZE 12
>#define JUDGEMENTDAY 13
>
>#include <stdlib.h>
>#include <stdio.h>
>int main(int argc, char **argv) {
>
> int i=0;
>
> int **chair_assg;
>// int chair_assg[JUDGEMENTDAY+1][SIZE];
> //int chair_assg[JUDGEMENTDAY][SIZE];
> int t=0,k,kk,j,jj,mid,did;
> double cost;
>
> ///*
> i = JUDGEMENTDAY + 1;
> printf("i: %d",i);
> //return;
> chair_assg = calloc(i,sizeof(int*));
> if(chair_assg == NULL)
> return;


Didn't your compiler generate a diagnostic because you are returning
from main but not returning an int as promised by the declaration?

> //chair_assg = calloc((JUDGEMENTDAY),sizeof(int*));
> for(i=0;i<SIZE;i++) {
> chair_assg[i] = calloc(SIZE,sizeof(int));


Funny that you don't feel the need to check the return from calloc as
you did above.

> }
>
> for(k=0;k<JUDGEMENTDAY;k++) {
> printf("k: %d", k);


This should print k: 0k: 1k: 2. Unless your terminal uses paper,
adding a \n to your format string does not cost any more money.

> for(i=0;i<SIZE;i++) {
> chair_assg[k][i] = 0;


Why are you doing this since the space was already zeroed out by
calloc?

> }
> if (k==11) // <-------------- HERE


Others have explained how your inconsistent indices have resulted in
attempting to dereference a pointer whose value is all bits zero
(possibly a NULL pointer).

> return;
> }
> return 0;
> }
>



Remove del for email
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13867 seconds with 13 queries