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;
} 4 1634
In article <fi**********@justice.itsc.cuhk.edu.hk>, a <a@a.comwrote:
>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.
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());
In article <fi**********@justice.itsc.cuhk.edu.hk"a" <a@a.comwrites:
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/
On Fri, 30 Nov 2007 01:50:38 +0800, "a" <a@a.comwrote:
>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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: M-One |
last post by:
See subject: how do I calloc (and free the memory, if that's not
free(my_bytes);) this?
TIA!
|
by: Koster |
last post by:
Sorry for the re-post, but my previous question was left unanswered.
I have a question about the appropriateness of calloc. Consider an array of
pointers to structs which need to be allocated...
|
by: David Hill |
last post by:
Is there a difference between:
/* code 1 */
struct sample test;
test = malloc(sizeof(struct sample));
memset(&test, 0, sizeof(test));
/* code 2 */
struct sample test;
test = calloc(1,...
|
by: laberth |
last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why?
Here is what I use :
typedef struct noeud {
int val;
struct noeud *fgauche;
struct noeud *fdroit;
} *arbre; //for those...
|
by: Mark Shelor |
last post by:
I've encountered a troublesome inconsistency in the C-language Perl
extension I've written for CPAN (Digest::SHA). The problem involves the
use of a static array within a performance-critical...
|
by: Harsimran |
last post by:
Can any one explain what are far pointers and what is the difference
between malloc and calloc .Which is better ?
|
by: boris |
last post by:
Hi!
I'm seeking some answers about what seems to be a memory leak.
I have a loop that looks much like this:
double *largeArray = (double*) calloc();
for (...) {
printf("iteration #...\n");...
|
by: lohith.matad |
last post by:
Hi all,
Though the purpose of both malloc() and calloc() is the same, and as we
also know that calloc() initializes the alloacted locations to 'zero',
and also that malloc() is used for bytes...
|
by: Roka100 |
last post by:
Hi all,
I tried 2 programs :
#include <stdio.h>
#include <string.h>
1,
int main(void){
char *str = NULL;
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |