I'm either missing something small or theres a BIG problem somewhere. The following code compiles sucessfully with g++ 4.1.0 under SuSE 10.1 i586 (Linux 2.6.16.13-4-default i686) but it gives a Segmentation Fault when I run it.

#include <stdio.h>
#include <list>

typedef struct    {
    std::list<int> b;
} data;

int main(int argc, char **argv)    {
    data *tmp = (data*)malloc(sizeof(data));

    printf("Hello1\n");
    tmp->b.push_back(3);
    printf("Hello2\n");

    return 0;
}

Output:
Hello1
Segmentation fault

In fact, any operation on the list (and only the list, everything else works fine, even other members inside the same struct) results in some kind of segmentation fault as if the list needs initialization.

Any help will be appreciated (a lot).

Dani AI

Generated

Short answer: the std::list inside your struct was never constructed when you used malloc. spotted the root cause — calling methods on an object whose constructor never ran is undefined behavior and typically crashes because internal pointers are garbage. For background on object lifetime and why constructors/destructors matter, see Object lifetime (cppreference).

Prefer RAII: let C++ construct and destroy the object for you. Modern, safe patterns:

#include <memory>
#include <list>

struct data { std::list<int> b; };

int main() {
    auto tmp = std::make_unique<data>(); // use smart pointer, no manual delete
    tmp->b.push_back(3);
}

Only use raw allocation when you actually need it (very rare). If you must allocate raw storage, use placement new to run the constructor and call the destructor explicitly before freeing the memory:

#include <cstdlib>
#include <new>
#include <list>

struct data { std::list<int> b; };

int main() {
    void* mem = std::malloc(sizeof(data));
    data* tmp = new (mem) data;   // placement new constructs the object
    tmp->b.push_back(3);
    tmp->~data();                 // explicitly call destructor
    std::free(mem);
}

Never mix malloc/free with new/delete for the same object. For debugging these crashes, compile with debugging symbols and warnings (-g -Wall -Wextra) and use tools such as [AddressSanitizer] or Valgrind to spot uses of uninitialized or unconstructed objects. The stylistic points by and are valid (prefer idiomatic struct/class usage and iostreams in C++); the important fix here is ensuring object constructors run. For placement new and related rules see Placement new (cppreference) and for smart pointers see std::unique_ptr.

Recommended Answers

All 4 Replies

> as if the list needs initialization.

I'd say that's dead on. If you use malloc the list's constructor isn't going to be called. Since this is C++ you should be using new/delete. You should also avoid using naked pointers most of the time, in favor of smart pointers like std::auto_ptr or a smart pointer provided by the boost library, which will automatically free resources through their destructor

Also, if you're using C++, there's no reason you should be writing typedef struct { ... } foo; . Just write struct foo { ... }; .

Cool stuff, thank you very much.

And, cout is the C++ output command, not printf() , so you don't need stdio.h any longer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.