473,402 Members | 2,061 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,402 software developers and data experts.

An Array and memory problem

Hi,

Two things confused me and I hope somebody can point out the wrong
part, thanks in advance

char * mytest ( const char src[])
{
char *ret;
ret = new char;
strncpy(ret, src, 5);
return ret;
}
in main( ), I use:
char *y = mytest("abcdef");

The code works but I think there are problems. I used 'new' to allocate
some memory, how to release them?
Another one:

char * mytest ( const char src[])
{
char ret[10];
strncpy(ret, src, 5);
return ret;
}
in main ( ) : char *y = mytest("abcdef");

After we use 'char rec[10]', I think the it is in stack and 'rec' will
be released after the function 'mytest' finishes. So I suppose I cannot
get anything in 'y', but why did I get 'abcde' in y?

Dan

Aug 2 '06 #1
1 1254
Daniel Smith wrote:
Hi,

Two things confused me and I hope somebody can point out the wrong
part, thanks in advance

char * mytest ( const char src[])
{
char *ret;
ret = new char;
You only allocate a _single_ char here. You need to check the length
of the "string" passed to this function and allocate _one_more_ than
the length:

char *ret = new char[strlen(src) + 1]();
strncpy(ret, src, 5);
And here you copy _five_ of them, stomping all over the memory you
don't even own. After that - undefined behaviour. You shouldn't be
using 'strncpy' either. Just copy the whole thing using 'strcpy':

strcpy(ret, src);
return ret;
}
in main( ), I use:
char *y = mytest("abcdef");

The code works but I think there are problems. I used 'new' to
allocate some memory, how to release them?
Since the proper allocation is done using 'new[]', you need to do

delete[] y;

here.
Another one:

char * mytest ( const char src[])
{
char ret[10];
strncpy(ret, src, 5);
return ret;
Here you're returning a pointer to the local array. As soon as the
function returns, the pointer is _invalid_.
}
in main ( ) : char *y = mytest("abcdef");

After we use 'char rec[10]', I think the it is in stack and 'rec' will
be released after the function 'mytest' finishes. So I suppose I
cannot get anything in 'y', but why did I get 'abcde' in y?
The behaviour is undefined. It could actually be (or pretend to be) what
you had expected.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 2 '06 #2

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

Similar topics

20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
7
by: James A | last post by:
Hi, I'm having trouble geting my head around this, although I know it's a pretty basic question. I have an application whereby an area of RAM is mapped to an array: unsigned char NV; Two...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
10
by: mark | last post by:
I have a simple windows application that has a function to read a csv file and enter the values into an array A as double(,). Also, an instance of form 2 (which has a DataGrid) is created and the...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
8
by: isaac2004 | last post by:
hello, i posted with a topic like this but got no real feedback(prob cuz of lapse in my explanation) so i am trying it again. i am trying to set up a function that brings in a txt file and adds the...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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,...

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.