面试题求助(数组的扩充)

liuchunshanyj 2012-08-23 10:38:34
前几天的一道面试题,一直没想明白, 核心问题是这样的:

定义一个数组 比如:

int *a = (int*) malloc(sizeof(int)*20);
a[0]=0; a[1]= 1; .............a[19]= a[19];

现在,a指针 开辟的20个int内存已经存满了,想存第21个数据

问题1: 扩展这个数组,注意内存连续性(就是在将第21个数据 放在紧接着a[19] 地址后面的那块内存里 )

我的想法是:

int *p = (a+20); //指向 a[20]
*p = 20;

这样是不是相当于在一个未知的内存上写数据,应该是不合法的吧, 但是我想不到别的办法。

高手们会怎么做呢? 求助!



另外第二个类似
问题2:

内存操作:将指针unsigned char* ptr的内容向后移动4个字节




...全文
220 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lnyat 2012-08-24
  • 打赏
  • 举报
回复
1、重新分配内存,复制
2、*(&ptr + 1) = ptr;
赵4老师 2012-08-24
  • 打赏
  • 举报
回复
realloc
Reallocate memory blocks.

void *realloc( void *memblock, size_t size );

Routine Required Header Compatibility
realloc <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

Parameters

memblock

Pointer to previously allocated memory block

size

New size in bytes

Remarks

The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc.

The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.

realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call

_set_new_mode(1)

early in your program, or link with NEWMODE.OBJ.

When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support.

Example

/* REALLOC.C: This program allocates a block of memory for
* buffer and then uses _msize to display the size of that
* block. Next, it uses realloc to expand the amount of
* memory used by buffer and then calls _msize again to
* display the new amount of memory allocated to buffer.
*/

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

void main( void )
{
long *buffer;
size_t size;

if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );

size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u\n", size );

/* Reallocate and show new size: */
if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );

free( buffer );
exit( 0 );
}


Output

Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000


Memory Allocation Routines

See Also calloc, free, malloc
jiangshi061 2012-08-24
  • 打赏
  • 举报
回复
用realloc 可以,但是一定要注意一个细节。否则拿不到分。

int *p = (a+20); //指向 a[20]
*p = 20;
char *q;
q = p;
p = (char * ) realloc (q,21); //这里的 q 可能会产生悬挂,应立即销毁
delete q;
wangweizhaoxin 2012-08-24
  • 打赏
  • 举报
回复
第一问:显然是调用realloc,百度下realloc,很简单的。
第二问:*(ptr+4)就可以了。
mengjiacun 2012-08-24
  • 打赏
  • 举报
回复
好帖子啊,见识了
  • 打赏
  • 举报
回复
重新分配内存可以 要么就非法写入未分配给你的内存
感觉这题是个坑,你应该质疑问题的可行性,说明理由,应该就能答对
因为面试,不一定要按常规来的
tchar 2012-08-24
  • 打赏
  • 举报
回复
3楼正解
dhdahai 2012-08-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

只能重新申请然后复制
那些内存并未分配给你所以你不该使用,假如那些内存已经被用于其他功能,那你的行为会造成什么后果自己想

或者你可以不用new,直接定义一个数组然后在它后面写数据,看看什么后果
局部数组的内存后面绝对是有用的内存,这点倒是可以肯定,连续压栈么
[/Quote]

++
baichi4141 2012-08-24
  • 打赏
  • 举报
回复
只能重新申请然后复制
那些内存并未分配给你所以你不该使用,假如那些内存已经被用于其他功能,那你的行为会造成什么后果自己想

或者你可以不用new,直接定义一个数组然后在它后面写数据,看看什么后果
局部数组的内存后面绝对是有用的内存,这点倒是可以肯定,连续压栈么
ken_scott 2012-08-24
  • 打赏
  • 举报
回复
第二个用memmove

第一个第一个想法是用realloc
如果出题者的意思不是这样的, 可以考虑压缩(但这一招不是总管用)
比如:
可以将value: int(4bytes)按下列空间存储:
if value <= 0x7F: 1 byte
else if value <= 0x3FFF: 2 bytes
else if value <= 0x1FFFFF: 3 bytes
else if value <= 0x0FFFFFFF: 4 bytes
else: 5 bytes
只要做出一个一一映射的方法, 让任意int写入后, 仍可正确读出就行
当然这种方式也有不可用的时候
cbzjzsb123 2012-08-24
  • 打赏
  • 举报
回复
1、重新分配内存,复制
2、*(&ptr + 1) = ptr;

  • 打赏
  • 举报
回复
用malloc重新申请大于20的内存,然后将原来的数组复制过去。



qq120848369 2012-08-23
  • 打赏
  • 举报
回复
realloc不行?

64,424

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧