0

I'm getting a segmentation fault when assigning the value stored at a pointer to a local variable in a function. I have a hunch that I'm handling pointers poorly, but I can't seem to figure out what the issue is. Any ideas? Thanks!

I've tried adding breakpoints, and I've determine that the actual seg fault happens at "int i = *AlignCounter;"

When looking at the variables window, I see this: Variables Window in VSC Debugger

Here's the function where the seg fault happens:

void align_S_T(int *StringCounterS, int 
*StringCounterT, int *AlignCounter, char *StringS, char 
*StringT, char *AlignmentS, char *AlignmentT)
{
//Local Variables
int i = *AlignCounter; //<--Seg Fault Line
int s = *StringCounterS;
int t = *StringCounterT;
//Set Strings
AlignmentS[i] = StringS[s];
AlignmentT[i] = StringT[t];

//Increment Counters
*AlignCounter++;
*StringCounterS++;
*StringCounterT++;

}

Here's how the pointers are created and allocated in main:

    int* StringCounterT; //T String Counter
    int* AlignCounter; //Align Counter

    StringCounterS=0;
    StringCounterT=0;
    AlignCounter=0;
5
  • 1
    What are the values of giving the parameter AlignCounter when you use the align_S_T function? Commented Mar 31, 2019 at 2:39
  • Do you think you are allowed to access the memory location at 0x0 (at the very base of the System Reserved memory range)? What do you think is stored there? int i = *AlignCounter; //<--Seg Fault Line is a good indication of the answer to the first question. In other words AlignCounter=0; stores the Address 0 as the value for AlignCounter and when you attempt to access the memory location you have no right to access, BAM SegFault. Commented Mar 31, 2019 at 2:40
  • 1
    before use the align_S_T function did you allocated dynamic memory to AlignCounter? Commented Mar 31, 2019 at 2:43
  • @Miguel, no I just declared the pointer. Commented Mar 31, 2019 at 2:45
  • What do you think happens when you try to dereference a pointer pointing to nowhere then? Commented Mar 31, 2019 at 3:10

3 Answers 3

1

You are indicating that AlignCounter points to address 0 (AlignCounter=0;), then, inside align_S_T() you are trying to get the value stored inside this adress with "*" operator(int i = *AlignCounter;). You cannt do this because it is not a valid address (try to point AlignCounter to a declared integer before calling align_S_T(), the segFault goes to next line, rss, same thing).

void align_S_T(int *StringCounterS, int 
*StringCounterT, int *AlignCounter, char *StringS, char 
*StringT, char *AlignmentS, char *AlignmentT)
{
//Local Variables
int i = *AlignCounter;
int s = *StringCounterS;  //<--Seg Fault Line!!
int t = *StringCounterT;
//Set Strings
AlignmentS[i] = StringS[s];
AlignmentT[i] = StringT[t];

//Increment Counters
*AlignCounter++;
*StringCounterS++;
*StringCounterT++;

}

int main(int argc, char *argv[]){
     int* StringCounterT; //T String Counter
     int* StringCounterS; //T String Counter
     int* AlignCounter; //Align Counter
     char *a, *b, *c, *d;
     int x;

     a = b = c = d = "";
     x = 10;

     StringCounterS=0;
     StringCounterT=0;
     AlignCounter=&x;  

     align_S_T(StringCounterS, StringCounterT, AlignCounter, a, b, c, d);
     return(0);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I have isolated your problem, you need to assign dynamic memory or static to AlignCounter variable before calling the function align_S_T as you can see in the following code.

#include <stdio.h>
#include <stdlib.h>

void  align_S_T(int *AlignCounter)
{
   int i = *AlignCounter;
   *AlignCounter++;
}

int main()
{
   // Dynamic memory
   int *AlignCounter=NULL;
   AlignCounter = (int*) malloc(10 * sizeof(int)); 
   *AlignCounter=0;
   // static memory
   // int AlignCounter[buffer_needed];
   // AlignCounter[0]=0;
   function(AlignCounter);
   free(AlignCounter);

   return 0;
}

I hope it help you.

Comments

0

Well, int* AlignCounter creates a pointer but doesn't allocate anything for it to point to. Then AlignCounter=0; makes it NULL. If you're then sending it to align_S_T, you're just referencing a NULL pointer. Boom.

You probably want to declare int AlignCounter = 0, then send &AlignCounter to your function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.