...

View Full Version : Not familiair with C but need some help



chrisvmarle
11-12-2002, 12:01 PM
I have a script and if I compile it I get an error with this message:
"Segmentation fault (core dumped)"
Can someone telll me what this means, or what to do about it?

Mzzl, Chris

Bosko
11-12-2002, 01:39 PM
Script?You mean application.
How about showing us some code??

chrisvmarle
11-12-2002, 02:15 PM
#include <stdio.h>
#include <stdlib.h>

void grabData(int *dat);
int processData(int *dat);

/* This is where it all kicks off */
int main() {
/* Array to store the file data */
int elements[30];

/* Pointer to element array */
int *data;

/* Result */
int result = 0;

/* Initialise the element array */
int integer;
memset(elements, 0, (sizeof(integer) * 30));

*data = elements[0];

printf("Simple data processing script\n");
printf("by slyfx [www.slyfx.com]\n\n");

/* Grab the data from the file */
/* grabData(data);*/

/* Process the data and grab the result */
result = processData(data);

printf("The result of the processing is %s\n", result);

return(0);
}

/* Opens the data file and read it in */
void grabData(int *dat) {
int loop;
FILE *fp;

fp = fopen("data.sly", "r");
if (fp == NULL) {
perror("Failed to open file for reading!\n");
exit(1);
}

/* Grab the lines */
for(loop = 0; loop < 30; loop++) {
if (fscanf(fp, "%d", dat[loop]) == EOF) {
perror("Fatal: not enough data!\n");
exit(1);
}
}

*(dat + 1) = 20;

fclose(fp);
}

/* Process the data */
int processData(int *dat) {
int loop;
int res = 0;

for(loop = 0; loop < 30; loop++) {
res += (dat[loop] ? (dat[loop] - 50) : 69) ? dat[loop] : 69;
while (res > 300000) {
res -= dat[loop] ? dat[loop] : 169;
}
}

return res;
}

maes
11-12-2002, 06:32 PM
As far as I know, the memset function doesn't allocate memory, it only fills the memorywith a certain value.
I don't really understand whet you're doing with the elements array. but if you want to initialize the data variable you can do it directly with memset (after you allocated memory for it)
also, if you want to output an integer, use %d or %i , not %s that's for strings

to be honest, I never used memse, so I could be wrong



int main() {
/* Array to store the file data */
int elements[30];

/* Pointer to element array */
int *data;

/* Result */
int result = 0;

/* Initialise the element array */
int integer;

/*allocate memory*/
data=(int *)malloc(sizeof(int) * 30);

/*fill the memory with 0*/
memset(data,0,30);
/*not needed now*/
//*data = elements[0];
printf("Simple data processing script\n");
printf("by slyfx [www.slyfx.com]\n\n");

/* Grab the data from the file */
/* grabData(data);*/

/* Process the data and grab the result */
result = processData(data);

/*result is an integer so you should use
printf("The result of the processing is %i\n", result);
/*free the memory*/
free(data);
return 0;

}

Wichetael
11-20-2002, 04:11 PM
the problem's not the memset, memset as you said simply sets a chunk of memory to a certain value and doesn't allocate any memory, but the memory is already allocated in the array so it isn't necessary to do it again.

There are two problems though, the first problem is in this line:

*data = elements[0];
I'm guessing you're trying to set data to point to the first element in the array, though you make a little mistake here, what you wrote in fact assigns the value of the first element to the location currently pointed to by data, and since data is undefined at this point this could be anywhere in the memory. Most likely that would be outside the segment of your program and that would generate a segmentation fault. Change your line in

data = elements
You can also do it like this:

data = &elements[0] but that is unnecessary since an array variable is nothing but a pointer to the first element in the array.

Though furthermore, you only use the variable data for passing the value of it to the grabData and processData function, instead of passing data you can simply directly pass elements like so:

grabData(elements);

The second problem is in the fscanf call, the third argument should be a pointer to the memory location, but you're passing it by value, this the function will try to access a part of memory which is most likely has no access rights to. You should either replace dat[loop] by dat + loop or by &dat[loop]

Though you have another few weird lines in your code...

In this line:

int integer;
You allocate and int named integer simlpy for the sake of using the size of it, that's not needed, you can simply do away with the integer variable and use sizeof(int) or better yet sizeof(element[0]) that way you don't have to change the type in two places if you decide to change the type of the array.

In this line:

*(dat + 1) = 20;
you set the second value of the array to 20, you could simply use dat[1] = 20; but more than likely it serves no purpose and you could leave it out...

I'd suggest you to go over you code very carefully again, there are likely to be some very sneaky pitfalls in there and you could probably be searching for hours to find them later on. It's best to make sure right away that your code is doing exactly what you're expecting it to do.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum