473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

dynamic memory allocating

i'm having a really hard time with this one:

class foo
{
private:
char* p;
public:
foo(int);
};
foo::foo(int j)
{
p = new char[j];
p[0] = '1';
p[1] = '2';
};
int main(void)
{
foo k(2);
return 0;
}
why can't i set the value of p[1]? p[0] passes ok, while p[1] = '2' is just
ignored.
please help
--
You're never too young to have a Vietnam flashback
Apr 24 '06 #1
2 1379
On Mon, 24 Apr 2006 16:31:17 +0200, "filox"
<fi*****************@yahoo.com> wrote:
i'm having a really hard time with this one:

class foo
{
private:
char* p;
public:
foo(int);
};
foo::foo(int j)
{
p = new char[j];
p[0] = '1';
p[1] = '2';
};
int main(void)
{
foo k(2);
return 0;
}
why can't i set the value of p[1]? p[0] passes ok, while p[1] = '2' is just
ignored.
please help


What do you mean by "ignored"? Are you trying to create a C-style
string? If so, you will need to leave room for the null byte at the
end.

(BTW, you have a memory leak -- by not calling "delete [] p;" in the
destructor of your class or some other appropriate place).

--
Bob Hairgrove
No**********@Home.com
Apr 24 '06 #2
uhm, never mind. there's actually no problem, i just put too much fait in Ms
Visual Studio debugger :)
its ok now. thanks for your effort, though

--
You're never too young to have a Vietnam flashback
"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:5n********************************@4ax.com...
On Mon, 24 Apr 2006 16:31:17 +0200, "filox"
<fi*****************@yahoo.com> wrote:
i'm having a really hard time with this one:

class foo
{
private:
char* p;
public:
foo(int);
};
foo::foo(int j)
{
p = new char[j];
p[0] = '1';
p[1] = '2';
};
int main(void)
{
foo k(2);
return 0;
}
why can't i set the value of p[1]? p[0] passes ok, while p[1] = '2' is justignored.
please help


What do you mean by "ignored"? Are you trying to create a C-style
string? If so, you will need to leave room for the null byte at the
end.

(BTW, you have a memory leak -- by not calling "delete [] p;" in the
destructor of your class or some other appropriate place).

--
Bob Hairgrove
No**********@Home.com

Apr 24 '06 #3

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

Similar topics

4
by: trouble | last post by:
Hi, I have a program that needs to reserve about 1 to 10 Mb of memory every time it's run. Only one instance of the program is run at any given time but it's called about 100 times in...
1
by: Tommy Lang | last post by:
I am trying to learn to use dynamic variables. I have pasted the code below. Is this the proper way of using dynamic variables? Thanks, Tommy ...
11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
6
by: Sandeep Chikkerur | last post by:
Hi, If the entire heap memory for dynamic allocation is not available, does the compiler always return NULL ? eg: char *s; s = (char *)malloc(...);
14
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
4
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... ...
13
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
9
by: danil52 | last post by:
I have dynamic memory allocation in my constructor and deallocation in my destructor. However if there is exception being thrown in constructor, the destructor does not get called and memory leaks...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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,...
0
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...

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.