• 0

[MFC] changing size of array at runtime?


Question

9 answers to this question

Recommended Posts

  • 0

I prefer to use STL for things like this when dealing with (personal preference).

example: An array that will hold a bunch of integers would be coded something like this:

#include <vector.h>
#include <iostream.h>
using namespace std; //to make the "No std::"-nazi's happy.

int main(int argc, const char * argv[])
{
 ?bool runLoop = true;
 ?vector myVector<int>;

 ?// First let's add some items to the array
 ?while(runLoop)
 ?{
 ? ?int temp;
 ? ?cout << "Enter a number (-99 quits): " << flush;
 ? ?cin >> temp;
 ? ?myVector.push_back(temp);
 ? ?if (temp == -99) runLoop = false;
 ?}
 ?myVector.pop_back(); ?// this line is needed to erase the "-99" in the last position.
 ?
 ?// lets print out the size of the vector, and then it's contents.
 ?cout << "\n\nThe size of myVector (vector) is: " << myVector.size() << endl << endl<< flush;
 ?for (int i = 0; i < myVector.size(); i++) cout << "element " << i << " is: " myVector[i];
  return 0;
}

Written here so it may not compile exactly as printed but I'm sure you get the idea. Vectors are nice because you can access them like a stanard array (array[index]), you can add/remove items to/from the end with ease, and you can use the STL routines to sort them.

Not as nice as NSMutableArray in Objective-C but it's not too bad either.

  • 0

I've done vector of vectors before, works fine and everything but if you prefer to use c-style memory management you can a)use a resize function (Which i believe is included) b)write your own.

vector< vector<int> > aliens;

//now to give memory to each vector of aliens
//makes something similar to aliens[5][11];
aliens.resize(5);
    for (int n=0;n<5;n++)
        aliens[n].resize(11);

  • 0

thanks, got vectors working now.

but i still cant get it to resize:

i can make it smaller and it works perfectly, however as soon as i try to make it bigger than its orrignal bounds i get a access violation

my code is:

decleration:

std::vector< std::vector<bool> > grid;
	std::vector< std::vector<bool> > newgrid;

resize function:

void Gbg::Resize(int x, int y)
{
	grid.resize(x);
	newgrid.resize(x);	
    for (int i=0;i<x;i++)
	{
  grid[i].resize(y);
  newgrid[i].resize(y);
	}


   for (int j=gx;j<x;j++)
   {
  for (int l = gy; l < y; l++)
  {
 	 grid[j][l] = false;
 	 newgrid[j][l] = false;
  }
   }

	gx = x;
	gy = y;
}

  • 0

try this, its much more elegant anyways. Think of an iterator as a pointer.

this is off top of my head. hope its right. :p

// This resizes it all accordingly

aliens.resize(2000000);
for (vector < vector<CAlien> >::iterator i = aliens.begin(); i <aliens.end(); i++)
{
        i->resize(10000);
}

// This performs an action() on every item in the list

for (vector< vector<CAlien> >::iterator i=aliens.begin(); i < aliens.end(); i++)
{
     for (vector<CAlien>::iterator it = i->begin(); it < i->end(); it++)
     {
               it->action();
     }
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Not Netflix's fault and they had to switch to content creation. Content owners jacked up prices, won't allow them renew, and didn't give them the option to license new content.
    • As sphbecker said you obviously missed the hidden /s at the end of my comment.
    • Everything I have ever read about Windows 11's telemetry collection over Windows 10 is that it's minor. Why would Microsoft be leaving data on the table with Windows 10? That doesn't even pass the smell test. LOL Windows 11 can be less performant in certain cases (and faster in others) but that has always been blamed on extra security features in WIn11. Windows 11 when I used it always ran fine for me. I would say If you were really concerned about either you wont be running Windows to begin with or limited to a secondary machine.
    • There was a brief time when they allowed Netflix (ad free) to pretty much license everything affordable, and piracy actually went down. When they saw how much Netflix was making, number of users, etc, they stopped allowing renewing of content and withholding it to make their own streaming services.
    • Wine 10.9 released bringing EGL support for all graphic drivers and several bug fixes by David Uzondu Wine 10.9 is out now, bringing EGL library support for all graphics drivers. For anyone trying to get Windows software running smoothly on Linux or macOS, this unification of EGL access promises more consistent graphical behavior and potentially better performance for applications that lean on it. EGL is a crucial bridge, letting things like OpenGL ES (often used in mobile apps, some desktop applications, and even games) talk to your system's window manager. Having it standardized across all drivers under Wine means fewer instances where an app might render beautifully on one setup but turn into a pixelated mess or refuse to launch on another. Gamers, and especially those dabbling with newer Windows titles, will appreciate that the bundled vkd3d, Wine's secret sauce for translating Direct3D 12 calls to Vulkan, has been upgraded to version 1.16. Every update to vkd3d generally means improved compatibility with the latest and greatest Direct3D 12 titles and ongoing fixes for stubborn ones. Considering vkd3d is a core component for projects like Steam's Proton, which enables a massive library of Windows games on Linux and the Steam Deck, this update is certainly welcome news. Key improvements vkd3d 1.16 brings include: DXIL shaders are now supported in the default configuration, raising the maximum supported shader model to 6.0. Graphics pipeline state objects can be created from shaders with embedded root signatures. Implementation of the SetEventOnMultipleFenceCompletion() method of the ID3D12Device1 interface. Experimental support for compiling DXIL shaders is now a fully supported and enabled-by-default feature in libvkd3d-shader. libvkd3d-shader has initial support for geometry shaders and various new intrinsic functions. The vkd3d demos now work on both Microsoft Windows and Apple macOS builds. Moving on, support for compiler-based exception handling with Clang also makes an appearance; this is a technical enhancement that can lead to greater stability for applications built using that specific compiler when they are run through Wine, reducing unexpected crashes for a subset of programs. Furthermore, Wine 10.9 introduces initial groundwork for generating Windows Runtime metadata in WIDL, its Interface Definition Language tool. As always with development releases, there is a solid list of bug fixes, 34 of them in this particular version. Some notable ones include resolving fullscreen issues with the classic 3D Pinball - Space Cadet, fixing display artifacts in Empire Earth, sorting out text display problems in the EA app launcher, and addressing crashes or visual glitches in games like Dyson Sphere Program and platforms like Geekbench 6. If you're interested in trying out this release, follow the installation instructions for your platform: Ubuntu/Debian, Fedora, or macOS. Installation guides for other platforms, including Slackware, are also available. The release notes for version 10.9 are available here.
  • Recent Achievements

    • First Post
      ClarkB earned a badge
      First Post
    • Week One Done
      Epaminombas earned a badge
      Week One Done
    • Week One Done
      Prestige Podiatry Care earned a badge
      Week One Done
    • Week One Done
      rollconults earned a badge
      Week One Done
    • Week One Done
      lilred1938 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      143
    2. 2
      Xenon
      130
    3. 3
      ATLien_0
      124
    4. 4
      +Edouard
      102
    5. 5
      snowy owl
      97
  • Tell a friend

    Love Neowin? Tell a friend!