473,503 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having problems initializing a managed array

Hi all,

I'm getting a runtime error for the below program, though it compiles
fine. The culprit seems to be the managed array--it doesn't exist.
What am I doing wrong?

RL

// OverL2_simple.cpp : main project file.

#include "stdafx.h"
#include "AClass.h"

using namespace System;

int main(array<System::String ^^args)
{
Console::WriteLine(L"Hello World");

AClass ^A10 = gcnew AClass(); //see AClass.h, .cpp below for
declaration/definition
for (int j=0; j<(A10->a1->Length); j++){
Console::WriteLine("a1 is: {0}", A10->a1[j]);
}

// Unhandled Exception: System.NullReferenceException: Object
reference not set to an instance of an object.
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
// Aclass.h
#pragma once
using namespace System;

ref class AClass
{
public:
AClass(void);
int pubint;
array<int>^ a1; //this array is causing problems
private:
int privint;

};
//////////////////////////////////////////////
// Aclass.cpp
#include "StdAfx.h"
#include "AClass.h"

//
AClass::AClass(void): pubint(1),privint(1)
{
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
}

/*
//compiles but give runtime error:
Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.
*/

Dec 28 '06 #1
3 1409
I'm getting a runtime error for the below program, though it compiles
fine. The culprit seems to be the managed array--it doesn't exist.
What am I doing wrong?
AClass ^A10 = gcnew AClass(); //see AClass.h, .cpp below for
>AClass::AClass(void): pubint(1),privint(1)
{
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
}
Subtle. in your constructor, you declare a local variable that has the same
name as your class member. I assume thatthis is copy and paste error.
you initialize it properly, but it hides your a1 member variable, which
remains uninitialized.
Then -in your test code- you dereference a1. since that is an unitialized
pointer, you will get an exception.

Change
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
to
a1 = gcnew array<int>(5);
for(int j=0; j<a1->Length; j++)
a1[j] = j;
to solve the problem
--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Dec 28 '06 #2
Muchos gracias Bruno van Dooren! Indeed this is the case--I have never
used managed arrays before and this being the first time, I did not
know the right format.
RL
Bruno van Dooren [MVP VC++] wrote:
Subtle. in your constructor, you declare a local variable that has the same
name as your class member. I assume thatthis is copy and paste error.
you initialize it properly, but it hides your a1 member variable, which
remains uninitialized.
Then -in your test code- you dereference a1. since that is an unitialized
pointer, you will get an exception.

Change
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
to
a1 = gcnew array<int>(5);
for(int j=0; j<a1->Length; j++)
a1[j] = j;
to solve the problem
--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Dec 28 '06 #3
raylopez99 wrote:
Muchos gracias Bruno van Dooren! Indeed this is the case--I have never
used managed arrays before and this being the first time, I did not
know the right format.
In C++/CLI, you can initialize the array at dynamic construction, which
is a really nice feature:

a1 = gcnew array<int>(5) {1,2,3,4,5};

Tom
Jan 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
26982
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
16
14071
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
6
2534
by: Omid Hodjati | last post by:
Hi All, I implemented an encryption algorithm using C#, native C++ and managed C++. Then I measured the CPU time used for executing this algorithm in all implementation. The managed version of...
0
1022
by: Mirek Rewak | last post by:
Hello, I have to move the project to the .NET (exactly ASP.NET) environment but I encountered some problems. The old code was written using C++ and MFC and was organized to separate "business...
8
1418
by: Johann Blake | last post by:
I need to call a C DLL function. The first parameter expects a pointer to a long. It returns a value at the address of the pointer. The second parameter expects a pointer to a pointer. It creates...
17
3678
by: Sharon | last post by:
I Have a PC with dual XEON CPU’s and 4 Giga Byte RAM win Windows XP Pro. I have 2 problems with it: (1) Windows is showing only 3.25 Giga byte on the System Properties ? General tab. While the...
5
1521
by: WesIdell | last post by:
Hi All: I need some advice regarding a problem that I'm experiencing. I'm using a group of TextBox controls in the .aspx page and am using a function in the .cs code to perform some actions with...
13
2306
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
6
4352
by: Jai Prabhu | last post by:
Hi All, Consider the following piece of code: void func (void) { static unsigned char arr = "\x00\xAA\xBB"; fprintf (stderr, "0x%x\n", arr); fprintf (stderr, "0x%x\n", arr);
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.