Question about memory allocation

Discussion in 'C++' started by laniik, Apr 8, 2005.

  1. laniik

    laniik Guest

    I am relativly new in c++, so Im not entirely sure about how the memory
    allocation stuff works. My problem is that i am running out of memory
    after what is seemingly a very small amoune of memory allocation.

    i have a class:

    struct Point{
    void set(int,int,int);
    float distance(Point);
    int x,y,z;
    };

    i am doing all my allocations with new, for example:

    Point* points = new Points[256];
    (which i do 4 times)

    (i am making sure to delete[] points; however, my program is crashing
    before it even gets to the deletes)

    i get crashes in built in functions like strlen(). which im assuming
    just means that i am out of memory, OR that my memory has become
    corrupted. but just 1024 instances of a small class cannot be anywhere
    near the maximum amount of memory that I can use in a program can it?
    is there anyway to allocate more memory?

    Thanks!

    Oliver
     
    laniik, Apr 8, 2005
    #1
    1. Advertising

  2. laniik

    Mark P Guest

    laniik wrote:
    > I am relativly new in c++, so Im not entirely sure about how the memory
    > allocation stuff works. My problem is that i am running out of memory
    > after what is seemingly a very small amoune of memory allocation.
    >
    > i have a class:
    >
    > struct Point{
    > void set(int,int,int);
    > float distance(Point);
    > int x,y,z;
    > };
    >
    > i am doing all my allocations with new, for example:
    >
    > Point* points = new Points[256];
    > (which i do 4 times)
    >
    > (i am making sure to delete[] points; however, my program is crashing
    > before it even gets to the deletes)
    >
    > i get crashes in built in functions like strlen(). which im assuming
    > just means that i am out of memory, OR that my memory has become
    > corrupted. but just 1024 instances of a small class cannot be anywhere
    > near the maximum amount of memory that I can use in a program can it?
    > is there anyway to allocate more memory?
    >


    Unless you're running on a Vic-20 it's nearly certain that you're not
    running out of memory from allocating ~3K integers. Unfortunately,
    since you haven't posted complete code, there's no way for us to divine
    what you are doing wrong. (hint hint...)
     
    Mark P, Apr 8, 2005
    #2
    1. Advertising

  3. laniik

    Alan Brown Guest

    Mark P <> wrote in
    news:qOl5e.746$:

    > laniik wrote:
    >> I am relativly new in c++, so Im not entirely sure about how the memory
    >> allocation stuff works. My problem is that i am running out of memory
    >> after what is seemingly a very small amoune of memory allocation.
    >>
    >> i have a class:
    >>
    >> struct Point{
    >> void set(int,int,int);
    >> float distance(Point);
    >> int x,y,z;
    >> };
    >>
    >> i am doing all my allocations with new, for example:
    >>
    >> Point* points = new Points[256];
    >> (which i do 4 times)
    >>
    >> (i am making sure to delete[] points; however, my program is crashing
    >> before it even gets to the deletes)
    >>
    >> i get crashes in built in functions like strlen(). which im assuming
    >> just means that i am out of memory, OR that my memory has become
    >> corrupted. but just 1024 instances of a small class cannot be anywhere
    >> near the maximum amount of memory that I can use in a program can it?
    >> is there anyway to allocate more memory?
    >>

    >
    > Unless you're running on a Vic-20 it's nearly certain that you're not
    > running out of memory from allocating ~3K integers. Unfortunately,
    > since you haven't posted complete code, there's no way for us to divine
    > what you are doing wrong. (hint hint...)


    Running C++ on a VIC-20 Wow!!!!

    Now there's a idea

    Alan
     
    Alan Brown, Apr 8, 2005
    #3
  4. laniik wrote:
    > I am relativly new in c++, so Im not entirely sure about how the memory
    > allocation stuff works. My problem is that i am running out of memory
    > after what is seemingly a very small amoune of memory allocation.
    >
    > i have a class:
    >
    > struct Point{
    > void set(int,int,int);
    > float distance(Point);
    > int x,y,z;
    > };
    >
    > i am doing all my allocations with new, for example:
    >
    > Point* points = new Points[256];
    > (which i do 4 times)
    >
    > (i am making sure to delete[] points; however, my program is crashing
    > before it even gets to the deletes)
    >
    > i get crashes in built in functions like strlen(). which im assuming
    > just means that i am out of memory, OR that my memory has become
    > corrupted. but just 1024 instances of a small class cannot be anywhere
    > near the maximum amount of memory that I can use in a program can it?
    > is there anyway to allocate more memory?
    >
    > Thanks!
    >
    > Oliver
    >


    Your assumptions that you are out of memory are wild speculation. Wrong
    theory. You will have to actually debug your code to get a better idea
    of what is going wrong.

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Apr 8, 2005
    #4
  5. laniik

    Mark P Guest

    Alan Brown wrote:
    > Mark P <> wrote in
    >>Unless you're running on a Vic-20 it's nearly certain that you're not
    >>running out of memory from allocating ~3K integers. Unfortunately,
    >>since you haven't posted complete code, there's no way for us to divine
    >>what you are doing wrong. (hint hint...)

    >
    >
    > Running C++ on a VIC-20 Wow!!!!
    >
    > Now there's a idea
    >


    It was the first thing that came to mind since I used to have a Vic-20
    with 4K RAM and 3000 integers seemed about the right amount to exhaust
    the memory. :)
     
    Mark P, Apr 8, 2005
    #5
  6. laniik wrote:

    > I am relativly new in c++, so Im not entirely sure about how the memory
    > allocation stuff works. My problem is that i am running out of memory
    > after what is seemingly a very small amoune of memory allocation.
    >
    > i have a class:
    >
    > struct Point{
    > void set(int,int,int);
    > float distance(Point);
    > int x,y,z;
    > };
    >
    > i am doing all my allocations with new, for example:
    >
    > Point* points = new Points[256];
    > (which i do 4 times)
    >
    > (i am making sure to delete[] points; however, my program is crashing
    > before it even gets to the deletes)
    >
    > i get crashes in built in functions like strlen(). which im assuming
    > just means that i am out of memory, OR that my memory has become
    > corrupted. but just 1024 instances of a small class cannot be anywhere
    > near the maximum amount of memory that I can use in a program can it?
    > is there anyway to allocate more memory?
    >
    > Thanks!
    >
    > Oliver

    Hi

    this Source works on my System ( GCC 3.X )

    #include <stdio.h>

    struct Point{
    float distance(Point);
    void set( int , int , int );
    int x,y,z;
    };

    int main()
    {
    Point* points = new Point[256];
    Point* points2 = new Point[256];
    Point* points3 = new Point[256];
    Point* points4 = new Point[256];

    delete points;
    delete points2;
    delete points3;
    delete points4;
    printf("HI");
    return 0;
    }

    .... No probmlem ....

    Greetings
    Drk

    --
    develop - Projekt
    A Tool for c-c++ / java / html / php
    http://dirk-wendland.de.vu
     
    Dirk Wendland, Apr 8, 2005
    #6
  7. laniik

    Andre Caldas Guest

    > delete points;
    > delete points2;
    > delete points3;
    > delete points4;


    Usually you should do:
    delete [] points;
    delete [] points2;
    delete [] points3;
    delete [] points4;
     
    Andre Caldas, Apr 8, 2005
    #7
  8. laniik

    laniik Guest

    hm the reason i thought it was running out of memory is that it was
    crashing when i was trying to allocate more memory, and the get last
    error was "out of memory". both new and malloc gave me identical
    results. (malloc returns a pointer to 0x0000000)
     
    laniik, Apr 8, 2005
    #8
  9. laniik

    Default User Guest

    laniik wrote:
    > hm the reason i thought it was running out of memory is that it was
    > crashing when i was trying to allocate more memory, and the get last
    > error was "out of memory". both new and malloc gave me identical
    > results. (malloc returns a pointer to 0x0000000)


    You are almost certainly corrupting the free store with some sort of
    undefined behavior. What we need is a minimal, complete program that
    demonstrates the problem. Possibly in cutting down your program, you'll
    discover whatever is causing the problem.



    Brian
     
    Default User, Apr 8, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    757
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    632
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,947
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    1,031
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,222
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page