Variables suddenly disappering (C programming)

Click For Summary

Discussion Overview

The discussion revolves around a problem encountered in C programming related to dynamic arrays, specifically an issue where elements of a 3-dimensional dynamic array appear to be "disappearing" or becoming inaccessible, leading to segmentation faults. Participants explore potential causes and solutions related to memory allocation and variable declarations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes experiencing a segmentation fault and memory access issues with a 3-dimensional dynamic array, noting that some elements seem to lose their values unexpectedly.
  • Another participant suggests that the issue may be similar to a previously encountered problem, although they cannot confirm without seeing the code.
  • A participant questions whether it is possible to use an undeclared variable in C, indicating uncertainty about variable declarations.
  • A later reply indicates that the original poster found an error in memory allocation, suggesting that this was a contributing factor to the issue.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the exact cause of the problem initially, but there is agreement that memory allocation errors can lead to such issues. The discussion reflects uncertainty and exploration of different aspects of the problem.

Contextual Notes

Participants reference specific lines of code and memory addresses, indicating a technical debugging process. There is mention of checking memory usage, but no definitive conclusions are drawn about the underlying cause until the memory allocation error is identified.

andresordonez
Messages
65
Reaction score
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
You seem to have a similar problem with this. Although I can't say for sure without the code.
 
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?!
 
Aha! I found it, there was an error in the memory allocation. Thanks Grieverheart! you pointed me in the right direction.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
235
Views
14K
Replies
12
Views
2K
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
Replies
47
Views
5K