Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:19:03+00:00 2026-06-14T19:19:03+00:00

I am just beginning with programming in c++ (IDE is CodeBlocks). I wrote two

  • 0

I am just beginning with programming in c++ (IDE is CodeBlocks).

I wrote two simple classes (Song and Metadata) and testing around with sqlite.

The Problem: My program seems to get insufficient memory: When I start the program it either crashes immediately or the sqlite3_open command returns the error “out of memory”. When I test the SQLITE function without any other code, it works!

So this does not work:

#include <iostream>
#include "metadata.h"
#include "Song.h"
#include <cstdio>
#include "sqlite3.h"

using namespace std;

int main()
{
    // Without this block it DOES work!
    Metadata* tmpMeta  = new Metadata("Eiffel 65", "Blue", "Europop");
    Song* tmpSong;
    tmpSong = new Song();
    tmpSong->set_metadata(*tmpMeta);
    // end of block

    sqlite3 *db;
    int rc;
    rc = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    cout << "Result: " << sqlite3_errmsg(db) << '\n';
    sqlite3_close(db);
    return 0;
}

Please see the Song and Metadata classes attached.

I do not know how to react to this error.

My environment:
– Windows 8, 64 bit
– Code::Blocks 10.05
– Compiler: GNU GCC Compiler

Thank you in advance!

Greetings
Sebastian

Song.h:

#include <iostream>
#include <cstring>
#include "metadata.h"

using namespace std;

class Song {
    private:
    Metadata metadata;
    unsigned int iD;
    char filename[400];

    public:
    //Constructors
    Song( Metadata, unsigned int, const char*);
    Song( );

    //Methods
    void set_metadata(Metadata& meta);
    void set_id(unsigned int i);
    void set_filename(const char* f);

    Metadata get_metadata();
    unsigned int get_id();
    const char* get_filename();

};

Song.cpp:

#include <iostream>
#include "Song.h"

using namespace std;

Song::Song(Metadata m, unsigned int id, const char* f) {
    metadata = m;
    iD = id;
    strncpy(filename, f, sizeof(filename)-1);
    filename[sizeof(filename)] = '\0';
}

Song::Song( ) {
    Metadata tmpMeta;
    metadata = tmpMeta;

    iD = 0;
    strncpy(filename, "unknown", sizeof(filename) -1);
    filename[sizeof(filename)] = '\0';
}

void Song::set_filename(const char* f) {
    strncpy( filename, f, sizeof(filename)-1 );
    filename[sizeof(filename)] = '\0';
}

void Song::set_id(unsigned int i) {
    iD = i;
}

void Song::set_metadata(Metadata& meta) {
    metadata = meta;
}

Metadata Song::get_metadata() {
    return metadata;
}

const char* Song::get_filename() {
    return filename;
}

unsigned int Song::get_id() {
    return iD;
}

Metadata.h:

#include <iostream>
#include <cstring>
#ifndef _METADATA_H_
#define _METADATA_H_

using namespace std;
class Metadata {
    private:
    unsigned int trackNumber;
    char artist[20];
    char title[20];
    unsigned int year;
    char genre[20];
    char album[20];

    public:
    Metadata(const char*, const char*, const char*);

    const char* get_artist();
    const char* get_title();
    const char* get_album();
    const char* get_genre();
    unsigned int get_trackNumber();
    unsigned int get_year();

    void set_artist(const char*);
    void set_title(const char*);
    void set_album(const char*);
    void set_genre(const char*);
    void set_year(unsigned int);
    void set_trackNumber(unsigned int);
};

#endif

Metadata.cpp:

#include <iostream>
#include "metadata.h"

Metadata::Metadata(const char* ar, const char* tit, const char* al) {
    trackNumber = 0;
    year = 0;
    strncpy(genre, "unknown", sizeof(genre) -1);
    genre[sizeof(genre)] = '\0';

    strncpy(artist, ar, sizeof(artist) -1);
    artist[sizeof(artist)] = '\0';

    strncpy(title, tit, sizeof(title) -1);
    title[sizeof(title)] = '\0';

    strncpy(album, al, sizeof(album) -1);
    album[sizeof(album)] = '\0';
}

const char* Metadata::get_artist() {
    return artist;
}

const char* Metadata::get_title() {
    return title;
}

const char* Metadata::get_album() {
    return album;
}

const char* Metadata::get_genre() {
    return genre;
}

void Metadata::set_artist(const char* ar) {
    strncpy(artist, ar, sizeof(artist) -1);
    artist[sizeof(artist)] = '\0';
}

void Metadata::set_title(const char* tit) {
    strncpy(title, tit, sizeof(title) -1);
    title[sizeof(title)] = '\0';
}

void Metadata::set_album(const char* al) {
    strncpy(album, al, sizeof(album) -1);
    album[sizeof(album)] = '\0';
}

void Metadata::set_genre(const char* g) {
    strncpy(genre, g, sizeof(genre) -1);
    genre[sizeof(genre)] = '\0';
}

void Metadata::set_trackNumber(unsigned int tn) {
    trackNumber = tn;
}

void Metadata::set_year(unsigned int y) {
    year = y;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:19:05+00:00Added an answer on June 14, 2026 at 7:19 pm

    For starters: All your string termination code is wrong in Metadata.cpp and Song.cpp:

    Example:

    strncpy(genre, "unknown", sizeof(genre) -1);
    genre[sizeof(genre)] = '\0';
    

    The call to strncpy() is correct. The hard-terminator is NOT. it should be:

    strncpy(genre, "unknown", sizeof(genre) -1);
    genre[sizeof(genre) -1] = '\0';
    

    This is replicated on all your strings. recheck them all. it will surface several side-effects until you fix them. Considering these objects are all heap-allocated, you’re corrupting your heap on the improper termination of Song::filename[] and MetaData::albumin[]. Don’t just fix those two; fix them all.

    Remember. sizeof() returns an octet-count of the field passed. which means char field[N]; will return N. if you already know C/C++ you know arrays are zero-based and therefore field[N-1] is the maximum allowable index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am just beginning recently developing some simple apps for the iphone. I
I am just beginning design work on a problem that I'm sure has been
I'm just beginning to learn python. I wrote an example script to test OOP
I am just beginning to play around with .NET (using C#). Which namespace, aside
I'm just beginning to learn programming (on C++), and by beginning I mean total
I'm just beginning to learn programming (on C++ and Python), and by beginning I
I'm just beginning F# and haven't really done functional programming since my programming languages
I'm just starting to mess around with some web programming, and am trying to
I am just beginning programming and for my first project, I decided to write
I'm just beginning to (hopefully!) learn programming / ruby on rails and trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.