Valgrind: Conditional jump or move depends on uninitialised value(s)

Pages: 12
I wrote underneath this time[0]=0; but as I recently learned that is not a valid c++ expression.

What on earth do you mean? Of course it's a valid C++ expression! Why do you think it's not?

However, it only initialises the first element of your array.

So, I wrote, std::vector<double> time(iter_max + 1, 0.0);

This, on the other hand, does indeed initialise every element of the array.

Actually, I didn't realize that, if iter == iter_max this will be past the end of array!

Do you understand why?
Last edited on
I meant double time[iter_max + 1]; isn't c++ valid expression.

Do you understand why?

I am not 100% sure why to be honest
Last edited on
Can I add my 2 cents worth? The other respondents are vastly more experienced & knowledgeable than I, but I am hoping this may be helpful in some way.

Can we see your updated code now that you are using vectors?

In an overall sense, it seems that you have found some C functions that do what you want, but surely there are C++ libraries out there that do the same thing. Using C functions seems to have led to the whole program to be written in C. The program might be somewhat simpler/easier if it was written in C++.

I am wondering if you are aware of the differences between C and C++ ? IMO the two are rather different animals. C++ has the Standard Template Library (STL), this basically means there are containers like std::vector, classes and algorithms that operate on them, plus things like range based for loop, which means one can avoid all these problems with arrays. Range based for loops are very useful for iterating through an entire container easily.

JamieAI wrote:
I am not 100% sure why to be honest


Array indexes start at zero. If one had an array with 10 elements: the first index is 0, the last element would have index of 9.
Consider this:

1
2
3
4
5
6
constexpr ArraySize {10}; // array size must be const, constexpr is stronger than const
int MyArray[ArraySize];

for (std::size_t Counter {0}; Counter < ArraySize ; Counter++ ) {
    MyArray[Counter] = 0;
}


With std::vector one does not need to worry about the size*, it will resize automatically, it does dynamic memory behind the scenes safely - no need for operator new or pointers.

* One can reserve the size for a vector, so that all the memory is allocated at once, rather than the compiler having to std::move the contents multiple times as it grows large. There is a strategy for memory allocation as a vector grows, it might say double in size each time it reaches it's max size. But if one knew there were going to be 1 million elements, then it's better to reserve that space once at the start.

ne555 pointed out earlier that you could use std::vector<std::complex>, I am wondering if you were aware that STL containers can also hold a class or struct type that you might invent yourself?

1
2
3
4
5
struct DataItem {
   double time {0.0}; // brace intialisation compiler warns about narrowing if one puts an int say.
   double momentum {0.0};
   double temperature {0.0};
};

// https://en.cppreference.com/w/cpp/container/vector/vector
std::vector<DataItem> MyData(10); // 10 default inserted DataItem's

Range based for loop
https://en.cppreference.com/w/cpp/language/range-for

Good Luck !!
Last edited on
Array indexes start at zero

A design decision inherited from BCPL, which was probably made for reasons discussed by Dijkstra
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

The letter also covers why we conventionally write loops with the less-than sign
for (int i = a; i < b; ++i)
Expressing the half-open interval [a, b).

Indexing from zero also makes sense in context of a machine's memory -- but this is just a special case of Djikstra's argument:

If p is the address of an array, then the first element in the array is located at an offset of 0 elements past p; the second element is located at an offset of 1 element from the beginning of the array.
Last edited on
@TheIdeasMan
Yes, I originally started using C, the end goal is to have C++ but like you said just got stuck with whatever is working for me. I am trying to learn more and your comments are very helpful. I can provide the updated code as well, it's still unfinished but making progress and fixed a lot of the major issues:
https://www.zipshare.com/download/eyJhcmNoaXZlSWQiOiJhOTkxMzRmZi1mN2JlLTRhNDQtYjFhNS05NjkwZTNjOTJhMWIiLCJlbWFpbCI6ImFzdHJvbHVqeUBnbWFpbC5jb20ifQ==

@JamieAl

I guess one could try to convert this to C++, but it probably worth trying to get it to work in C at the moment.

The thing is, one wouldn't normally start with C and have C++ as an end goal: the two are quite different animals, so the work flows are different. It is a big misconception to think that C++ is C with classes.

I haven't programmed in C since the late 1980's, but I do remember that one had to be so much more careful compared to C++. This is because C++ has things like the STL which takes away the tricky things like memory management and pointers.

What are the issues you are having at the moment?

I guess you should make sure that:

* memory is allocated for everything
* all values are initialised with actual values. Note that initialising to zeroes is better than uninitialised values, but can lead to other problems such as division by zero. So that is why I say initialise to actual values.
* validate the input
* make sure that each item in an array is being dealt with: don't skip the first one; don't go past the end
* check that there are no mathematical problems with the values: division by zero (write code to check denominators), discontinuities in math functions at particular values, values that become too small or large and cause problems.
* make use of const correctness, so the compiler can help if you try to change a const value. Have const parameters in your functions where you can. All those values at the beginning of main - could they be const ?

With variable & function names: If one does a good job of this, the code should read like a story of what is happening. Although I do try to keep variable names similar to what they are in the mathematical or physics or Wkipedia documentation. If there is a link to such documentation I put it in the code as a comment. A reasonable expectation is that if a reader of the code has even a partial education in maths or physics, but not much knowledge of your field, they should be able to read the documentation and be able to understand what is going in your code.

I mentioned earlier about compiler settings to produce warnings or errors, make use of this as much as you can. The compiler is your friend, telling you where problems lie.

Finally, once you have code that compiles you can use a debugger.
There is a c++ wrapper for this same software:

https://sourceforge.net/projects/fftwpp/

and another one here:

http://mffmfftwrapper.sourceforge.net/
Last edited on
@TheIdeasMan
Thank you for the great advice! The code is working now, with no memory leaks so far, which is great news to me.
Topic archived. No new replies allowed.
Pages: 12