Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,423 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,339 people online right now. Registration is fast and FREE... Join Now!




malloc error that makes me want to kill myself

 
Reply to this topicStart new topic

malloc error that makes me want to kill myself

laharah
14 Oct, 2008 - 09:26 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 2

Hi everyone,

I'm working on a problem from the euler project (#185). anyways, I've written up this "smart" bruteforcing algorithm in C++. now the execution of the algorithm is ugly I know (not a very good programmer) but I've been getting a malloc error and I really can't see why, it's only about 1/2KB. through commenting out code, I've figured out that the error is in this subset of functions. here is a quick setup of the way it's structured:
CODE

class Num{
public:
  int value;
  bool active;
};

class Guess{
public:
  Num list[16];
  int numCorrect;
};


CODE

bool crunchLoop( Guess* guesses, int guessesLength, Num* table, int tableLength, int column ){

//backs up the two tables it recieves, this allows it to revert to a previous state
//when it hits a block that can't continue

  Guess* guessesBackup = backupGuesses(guesses, guessesLength);
  Num* tableBackup = backupTable(table, tableLength);
  
  for(int i=1; i<10; ++i){
    if(table[column*i].active == true)  
      updateTables(guesses, guessesLength, table, tableLength, column, table[column*i].value);
      crunchLoop( guesses, guessesLength, table, tableLength, column+1);
    }

  revertGuesses(guesses, guessesBackup, guessesLength);
  revertTable(table, tableBackup, tableLength);
  return false;
}
//-----------------------------------------------
Num* backupTable( Num* &table, int tableLength){
//backs up the table for reversion
Num* tableBackup = new Num[tableLength * 10];
  for(int i=0; i< tableLength*10; ++i){
    tableBackup[i].value = table[i].value;
    tableBackup[i].active = table[i].active;
  }
  return tableBackup;
}
//----------------------------------------------
Guess* backupGuesses( Guess* &guesses, int guessesLength){
  Guess* guessesBackup = new Guess[guessesLength*16];
  int i = 0;
  for(i=0; i < guessesLength; ++i){
    for(int j=0; j < 16; j++){
      guessesBackup[i].list[j].value = guesses[i].list[j].value;
      guessesBackup[i].list[j].active = guesses[i].list[j].active;
    }
    guessesBackup[i].numCorrect = guesses[i].numCorrect;
  }
  return guessesBackup;
}


I believe that the malloc is coming from backupGuesses though I can't see what's causing it. if anyone has any suggestions or see's something else that's crippling I'd love for any help I can get. also I can post the entirety of the code, but I warn you it's not pretty.
thanks ;-)



User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Malloc Error That Makes Me Want To Kill Myself
14 Oct, 2008 - 10:02 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
I can't understand these Num* &table kind of formal parameter you have in your functions.

User is offlineProfile CardPM
+Quote Post

David W
RE: Malloc Error That Makes Me Want To Kill Myself
15 Oct, 2008 - 01:03 AM
Post #3

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
How about giving an overview of what the program is supposed to do?

All I grasped so far (after a quick look) is that on some error flag you want to

//back up the two tables ... this allows it to revert to a previous state
//when it hits a block that can't continue


Can you dump a copy to disk ... or do you want to keep a 2nd copy of both tables (always) in memory, so that you can quickly switch to their pointers ?

Just wondering why too, you are using malloc in C++ ( and arrays ?)

Maybe a vector is a good option to hold your objects ? (Tables?)

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Malloc Error That Makes Me Want To Kill Myself
15 Oct, 2008 - 03:37 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,024



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
If you're using C++ and classes, why are you using malloc?

This looks to be wrong:
cpp

Guess* guessesBackup = new Guess[guessesLength*16];


Maybe you meant this?
cpp

Guess* guessesBackup = new Guess[guessesLength];


Just a guess, though. It's impossible to really see what the point of this is with what's been offered. A quick observation is that the static numbers ( 16, 10 ) with no real reference to what they mean make what is offered a little cryptic. I'd put a copy method inside the classes, probably make things easier to follow as well.

User is online!Profile CardPM
+Quote Post

laharah
RE: Malloc Error That Makes Me Want To Kill Myself
15 Oct, 2008 - 09:42 AM
Post #5

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 2

QUOTE(baavgai @ 15 Oct, 2008 - 04:37 AM) *

This looks to be wrong:
cpp

Guess* guessesBackup = new Guess[guessesLength*16];


Maybe you meant this?
cpp

Guess* guessesBackup = new Guess[guessesLength];


yes it is. thanks, I compleatly missed that. after fixing it, I just get a segmentation fault, which is probably somewhere else in my poorly written code. if you feel like wasting some time I've attached my code (number_mind.txt). (help is always appreciated).

QUOTE(baavgai @ 15 Oct, 2008 - 04:37 AM) *

If you're using C++ and classes, why are you using malloc?


I wasn't using malloc, that's just the error I was getting. and num and guess weren't originally classes they were structs that got turned into classes while I was dubuging. As were passing the pointers as references. Thanks Guys!



Attached File(s)
Attached File  guess.txt ( 616bytes ) Number of downloads: 5
Attached File  number_mind.txt ( 6.46k ) Number of downloads: 4
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:19PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month