Variables suddenly disappering (C programming)

In summary, I am having trouble generating powers_b_c_lambda because there's an error in memory allocation.
  • #1
andresordonez
68
0
Hi, I have an array that is partially disappearing from one line of code to the next and I don't have idea what could be causing this. The pseudocode of the relevant chunk of code goes like this:

...
declare some 3d-dynamic arrays
allocate memory for the arrays
assign values to the elements in the arrays
do something unrelated to the variables in the arrays
do something related to the variables in the arrays
...

when I run the code I get a segmentation fault, when I checked with gdb what was causing this segmentation fault I realized that some elements in one of the arrays were lost, i.e. when printing the value of the variable in gdb the output was "Cannot acces memory at address 0xa3a5b30acffdce5a".

Then I looked more carefully for the problem and I found this:

*****************************************************************************
(gdb) n
260 generate_powers_b_c(powers_b_c, powers_b_c_perm, alpha, alpha, p.lambdaD, edim, edim);
(gdb) n
261 generate_powers_a_b_lambda(powers_a_b_lambda, alpha, alpha, p.lambdaD, edim, edim);
(gdb) p powers_b_c[1][2][2]
$31 = 10509.453369140625
(gdb) n
262 generate_powers_a_c_lambda(powers_a_c_lambda, powers_a_c_lambda_perm, alpha, alpha, p.lambdaD, edim, edim);
(gdb) p powers_b_c[1][2][2]
$32 = 10509.453369140625
(gdb) n
263 generate_powers_b_c_lambda(powers_b_c_lambda, powers_b_c_lambda_perm, alpha, alpha, p.lambdaD, edim, edim);
(gdb) p powers_b_c[1][2][2]
$33 = 10509.453369140625
(gdb) n
267 for( bra = 0; bra < dim; bra++){
(gdb) p powers_b_c[1][2][2]
Cannot access memory at address 0xa3a5b30acffdce5a
*****************************************************************************

where:

powers_b_c, powers_b_c_perm, powers_a_b_lambda, powers_a_c_lambda, powers_a_c_lambda_perm, powers_b_c_lambda, powers_b_c_lambda_perm

are 3-dimensional dynamic arrays.

As can be seen, the value of powers_b_c[1][2][2] is lost as soon as line 263 is executed. It can also be seen that there's no apparent way in which powers_b_c is modified in line 263. The same happens with several elements of all the arrays, there are some elements that don't disappear but instead change their values.

At first I thought it could be due to insufficient memory in my laptop but according to "top" at this stage the program has used less than 0.8% of the total memory.

I don't have a clue about what could be causing this.

If anyone wants further information about the problem or if I was unclear please let me know.

Thanks in advance.
 
Technology news on Phys.org
  • #2
You seem to have a similar problem with this. Although I can't say for sure without the code.
 
  • #3
Thanks for the immediate reply!

It sure looks like my problem. I just finished checking that I have all the declarations and function prototypes right. :(

By the way, can you use an undeclared variable in C?!
 
  • #4
Aha! I found it, there was an error in the memory allocation. Thanks Grieverheart! you pointed me in the right direction.
 
  • #5


Dear scientist,

Thank you for sharing your issue with disappearing variables in your C programming code. Based on the information provided, it seems that there may be an issue with the memory allocation or management in your code. This can sometimes lead to unexpected changes or loss of variables.

I would suggest checking your code for any errors related to memory allocation or management, such as using uninitialized variables or accessing memory that has already been freed. It may also be helpful to use a debugger like gdb to track the values of your variables at each step and see where the issue may be occurring.

Additionally, it may be helpful to consult with other programmers or seek assistance from online forums to troubleshoot and find a solution to this issue. I hope this helps and wish you the best of luck in resolving this issue.
 

1. Why are my variables suddenly disappearing in my C program?

Variables can disappear in a C program due to a variety of reasons, such as incorrect use of memory management functions, overwriting variable values, or errors in code logic. It is important to carefully review your code and check for any potential bugs or mistakes that may be causing the issue.

2. How can I prevent my variables from disappearing in my C program?

To prevent variables from disappearing, it is important to properly initialize variables before using them and make sure they are allocated enough memory. Additionally, avoid overwriting variable values unintentionally and use memory management functions correctly to avoid memory leaks.

3. Can a variable suddenly disappear without any errors or warnings?

Yes, a variable can suddenly disappear without any errors or warnings if it is overwritten or goes out of scope. This can happen if the variable is declared within a block of code and is not accessible outside of that block.

4. Can a function cause variables to suddenly disappear?

Yes, a function can cause variables to suddenly disappear if it modifies or deallocates variables within its execution. This can happen if the function is not properly designed to handle variables or if there are errors in the code logic.

5. How can I debug variables disappearing in my C program?

To debug variables disappearing, you can use tools such as a debugger or print statements to track the values and memory allocation of your variables. This can help identify where the issue is occurring and allow you to fix it. You can also try reviewing your code and checking for any potential mistakes or using debugging techniques such as stepping through the code line by line.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
7
Replies
235
Views
9K
  • Programming and Computer Science
Replies
2
Views
967
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
936
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top