相关疑难解决方法(0)

用new定义的数组大小?

是否有一个函数(可以编写),它允许知道用new定义的数组的大小:

  int *a=new int[3];
  *a=4;
  *(a+1)=5;
  *(a+2)=6;  
Run Code Online (Sandbox Code Playgroud)

谢谢!

c++ arrays sizeof new-operator

2
推荐指数
2
解决办法
165
查看次数

如何知道传递给函数的字符数组的大小(以字节为单位)?

我正在测试sizeof运算符。在我的代码中的两种情况下,我得到了指针大小(我认为)。在其他情况下,我得到数组占用的字节数将数组传递给函数时,如何以字节为单位获取数组的大小? 不是的sizeof操作符就够了吗?难道我做错了什么?

#include <stdio.h>

/*Testing the operator sizeof*/

void test (char arrayT[]);
void test2 (char *arrayU);

int main(int argc, char *argv[]){

    char array1[7];
    printf("array1 size is:%d\n", sizeof array1);
    /*array1 size is: 7*/

    char array2[] = { '1', '2', '3', '4', '5', '6', '7'};
    printf("array2 size is:%d\n", sizeof array2);
    /*array2 size is: 7*/

    char array3[] = "123456";
    printf("array3 size is:%d\n", sizeof array3);
    /*array3 size is: 7*/

    unsigned char array4[] = …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers sizeof char

2
推荐指数
1
解决办法
4857
查看次数

关于sizeof运算符的困惑

我在Mac OSX Lion上使用C++,我创建了以下代码:

float* floatArray = new float[10];

for(int i = 0; i < 10; i++)
{
    floatArray[i] = 0.0 ;
}

std::cout<< "Test size of a float " << sizeof(floatArray[0]) << std::endl;
// The value is 4 byte which is what I would expect.

std::cout<< "Test the size of the whole array" << sizeof(floatArray) <<std::endl;
// the value is 8. I would have expected the value to be 40 bytes.
Run Code Online (Sandbox Code Playgroud)

我不明白的是什么?

提前致谢

c++ arrays macos sizeof

1
推荐指数
1
解决办法
179
查看次数

char指针c ++的长度

可能重复:
如何查找sizeof(指向数组的指针)

例如,使用malloc声明char指针时.有没有办法确定分配的长度.

例如,如果我定义一个char数组,我可以很容易地找到元素的数量

char a[10];
a[0]='c';
cout << sizeof(a) / sizeof(a[0]);
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用char指针,那么会产生10.

char *a;
a = (char*)malloc(10);
a[0] = 'c';
cout << sizeof(a) / sizeof(a[0]);
Run Code Online (Sandbox Code Playgroud)

然后它产生8,这显然是指针的大小,而不是分配的长度.

是否有可能找到char指针的分配长度?

我问这个而不是使用std :: string或其他的原因是因为我在解决一些面试问题时遇到了这个问题.因此出于好奇

c

1
推荐指数
1
解决办法
1万
查看次数

C/C++编译器如何自动推断某些C函数调用的数组长度?

可能重复:
如何查找sizeof(指向数组的指针)

在第一次调用strcpy_s时,编译器可以推导出数组长度,但在第二次调用中,必须传入数组长度.

TCHAR szTemp[512];
::strcpy_s(szTemp, "a long text message");

TCHAR* pszTemp = new TCHAR[512];
::strcpy_s(pszTemp, 512, "a long text message");
delete []pszTemp;
Run Code Online (Sandbox Code Playgroud)

编译器如何做到这一点?这是Microsoft唯一的扩展吗?另外,我怎么能声明我的方法来利用参数推导?

c c++ visual-c++

1
推荐指数
1
解决办法
469
查看次数

如何在没有任何附加参数的函数中获取数组大小

main()
{
   int arr[] = {1, 2, 3, 4};
   function(arr);
}
int function(int a[])
{       

}
Run Code Online (Sandbox Code Playgroud)

在这里,我想获得未初始化的数组的长度.在我的情况下的主要功能是我不知道..假设我从其他程序获取数组元素.

c

1
推荐指数
1
解决办法
79
查看次数

奇怪的数组错误C.

这可能是由于我对C的理解不足,但在处理数组时,我得到一些(我称之为)模糊的错误:

这是代码:

int * generateRandomArray(int numPageReplacements) {
    // Seed random number generator
    srand(time(NULL));

    int * randPages = (int *)malloc(sizeof(int)*numPageReplacements);

    for (int i = 0; i < numPageReplacements; i++) {
        randPages[i] = rand() % 10;     // generate (with uniform distribution) number 0 - 9
        printf("%d ", randPages[i]);    // for testing purposes
    }
    printf("\nRandomPages[3] = %d \n" ,randPages[3]);
    return randPages;   // return array pointer
}
Run Code Online (Sandbox Code Playgroud)

输出:

7 9 4 6 4 6 5 7 6 3 
RandomPages[3] = 6 
Program ended with …
Run Code Online (Sandbox Code Playgroud)

c arrays function

1
推荐指数
1
解决办法
90
查看次数

strncat上的分段错误()

首先,我写了一个简单的程序.

  1 #include <string.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4
  5 int main()
  6 {
  7     char *buf = (char*)malloc(sizeof(char)*2000);
  8     char tmp[256];
  9     strcpy (tmp, "test");
 10     printf ("tmp: %s\n", tmp);
 11     strncat (buf, tmp, strlen(tmp));
 12     printf ("buf: %s\n", buf);
 13 }
Run Code Online (Sandbox Code Playgroud)

预期的结果是:

tmp: test
buf: test
Run Code Online (Sandbox Code Playgroud)

但是在我的大项目中合并代码之后.(使用大量堆段)

153     char *inbuf = (char*)malloc(sizeof(char)*2000);
154     char *outbuf = (char*)malloc(sizeof(char)*2000);
155     char *oldinbuf = (char*)malloc(sizeof(char)*2000);
156     char *errbuf = (char*)malloc(sizeof(char)*2000);
157     memset (inbuf, '\0', strlen(inbuf));
158     memset (oldinbuf, '\0', …
Run Code Online (Sandbox Code Playgroud)

c string heap-memory segmentation-fault

1
推荐指数
1
解决办法
676
查看次数

数组名称和指向第一个元素的另一个指针有什么区别?

有一个数组A,定义为int A[10],并且有另一个包含基址的指针,定义为int *ptr = A.

我最近看过这篇文章 如何找到'sizeof'(一个指向数组的指针)?,我无法弄清楚A和ptr之间的区别是什么.他们俩都不只是拥有阵列的基地址吗?

我们A[i] and ptr[i]现在可以互换使用,对于一些小于10的整数.

那么为什么使用sizeof()得到的结果有所不同?

c++ arrays pointers

1
推荐指数
1
解决办法
74
查看次数

C 中堆上的数组分配

我已经研究过堆栈和堆,以及一般的内存管理主题,但有很多东西我无法理解

就像,如果我使用堆分配一个整数数组mallocrealloc我如何确定我想要使用的数组的确切大小?

这个例子:

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

int main(int argc, char* argv[]){
    int *array = (int *)malloc(2);

    array[0] = 2;
    array[1] = 1;
    //What? i have allocated just 2 to be the size
    array[2] = 3;
    array[3] = 4;
    array[4] = 4;
    array[5] = 6;
    //there is no segmentation fault


    for(int i = 0; i < sizeof(array); i++){
        printf("%d\n",array[i]);
    }

}
Run Code Online (Sandbox Code Playgroud)

我得到的奇怪结果是:

2
1
3
4
4
6
1041 // ???
0
Run Code Online (Sandbox Code Playgroud)

那么,有人可以向我解释一下我怎样才能malloc以100%正确的方式使用吗?

c

1
推荐指数
1
解决办法
2805
查看次数