Segmentation fault printf - slam

When I build rgb lsd_slam
I search about segmentation fault, and I know something about it. But when I debug by stepping through each line, I get the result following:
for (float* pt = data.imageRGB[0]; pt < maxPtRGB; pt++) {
*pt = *rgbImage;
printf("3!\n");
rgbImage++;
}
for(float* pt = data.imageRGB[0]; pt < maxPtRGB; pt++) {
*pt = *rgbImage;
rgbImage++;
}
If you see the two above, the first run without the problem, and the second will show the segmentation fault. I am using Ubuntu 16.04.

Related

Segmentation fault - weird debugging with gdb

I'm working in C, Linux terminal. I need to find a pattern in a text and recolor it. GDB debugging can locate the function that is causing the problem via (gdb) backtrace, but it shows me a terrible message when I try to find the exact line:
Error
Program received signal SIGSEGV, Segmentation fault.
strstr_sse2_unaligned ()
at ../sysdeps/x86_64/multiarch/strstr-sse2-unaligned.S:40
40 ../sysdeps/x86_64/multiarch/strstr-sse2-unaligned.S: No such file or dir
ectory.
(gbd)
The broken function is find_and_recolor:
char* my_replace(char *text, char* replacement)
{
int lgreplacement = strlen(replacement);
int lgtext = strlen(text);
char *aux = (char*)malloc((lgreplacement + lgtext + 10) * sizeof(char));
strcpy(aux, replacement);
strcat(aux, text);
return(aux);
}
char* find_and_recolor(char* text, char* pattern)
{
int lgpattern = strlen(pattern);
int lgreplace = lgpattern + 10;//there are exactly 10 characters that must be inserted along the pattern word
int dif = 0;
char *p;
char *replacement = (char*)malloc(lgreplace * sizeof(char));
strcpy(replacement, "\e[0;31m");
strcat(replacement, pattern);
strcat(replacement, "\e[m");//to recolor a word in red, that word must be surrounded by those characters
while(p = strstr(text + dif, pattern))
{
p = my_replace(p, replacement);
p += lgreplace;
dif = p - text;
}
free(replacement);
return strdup(text);
}
it shows me a terrible message when I try to find the exact line:
There is nothing terrible, weird or unusual about this message, you just need to learn proper debugging technique.
What's happening is that the segmentation fault doesn't happen in your code, it happens inside GLIBC code (inside strstr), because you called strstr with bad arguments.
To find which call to strstr that was, use GDB up command to step out of GLIBC code, and into your code. Once you are inside find_and_recolor, you would be able to see the exact line, and print values of text, dif and pattern which caused your crash (assuming you compiled your code for debugging, i.e. with the -g flag).
Updating diff to p-text in while loop where both pointer points to different array doesn't make sense. It is undefined behavior.
Also code has other issues.
Uninitialized variable.
Less optimized as number of call can be reduced.

Seg Fault:I can't understand why

I get a pretty weird segmentation fault error when I am trying to use the same function in two different places.
printTVNode function work fine on main.
On Main:
printTVNode(headTVNode); /* Works fine here */
TVNodePointer headTopic = NULL;
TopicEmmissions(&headTopic,headTVNode,desiredTopic);
When I am trying to use printTVNode inside TopicEmmissions function a get Seg Fault.
void TopicEmmissions(TVNodePointer * headTopic,TVNodePointer headTVNode,char * desiredTopic){
TVNodePointer currentTVNode = headTVNode;
EmmissionPointer currentEmmission;
EventPointer currentEvent;
EventPointer topicSubjects = NULL;
int flag,countEvent = 1,countEmmission = 1;
printTVNode(headTVNode); /* Get Segmentation Fault here*/
...
printTVNode function:
void printTVNode(TVNodePointer headTVNode){
TVNodePointer currentTVNode = headTVNode;
while ( currentTVNode != NULL ){
printEmmission(*(currentTVNode->anEmmission));
currentTVNode = currentTVNode->next;
}
}
The problem seems to be in the following line :
printEmmission(*(currentTVNode->anEmmission));
In a situation where anEmmission is NULL, when you try to dereference it, I think you will get a segfault.
Make sure to check that anEmmission is not NULL before doing dereferencing.

I have a segmentation fault in c

I have a segmentation fault in the following code, but do not understand why. This is part of a larger program, where fsi is a double and is calculated immediately preceeding this block of code. The program calculates fsi over a number of years (lt) and I want to print the output as an array (fsi.dat). It prints the first value but then seg faults. What am I missing?
Here is the code:
FILE *fpout;
int lt;
double silicate[lt];
fpout = fopen("fsi.dat","w");
if(fpout == NULL)
ferrx("writedat(): Can't open file to write: fsi.dat");
for(i=1;i<=lt;i++)
silicate[i] = fsi;
fprintf(fpout,"%18.15f \n", silicate[i]);
fclose(fpout);
You need to put brackets in order to execute more than one statement in a for
for(i=1;i<=lt;i++) {
silicate[i] = fsi;
fprintf(fpout,"%18.15f \n", silicate[i]);
}
The version you have is equivalent to
for(i=1;i<=lt;i++) {
silicate[i] = fsi;
}
fprintf(fpout,"%18.15f \n", silicate[i]);
the last line executing with i = lt + 1
for(i=1;i<=lt;i++)
silicate[i] = fsi;
is wrong, indexes in C are zero-based. So your valid indexes are 0 .. (lt-1)

2-3 trees segmentation fault on search/deletion

I'm trying to implement 2-3 trees and I get a segmentation fault when searching for the node to be deleted. Here's the code:
p = root;
while (p!=NULL || p->k1!=kkey || p->k2!=kkey)
{
if (kkey < p->k1)
p = p->st;
else if (kkey > p->k1 && kkey < p->k2)
p = p->mid;
else
p = p->dr;
}
So I'm trying to search for the node that contains the kkey. The debugger tells me I have a Segmentation fault in that while where I'm trying to check whether the key was found or we are still in the tree.
Your while condition looks highly suspect.
Let's say p is NULL; the while will still try to dereference p->k1 and will segfault.
Did you mean to say && ("and") instead of || ("or")?

Segmentation fault on Ubuntu while running fine on Windows, programmed in c

I am writing a small oscillatory motion program that would run on Ubuntu and Windows.
After completing part of the program (major part) I tried to test it on windows, works fine (working with Pelles C)
Then, i copied my data on the Ubunutu computer, Running on a virtual Machine (VMware Workstation).
i compiled it fine using GCC and crashes with "Segmentation fault (core dumped)" error.
Output before crashing:
Simulation Starting...
Creating Containers.....
Done!
Initializing Containers.....
Initializing Container
Done!...
Initializing Container
Done!...
Initializing Container
Segmentation fault (core dumped)
--------------------------------
Part of zDA.c, the function required to initialize vectors before using in simulation
int InitializeArray (DATA *item) {
printf("\nInitializing Container");
item->num_allocated = 0;
item->num_elements = 0;
item->the_array = NULL;
printf("\nDone!...");
if (!item->the_array) {
return -1;
}
return 0;
}
While calling the function, snippet from zSim.c
int Simulate(SIMOPT so,PRINTOPT po) {
printf("\nSimulation Starting...\nCreating Containers...");
//Create Data Objects
//vectors
DATA Theta,Omega,T;
DATA *pTheta = Θ
DATA *pOmega = Ω
DATA *pT = &T;
//Initial Values
int method = so.method;
float g = so.g;
float l = so.l;
float itheta = so.theta;
float iomega = so.omega;
float dt = so.dt;
float df = so.df;
float dw = so.dw;
float q = so.q;
float maxtime = so.maxtime;
//backend variables
float i = 0; //Simulation Counter
int k=0; //Counter to Count array size;
int kmax = 0;
float th,thi,om,omi,t,ti; //Simulation variables
int gt,go,pl,mat;
printf("..");
printf("\nDone!\nInitializing Containers...");
printf("..");
//Initialize Containers
InitializeArray(pTheta);
InitializeArray(pOmega);
InitializeArray(pT); //**FOR SOME REASON, it stops working here -_-
printf("DONE! NIT"); //It worked fine on windows, there are no dependencies.
th = pTheta->the_array[0];
om = pOmega->the_array[0];
t = pT->the_array[0];
i don't get why it worked on windows, didn't on ubuntu, either the compiler on Pelles fixed something for me, or my virtual machine is going crazy,
i mean... it already Initialized 2 out of 3 arrays, what's wrong with the third :)"?
The initialization looks like it would not crash. But the code immediately following it looks suspect. The array has not been initialized (well ... it is initialized, but it is initialized to NULL), yet the following code accesses it:
th = pTheta->the_array[0];

Resources