malloc(): corrupted top size. Aborted (core dumped)

I am simply opening a file inside a function. I really have no idea what could cause such a problem
1
2
3
4
5
6
7
8
#include "encoder.h"

void Encoder::Netpbm(unsigned int MagicNumber, RGBMatrix matrix){

  std::ofstream img("test.txt");
  img.close();
  return;
}

here is the function where i call the Netpbm
1
2
3
4
5
6
7
8
9
10
11
void test(){
RGBMatrix test;

test.ModifySize(5,5);
test.Fill(255,0,0);
test.Print();
Encoder encoder;
//encoder.OutputFileName = "test.txt";
encoder.Netpbm(3, test);

}

everything main does is to call test();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(int argc, char *argv[]){
  while((++argv)[0]){
    if(argv[0][0] == '-')
      switch (argv[0][1]) {
        default: //wrong command
          printf("Command Unknown, type -help for a list of commands.\n");
          break;
        case 'h': //help
          printf("help is on the way\n");
          break;
        case 't': //testCommand
          printf("test\n");
          test();
          break;
    }
  }


  return 0;
}
Last edited on
what means javascript:tx('code') ?
can you provide stack trace of this code?
Edit your post and use code tags.

[code]
    { code here }
[/code]


Where/when exactly does the error happen?
There's calls in your code that we can't see the implementation of.

1
2
test.ModifySize(5,5);
test.Fill(255,0,0);
what happens here?
Last edited on
wild guess, because you give us so little to work
RGBMatrix uses dynamic allocation and you failed to provide a proper copy constructor, assignment operator and destructor

also while((++argv)[0])
iirc, the last element of the array is a NULL pointer, not a null string
however, that's a moot point, use argc dammit
EDIT: I read something that would suggest that this is caused by an already heap corruption from before the command

This is all the code
The command that is causing me the error is in bold.

ne555
I suppose it does make it much easier to read. I will modify that, thank you!

Gando
Added! I posted all the code.

I just dont understand what could affect the opening of the file like that...

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstring>

#include "graph.h"
#include "encoder.h"

#include <fstream>

void test(){
RGBMatrix test;

test.ModifySize(5,5);
test.Fill(255,0,0);
test.Print();
Encoder encoder;
//encoder.OutputFileName = "test.txt";
encoder.Netpbm(3, test);

}

int main(int argc, char *argv[]){
  while((++argv)[0]){
    if(argv[0][0] == '-')
      switch (argv[0][1]) {
        default: //wrong command
          printf("Command Unknown, type -help for a list of commands.\n");
          break;
        case 'h': //help
          printf("help is on the way\n");
          break;
        case 't': //testCommand
          printf("test\n");
          test();
          break;
    }
  }


  return 0;
}


encoder.cpp
1
2
3
4
5
6
7
8
#include "encoder.h"

void Encoder::Netpbm(unsigned int MagicNumber, RGBMatrix matrix){

  std::ofstream img("test.txt");
  img.close();
  return;
}


graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "graph.h"
#include <cstdio>

void RGBMatrix::ModifySize(const unsigned int w, const unsigned int h){
  width = w;
  height = h;
  matrix = new unsigned char*[h];
  for (unsigned int i = 0; i < height; i++) {
    matrix[i] = new unsigned char[w*3];
  }
  return;
}

void RGBMatrix::Fill(unsigned char Red, unsigned char Green, unsigned char Blue){
  for(unsigned char i = 0; i < height; i++)
    for (unsigned char j = 0; j < width*3; j++) {
        matrix[i][j*3]   = Red;
        matrix[i][j*3+1] = Green;
        matrix[i][j*3+2] = Blue;
    }
}

void RGBMatrix::Print(){
  for (unsigned int i = 0; i < height; i++) {
    for (unsigned int j = 0; j < width*3; j++) {
      printf("%d ", matrix[i][j]);
    }
    printf("\n");
  }
}


//GRAPH.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#ifndef __GRAPH_H_INCLUDED__
#define __GRAPH_H_INCLUDED__

class RGBMatrix{
  unsigned char **matrix;


public:
  unsigned int width, height;
  unsigned char* operator[](unsigned int i){ return matrix[i]; }
  void ModifySize(unsigned int width, unsigned int height);
  void Fill(unsigned char Red, unsigned char Green, unsigned char Blue);
  void Print();

};
#endif


//ENCODER.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#ifndef __ENCODER_H_INCLUDED__
#define __ENCODER_H_INCLUDED__
#include "graph.h"
#include <cstdio>
#include <fstream>
class Encoder{

public:
  char* OutputFileName;
  void Netpbm(unsigned int MagicNumber, RGBMatrix matrix);


};

#endif
Last edited on
Your error is looping up to width * 3 when you are also multiplying the index j by 3. Just loop up to width:

1
2
3
4
5
6
7
8
9
10
11
12
// You might want to introduce a couple of typedefs:
using byte = unsigned char;
using uint = unsigned int;

void RGBMatrix::Fill(byte Red, byte Green, byte Blue){
  for (uint i = 0; i < height; i++)
    for (uint j = 0; j < width; j++) {
        matrix[i][j*3]   = Red;
        matrix[i][j*3+1] = Green;
        matrix[i][j*3+2] = Blue;
    }
}

You should also have a constructor for the matrix that sets the width and height to 0 and the pointer to nullptr. And your ModifySize function should delete the previous matrix if the pointer isn't nullptr.
dutch ->Worked like a charm! Also very good suggestions, I will take that into account right away.
Thanks to everyone for help!
Topic archived. No new replies allowed.