first of all thank you so much for trying to help me, or at least have a look at my problem and maybe tell me, what I am doing wrong.
Ok, so I've recently encountered the problem that realloc() throws a segmentation fault error, when used to extend an array in a typedef struct, or at least so it does in my case.
I've tried to find the solution online, on this webpage and on other ones, but sadly I couldn't seem to find one. Which is why I really hope that one of you would be so kind to help me.
Thank you so much in advance,
Matthias
Btw. here I appended the code that is providing me errors.
typedef struct dict_int {
int* size;
char** keys;
int* values;
} dict_int;
This is my struct. And below is the function (add_int()), which is making problems... ;(
void add_int(dict_int* dictionary, int len, ...) {
va_list valist;
va_start(valist, len);
int di_size = dictionary->size[0];
for(int i = di_size-1; i < di_size-1+len; i++) {
dictionary->keys[i] = va_arg(valist, char*);
dictionary->values[i] = va_arg(valist, int);
printf("Change [Size]: %d -> ", dictionary->size[0]);
dictionary->size[0]++;
printf("%d\n", dictionary->size[0]);
if((dictionary->keys = (char**) realloc(dictionary->keys, dictionary->size[0] * 4 + 4))
!= NULL) {
printf("Worked out!");
} else
printf("Didn't work!");
if((dictionary->values = (int*) realloc(dictionary->values, dictionary->size[0] * 4 + 4))
!= NULL) {
printf("Worked out!");
} else
printf("Didn't work!");
}
va_end(valist);
}
And here's how I used it in the main() function:
int main(int argc, char* argv[]) {
printf("\n - !Program has started! - \n");
dict_int dictionary = Dict_int(2, "first", 0, "second", 1);
printf("\nDictionary-Size: %d\n", dictionary.size[0]);
printf("Dictionary: %s: %d\n", dictionary.keys[0], dictionary.values[0]);
printf("Dictionary: %s: %d\n", dictionary.keys[1], dictionary.values[1]);
printf("Dictionary: first: %d\n", get_int(&dictionary, "first"));
add_int(&dictionary, 1, "third", 2);
add_int(&dictionary, 1, "uno", 1);
/* printf("uno: %d\n", get_int(&dictionary, "uno"));
printf("third: %d\n", get_int(&dictionary, "third"));
printf("\n - !Program has finished! - \n");*/
return 0;
}
Also, the code works out alright, if I only call add_int() once, so if I only were to add one element to my dict_int it wouldn't throw an error, which is why I'm guessing that it has something to do with the memory...
Also, I tried to remove all the pieces of add_int() that included a realloc(), in order to prove my theory, and without realloc() it seems to work just fine...
So, just know that I'm very grateful to you, for just reading trough this question, and I'd be even more grateful, if you were so kind, and tried to tell me what I'm doing wrong, since sadly, I can't figure out. - Matthias
Related
This is my first time posting so bear with me! I have to make a quicksort algorithm as an assignment for university, using c. However, when I try running the program a pop-up window opens up and informs me that the exe has stopped working. So then I started debugging and I found that the problem appears when the program enters the recursion process, as I receive around those lines a SIGSEGV type error. Unfortunately I cant find the problem so that's why I'm posting here my code, hoping that you can help me. Any further advice about my code in general will be appreciated. Thanks!
#include <stdio.h>
#include <string.h>
#define SIZE 20
void quicksort(int *first, int *last);
main()
{
FILE *fp;
int *ptr, arr[SIZE], k;
ptr=arr;
fp=fopen("numbers.txt", "r");
printf("Initial array before sorting:\n");
for (k=0; k<SIZE; k++)
{
fscanf(fp, "%d", ptr);
printf("Element[%d]:\t%d\n", k+1, *ptr);
ptr++;
}
fclose(fp);
printf("Final array after sorting:\n");
quicksort(arr, arr+SIZE-1);
for (k=0; k<SIZE; k++)
{
printf("Element[%d]:\t%d\n", k+1, *(arr+k));
}
}
void quicksort(int *first, int *last)
{
int *item_left, *item_right, *i, *j, *pivot, temp, check_left=0, check_right=0, limiter=0;
pivot=last;
while (!check_left && !check_right && limiter<10)
{
for (i=first; i<last; i++)
{
if (*i>*pivot)
{
item_left=i;
check_left=1;
break;
}
}
for (j=last; j>first; j--)
{
if (*j<*pivot)
{
item_right=j;
check_right=1;
break;
}
}
if (check_left==1 && check_right==1)
{
temp=*item_left;
*item_left=*item_right;
*item_right=temp;
check_left=0;
check_right=0;
}
limiter++;
}
temp=*item_right;
*item_right=*pivot;
*pivot=temp;
if (last-first>1)//-------problem
{
quicksort(first, item_left-1);
quicksort(item_left, last);
}//----------problem
}
So I checked the code again and I found the problem. You see, I declared some local variables in the function I was calling. This was causing trouble in the computer's memory, as every time the function was called again, it was told to create the same variables which were already created and stored in the memory. So what I did is to make all those variables needed global. This way they are created only one time.
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 2 years ago.
I'm not sure if I'm missing something and that might make this question very stupid. But I can't understand why the hell it's failing after trying pretty much every approach one could have.
So, super simple, I have this GLib Tree and I want to insert stuff into it in other functions. Why none of the options presented below work? I can understand the first failing more than the second to be completely honest.
int compare_ints(gconstpointer gpa, gconstpointer gpb){
int a = *((int*) gpa);
int b = *((int*) gpb);
return (a-b);
}
void test1(GTree* tree){
int code = 1234;
gpointer gcp = &code;
g_tree_insert(tree, gcp, gcp);
printf("%d\n", (g_tree_lookup(tree, gcp) == NULL)); // Outputs 0 (obviously)
}
void test2(GTree** tree){
int code = 1234;
gpointer gcp = &code;
g_tree_insert(*tree, gcp, gcp);
printf("%d\n", (g_tree_lookup(*tree, gcp) == NULL)); // Outputs 0 (obviously)
}
int main(int argc, char** argv){
GTree* tree = g_tree_new(compare_ints);
int code = 1234;
gpointer gcp = &code;
test1(tree);
printf("%d\n", (g_tree_lookup(tree, gcp) == NULL)); // Outputs 1 (Why?)
test2(&tree);
printf("%d\n", (g_tree_lookup(tree, gcp) == NULL)); // Outputs 1 (Why?)
return 0;
}
Sorry about it if it's a stupid question, any help at all apreciated :)
Edit: Removed vim line notation
As #UnholySheep mentioned in the comments of the main thread, doing the following would work:
void test1(GTree* tree, int* code){
g_tree_insert(tree, code, code);
printf("%d\n", (g_tree_lookup(tree, code) == NULL));
}
void test2(GTree** tree, int* code){
g_tree_insert(*tree, code, code);
printf("%d\n", (g_tree_lookup(*tree, code) == NULL));
}
int main(int argc, char** argv){
Catalog* c = init_catalog(26, 1, compare_ints);
int* code = malloc(sizeof(int));
*code = 1234;
GTree* tree = g_tree_new(compare_ints);
test1(tree, code);
printf("%d\n", (g_tree_lookup(tree, code) == NULL));
test2(&tree, code);
printf("%d\n", (g_tree_lookup(tree, code) == NULL));
destroy_catalog(c);
free(code);
return 0;
}
The reason this works is because code only dissapears when you free it!
On the initial case in the end of the function the int would stop existing and that would justify the behavior. If your interested in reading more about it check out the link that UnholySheep mentioned in the comments of the main thread!
Stuck at if block right below //step 5, the issue is that the code will not progress into or after the given if block. I need to figure out how to get this particular issue settled before starting the task of generating parallel code. If you run the code you will see one print statement that indicates the value of "one" and another two for "i" and "j". After the if block begins, none of the other print statements are hit. As a result I am quite stuck, I am aware that this is a specific issue, however, I cannot seem to determine it's cause.
Any help is appreciated!
Thanks in advance!
Input file sample.
>386.fasta.screen.Contig1
GAGTTTGATCCTGGCTCAGAATCAACGCTGGCGGCGCGCTTAACACATGC
AAGTCGAACGAGAAAGTGGAGCAATCCATGAGTACAGTGGCGTACGGGTG
AGTAACACGTGGGTAATCTACCTCTTAGTGGGGAATAACTTTGGGAAACC
GAAGCTAATACCGCATAAGCTCGAGAGAGGAAAGCAGCAATGCGCTGAGA
GAGGAGCCCGCGGCCGATTAGCTAGTTGGCAGGGTAAAAGCCTACCAAGG
CAGAGATCGGTAGCCGGCCTGAGAGGGCACACGGCCACACTGGCACTGAA
ACACGGGCCAGACTCCTACGGGAGGCAGCAGTGGGGAATCTTGCACAATG
GGGGCAACCCTGATGCAGCGACGCCGCGTGAGCGATGAAGCCCTTCGGGG
TGTAAAGCTCTTTCGTCAGGGAAGATAGTGACGGTACCTGGAGAAGCAGC
TGCGGCTAACTACGTGCCAGCAGCCGCGGTAATACGTAGGCAGCGAGCGT
TGTTCGGAGTTACTGGGCGTAAAGGGTGTGTAGGCGGTTGTTTAAGTTTG
GTGTGAAATCTCCCGGCTCAACTGGGAGGGTGCGCCGAATACTGAGCGAC
TAGAGTGCGGGAGAGGAAAGTGGAATTCCTGGTGTAGCGGTGAAATGCGT
AGATATCAGGAGGAACACCGGTGGTGTAGACGGCTTTCTGGACCGTAACT
GACGCTGAGACACGAAAGCGTGGGTAGCAAACAGGATTAGATACCCTGGT
AGTCCACGCCCTAAACGATGCATATTTGGTGTGGGCAGTTCATTCTGTCC
GTGCCGGAGCTAACGCGTTAAATATGCCGCCTGGGGAGTACAGTCGCAAG
GCTGAAACTCAAAGGAATTGACGGGGGCCCGCACAAGCGGTGGAGCATGT
GGTTTAATTCGACGCAACGCGAAGAACCTTACCTGGGCTCGAACGGCTTC
CCAACGCCGGTAGAAATATCGGTACCCCGCAAGGGGGTGGAATCGAGGTG
CTGCATGGCTGTCGTCAGCTCGTGTCGTGAGATGTTGGGTTAAGTCCCGC
AACGAGCGCAACCCTTGTCCTGTGTTGCCATGCCGCAAGGCGGCACTCGC
AGGAGACCGCCAGCGATAAGCTGGAGGAAGGTGGGGATGACGTCAAGTCC
TCATGGCCTTTATGTCCAGGGCTACACACGTGCTACAATGGCCGGTACAA
AGCGTCGCTAACCTGCGAAGGGGAGCCAATCGCAAAAAACCGGTCTCAGT
TCGGATTGCAGGCTGCAACCCGCCTGCATGAAGCTGGAATCGCTAGTAAT
GGCAGATCAGCACGCTGCCGTGAATACGTTCCCGGGCCTTGTACACACAT
/********************************
Based on code by:
Lorenzo Seidenari (sixmoney#virgilio.it)
*********************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#define MAX_SEQUENCE_LENGTH 100000
int n;
int m;
int levenshtein_distance(char *s,char*t);
int minimum(int a,int b,int c);
//-----------------------------------------------------------------------------
void cleanString(char string[]) {
//Removes all spaces from string pointed to by "string", converts characters
//to uppercase, and deletes a terminating newline character.
int i, current;
int length = strlen(string);
current = 0;
for(i=0;i<length;i++) {
if(string[i]=='\n') {
string[current++] = '\0';
break;
}
else if(string[i]!=' ') {
string[current++] = toupper(string[i]);
}
}
}
//-----------------------------------------------------------------------------
int importFASTA(char *filename, char *sequence) {
//Reads a file, located at path specified by "filename", containing a FASTA
//sequence. It finds the first full, complete sequence in the file, stores
//it in "sequence", and returns the length of the sequence, or -1 on failure.
FILE *fastaFile;
char input[256];
int readFlag; //set to 1 once a sequence has been read in
int length;
//open the file
if((fastaFile = fopen(filename, "r")) == NULL) {
return -1;
}
sequence[0] = '\0';
//read the full first sequence, discarding unnecessary headers
readFlag=0;
length = 0;
while(fgets(input,256,fastaFile)!=NULL) {
//is it a header or a comment?
if(input[0]=='>' || input[0]==';') {
if(readFlag) break;
else continue;
}
else readFlag = 1;
cleanString(input);
length += strlen(input);
strncat(sequence,input,MAX_SEQUENCE_LENGTH-length - 1);
}
//Add a terminatng null character, just in case
sequence[length] = '\0';
fclose(fastaFile);
return length;
}
/****************************************/
/*Implementation of Levenshtein distance*/
/****************************************/
__global__ void levenshtein_distance(char *s,char*t, int one, int two)
/*Compute levenshtein distance between s and t*/
{
//Step 1
int k,i,j,cost,*d;
int distance = 0;
if(one!=0&&two!=0)
{
d=(int *)malloc((sizeof(int))*(two+1)*(one+1));
two++;
one++;
//Step 2
for(k=0;k<one;k++){
d[k]=k;
}
for(k=0;k<two;k++){
d[k*one]=k;
}
//Step 3 and 4
for(i=1;i<one;i++){
for(j=1;j<two;j++)
{
//Step 5
printf("%d %d %d\n", one, i, j);
if(s[i-1]==t[j-1]){
cost=0;
printf("%d %d %d\n", one, i, j);
}
else{
cost=1;
printf("%d %d %d\n", one, i, j);
}
printf("%d %d %d\n", one, i, j);
//Step 6
int min = d[(j-1)*one+i]+1;
if (d[j*one+i-1]+1 < min)
min = d[j*one+i-1]+1;
if (d[(j-1)*one+i-1]+cost < min)
min = d[(j-1)*one+i-1]+cost;
d[j*one+i] = min;
}
distance=d[one*two-1];
free(d);
printf("%d\n", distance);
}
}
else
printf ("-1");
}
int main(int argc, char *argv[]) {
char A[MAX_SEQUENCE_LENGTH+1];
char B[MAX_SEQUENCE_LENGTH+1];
if(argc < 3) {
printf("Usage: new_edit_distance <sequence1> <sequence2>\n");
printf("<sequence1>: file containing the first sequence, FASTA format\n");
printf("<sequence2>: file containing the second sequence, FASTA format\n");
return EXIT_FAILURE;
}
n = importFASTA(argv[1],A);
m = importFASTA(argv[2],B);
levenshtein_distance<<<1, 1>>>(A,B, n, m);
cudaDeviceSynchronize();
printf ("%s\n", cudaGetErrorString(cudaGetLastError()));
return EXIT_SUCCESS;
}
I get it now. You took straight serial C/C++ code, dropped it into a kernel, intended to run that kernel as a single thread, and then want to proceed from there.
The idea is plausible, but you're missing a key fact about CUDA and GPUs: they can't directly access host memory.
So when you set up A and B like this:
char A[MAX_SEQUENCE_LENGTH+1];
char B[MAX_SEQUENCE_LENGTH+1];
....
n = importFASTA(argv[1],A);
m = importFASTA(argv[2],B);
those are ordinary variables that live in host memory. GPU (ordinary CUDA) code can't directly access host memory. So when you pass those pointers to a kernel like this:
levenshtein_distance<<<1, 1>>>(A,B, n, m);
the GPU code will try and dereference those A and B pointers and will fault (unspecified launch failure).
Every CUDA program has the following basic sequence:
copy data to the GPU
perform computations on the GPU
copy results back
You've tried to do step 2 without step 1. It won't work.
Since I'm not able to run your program since I don't have valid input files, I'll make the following suggestion. I assume you know little or nothing about CUDA. Try adding lines like this:
n = importFASTA(argv[1],A); // no change
m = importFASTA(argv[2],B); // no change
char *d_A, *d_B; // add this line
cudaMalloc(&d_A, MAX_SEQUENCE_LENGTH+1); // add this line
cudaMalloc(&d_B, MAX_SEQUENCE_LENGTH+1); // add this line
cudaMemcpy(d_A, A, MAX_SEQUENCE_LENGTH+1, cudaMemcpyHostToDevice); // add
cudaMemcpy(d_B, B, MAX_SEQUENCE_LENGTH+1, cudaMemcpyHostToDevice); // add
levenshtein_distance<<<1, 1>>>(d_A,d_B, n, m); //modify parameters
n and m don't need to be handled any differently since you are passing those by value.
And add proper cuda error checking to your code.
EDIT: after some further analysis, it's clear that this sequence is not correct:
distance=d[one*two-1];
free(d);
printf("%d\n", distance);
}
}
You are freeing d on every iteration of the i loop. That cannot possibly be correct. I suggest you go back to square one and get your serial code working first, in ordinary serial C code, before dropping it into a cuda kernel this way. If you move that free statement outside the i loop, then your kernel runs for a very very long time. Be advised that in-kernel printf is limited in the amount of output that can be easily generated.
I'm not going to debug your code any further for you. Get your serial code working first, then figure out a way to create a kernel without massive quantities of printout.
A final comment: I said above your approach is "plausible". That it means it could be made to work, i.e produce the same behavior as the same code executing on the host. It does not mean it will run fast. This is not how you get acceleration out of a GPU (running a single block of a single thread). I assume you already know this based on your comment "how to get this particular issue settled before starting the task of generating parallel code." But I think the disclaimer is appropriate anyway.
I am trying (and having trouble) to write a program (In C) that accepts a string in the command line (eg. $ test.out "This is a string") and looks through the string to find verbs (and nouns, but if I figure out verbs, I can do nouns on my own).
A list of aplphabetically sorted verbs is given in the file lexicon.h, and is what I am supposed to use as my dictionary.
I know how to accept the string from the command line and use that input to create an array of strings, each string itself being a separate word, and I already have a working program that can do that, and that I hope to use part of for this one.
I am supposed to create a function called binary_search(...stuffgoeshere...) and use that to search through the lexicon file and find the verb.
I would like some suggestions or guidance on how to create a function (binary_search) that can check to see if an already separated word matches any on the list in lexicon.h. I do not want someone to just write an answer, I would like to know why you are suggesting what you do. Hopefully I can learn something fun out of this!
I know it's messy, but this is what I have so far.
Also note that lexicon's verb array has 637 values (as seen when I make int size = 637)
This program does not compile anymore, as I have not yet figured out how to make the binary_search function work yet. I am trying to modify a binary search function used in an example for class, however, that one sorted numbers in a text file, not strings of characters.
If there is anything else I should include, let me know. Thank you for your help!
#include <stdio.h>
#include <string.h>
#include "lexicon.h"
int binary_search(char word[], char verbs[][], int size);
int
main(int argc, char*argv[])
{
char word[80];
char str[80],
args[80][80];
int counter = 0,
a = 0,
i = 0,
index = 0,
t = 0;
while(str[a] != '\0')
{
if(str[a] == ' ')
{
args[index][i] = '\0';
i = 0;
a++;
index ++;
counter ++;
}
args[index][i++] = str[a++];
}
args[index][i] = '\0';
counter = counter + 1;
printf("\nThe verbs were: ");
int verbposition= -1;
int size = 637;
while(t<counter)
{
strcpy(word, args[t]);
verbposition = binary_search(word, verbs, size);
if(verbposition > -1)
printf("%s", args[t]);
t++;
}
return 0;
}
int
binary_search(char word[], char &verbs[][], int size)
{
int bottom = 0,
top = size - 1,
found = 0,
middle;
while(bottom <= top && !found)
{
middle = (bottom + top) / 2;
if(strcmp(word, verbs[middle]))
{
found = 1;
return = middle;
}
if(strcmp(word, verbs[middle]) > 0)
{
top = middle - 1;
}
else
bottom = middle + 1;
}
return -1;
}
You are on the right track. I would highly suggest you to use print statements as you will have a clear idea of where you are going wrong.
I am having some problems when returning with printf of a char array from a struct in C.
struct q_entry
{
long mtype;
char mtext[MAXLENGTH + 1];
};
The long mtype from the struct is returning fine, but the string is just returning some weird characters.
int proc_obj(struct q_entry *msg)
{
printf("\npriority: %ld name: %s\n", msg->mtype, msg->mtext);
}
It just returns some strange characters like "priority: 1 name: ▒▒(" and not "priority: 1 name: hello"
I am populating the struct using the following code
int enter(char *objname, int priority)
{
...
strncpy(s_entry.mtext, objname, sizeof(s_entry.mtext) - 1);
s_entry.mtype = priority;
// Send the message
if (msgsnd(s_qid, &s_entry, len, 0) == -1)
{
printf("error: msgsnd failed\n");
return(-1);
}
else
{
return(0);
}
}
I don't have much experience with C, so I don't know too much about using structs. Please let me know if more context or parts of the code is needed. Any kind of help would be very helpful.
I have added a little more code in enter above, and here is more code of the when enter and proc_obj are called
main(int argc, char **argv)
{
int priority;
if (argc != 3)
{
printf("error: incorrect number of arguments\n");
exit(1);
}
else
{
priority = atoi(argv[2]);
}
if (enter(argv[1], priority) < 0)
{
printf("error: message entering failed\n");
exit(1);
}
exit(0);
}
This is in a different file from enter and above code
int server(void)
{
int mlen, r_qid;
struct q_entry r_entry;
// Initialize queue
if ((r_qid = init_queue()) == -1)
return(-1);
for (;;)
{
if ((mlen = msgrcv(r_qid, &r_entry, MAXLENGTH, (-1 * MAXPRIOR), MSG_NOERROR)) == -1)
{
printf("error: msgrcv failed\n");
exit(1);
}
else
{
proc_obj(&r_entry);
}
}
}
The only obvious error in your code is that you should explicitly fill in a zero at s_entry.mtext[MAXLENGTH] such that the string will still be zero terminated if strncpy() hits the limit. But if that were the problem, you would see "hello" followed by strange characters. Are you sure that objname points to the text you're expecting it to point to?
Also, it looks a bit strange that proc_obj() is declared to return an int but actually does not return anything. Your compiler ought to complain about that.
Answer before adding more code
It looks like the s_entry structure object is local, and enter works on the local variable. How are you calling enter and returning the structure after you have done initializing it ? note that you have int as the return type of the enter function. If you are doing return s_entry; then the output you are getting is possible, as only the first word of the structure, ie the lower sizeof (int) part of mtype is considered.
If you are using enter function like this as i described above then make the return type of enter to struct s_entry
You should check the size of len when sending the message.
You don't show the message queue call but my guess is you are somehow miscalling the API and putting garbage into the queue (and then printing garbage in the server).