0

I have a multidimensional array (a representation of a matrix). I need to zero out, not the entire array, but a section of it. What's the best method of doing this? I tried using memset, but it just gives me a typecast error.

Example

_matrix[row][column] = memset(
                        _matrix[row][column], 
                        0, 
                        sizeof(_matrix[row][column])
                    );

Declaration

float** _matrix = new float*[NUM_ROWS][NUM_COL];

zeboidlund
  • 9,731
  • 31
  • 118
  • 180
  • 6
    Why would you assign the result of `memset`? – Tom van der Woerdt Dec 27 '11 at 22:55
  • 3
    omg. you should consider getting a good book on C/C++ – Anycorn Dec 27 '11 at 22:55
  • What is the section you want zeroed out? A row? column? Rows and columns? – Marlon Dec 27 '11 at 22:56
  • @Marlon what do you think? I do have a good book on C/C++ - and I'm taking a class this quarter on it :P. – zeboidlund Dec 27 '11 at 22:59
  • there is a host of problems with your snippet. C++ has very large learning curve - and you have to understand memory layout and indexing. you really should be asking more basic questions or read similar posts. – Anycorn Dec 27 '11 at 23:01
  • 2
    @Holland : He said a _good_ book. ;-] Which book are you reading? – ildjarn Dec 27 '11 at 23:02
  • cf: http://stackoverflow.com/questions/5633609/c-matrix-class-suggestions, http://stackoverflow.com/questions/2076624/c-matrix-class – Anycorn Dec 27 '11 at 23:03
  • @ildjarn The OpenGL red book. – zeboidlund Dec 27 '11 at 23:05
  • 1
    The OpenGL red book might be a good book on OpenGL, but it's not a good book to learn C++ from. – Fred Foo Dec 27 '11 at 23:06
  • Would you have any suggestions? I'm looking for something which I can literally open up for a few minutes and scan through, rather than read from cover to cover. I'm passed the basics of programming - I just came from C#. – zeboidlund Dec 27 '11 at 23:09
  • @Holland : See this thread: [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/636019) – ildjarn Dec 27 '11 at 23:10

2 Answers2

2

The first parameter to memset() is the address of the location to zero. So:

memset(&_matrix[row][column], ...)

However, in your case the following would be far more straightforward:

_matrix[row][column] = 0.0;
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

This is tagged as C++ yet you're not really doing any C++. Why not just do that? Avoid all the lower level memset and direct array nonsense.

I certainly think you should take some of the previous advice and really learn the underlying C, memory access, etc., but in C++ this could simply be avoided.

A quick line of code to get you on your way:

typedef std::vector<std::vector<int> > Array2D;
NuSkooler
  • 5,391
  • 1
  • 34
  • 58