我有一个带有整数指针数组的结构.第一个指针的值在对printf的调用之间发生变化.除非我通过函数生成结构,否则不会发生这种情况.代码如下.有人能告诉我发生了什么吗?谢谢.
struct a {
int *nums[10];
};
struct a *makeS();
int main(int argc, char *argv[]){
struct a *_b = (struct a*) malloc(sizeof(struct a));
struct a *_c = makeS();
int i = 1;
_b->nums[0] = &i;
printf("Num is %d\n", *_b->nums[0]); // prints 1
printf("Num is %d\n", *_b->nums[0]); // prints 1
printf("Num is %d\n", *_b->nums[0]); // prints 1
printf("Num is %d\n", *_b->nums[0]); // prints 1 … etc.
printf("Num is %d\n", *_c->nums[0]); // prints 1
printf("Num is %d\n", *_c->nums[0]); // prints 0
printf("Num …Run Code Online (Sandbox Code Playgroud) 我t动态创建了一个3D数组(t类型int***).现在我想删除它.
我遇到了两个建议:一个就是这样做
delete[] t;
Run Code Online (Sandbox Code Playgroud)
显然,它会删除所有内容.
另一种是做类似的事情
for(int i=0;i<3;i++)
{
for(int j=0;j<t1[i];j++)
{
delete[] t[i][j];//delete all 1D array
}
delete[] t[i];//delete all 2D array
}
delete[] t;//delete the 3D array
Run Code Online (Sandbox Code Playgroud)
(t1存储尺寸t[i]和t2尺寸t[i][j])
什么是最好的方法?
我想了解从函数调用返回指针的行为.假设我有以下简单代码:
int main(){
int i;
float *ssd;
ssd = test();
for (i = 0; i < 3; ++i) {
printf("%f, ", ssd[i]);
}
printf("\n \n");
memset(ssd, 0.0, 3*sizeof(float));
for (i = 0; i < 3; ++i) {
printf("%f, ", ssd[i]);
}
printf("\n \n");
}
float *test(){
float *buff = malloc(3* sizeof(float));
int i;
for (i = 0; i < 3; ++i) {
buff[i] = (float) (6.31 + i);
}
free(buff);
return buff;
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我在buff里面创建了一个临时缓冲区test().在我回来之前,我buff在 …
我有以下cpp函数,并希望在R中编写它
我使用rcpp包编译并使用它但发生了一些错误
实际上我在R中使用指针时遇到问题
void creationPI(double *distC, int mC, int tailleC, double *PIC,int aC)
//distC: distribution de X ; mC= smax-smin+1 ie u+v+1; tailleC=a+1; PIC la matrice PI comme resultat
{
double *f;//=NULL; /*f(k)=P[X<=k]*/
int t1,k,b,c,l;
int top_un,top_deux;
//FILE *matrix;
t1=2*(aC-1)+1; // taille du vecteur des f ca va de 1-a à a-1 ; k va de [0 à 2*(a-1)]
/* ALLOCATION DES MATRICES*/
//if (!(f = (double *)calloc(t1, sizeof(double))))
//exit(ALLOC_ERROR);
f = (double *)calloc(t1, sizeof(double));
/* CREATION DES f */ …Run Code Online (Sandbox Code Playgroud) 这是我为GCC编译器编写的代码.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p;
p = (int *)malloc(9 * sizeof(char));
p[0] = 2147483647;
p[1] = 2147483647;
p[2] = 1025;
printf("%d, %d, %d, %d\n", sizeof(char), *(p), *(p+1), *(p+2));
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
1, 2147483647, 2147483647, 1025
Run Code Online (Sandbox Code Playgroud)
我的问题是,虽然我只为指针分配了9个字节,但它似乎使用了所有12个字节.结果是相同的(除非编译器警告),如果我转换(char *)而不是(int*).分配malloc()是完整的圆圈吗?即它总是以指针的数据类型的倍数分配,而不管我们分配的是什么?或者具体实施?
我是一名努力理解C指针和数组的Java程序员.(完全披露:我也是CS学生,是的,这个问题可以帮助我完成编程任务.)
我正在尝试创建一个int*指针数组,然后确保每个指针都为NULL.稍后,当我需要在数组中查找数据时,这将发挥作用; 在给定的位置中是否存在有效的int或NULL将是重要的.
因此,为数组分配空间很简单,但如何将所有指针设置为NULL?这是我不那么出色的尝试:
#include<stdio.h>
#include<stdlib.h>
#define TABLESIZE 10
int main(int argc, char *argv[]){
int* table = (int*) malloc(TABLESIZE * sizeof(int));
// Initialize all *int pointers to NULL
int i;
for(i=0; i<TABLESIZE; i++){
if((table+i)!=NULL){ // They may be NULL already? I don't know...
*(table+i) = NULL; // This generates a warning:
// "warning: assignment makes integer from pointer without a cast [-Wint-conversion]"
}
}
// Sanity check : are all int* are NULL ?
for(i=0; i<TABLESIZE; i++){
printf("%d: %p %d …Run Code Online (Sandbox Code Playgroud) 在阅读K&R(第6.5节,第二版)时,我遇到了以下功能:
struct tnode *talloc(void)
{
return (struct tnode *) malloc( sizeof(struct tnode) );
}
Run Code Online (Sandbox Code Playgroud)
该函数分配一些空格来存储struct tnode.我只是想通过询问我是否会达到以下目的来检查我的理解:
struct tnode *talloc(void)
{
struct tnode s;
return &s;
}
Run Code Online (Sandbox Code Playgroud) 我读了一本C书.但是无法理解句子
如果调用calloc成功,则返回指向内存中数组基类的void*类型的指针;
这里,如果指向类型为void*的指针指向内存中数组的基数,那么array [0]的类型为void?
我想知道什么是......
谢谢大家阅读我的问题,希望得到答复!
1)
int main()
{
int *j,i=0;
int A[5]={0,1,2,3,4};
int B[3]={6,7,8};
int *s1=A,*s2=B;
while(*s1++ = *s2++)
{
for(i=0; i<5; i++)
printf("%d ", A[i]);
}
}
2)
int main()
{
char str1[] = "India";
char str2[] = "BIX";
char *s1 = str1, *s2=str2;
while(*s1++ = *s2++)
printf("%s ", str1);
}
Run Code Online (Sandbox Code Playgroud)
第二个代码工作正常,而第一个代码导致一些错误(可能是分段错误).但是如何在程序2中的指针变量s2工作正常(即直到字符串的结尾),而不是在程序1中,它无限运行....另外,在第二个程序中,s2变量不会增加超出阵列的长度?
我使用malloc获得了一些非常奇怪的行为.我从不分配超过4kB,所以这对我来说似乎特别奇怪.我的主要看起来像:
int main(int argc, char **argv)
{
char *buf;
char *raw = malloc(1024);
fgets(raw, 1024, stdin);
parse(raw, &buf); // Process raw input, get parse length
printf("raw: 0x%p\n", raw); // Outputs 0x00000000
if(raw != NULL)
{ // Only prints when less than 10 characters are entered
free(raw);
printf("raw is not NULL\n");
}
free(buf);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我输入少于10个字符时,这可以正常工作,当我输入10个字符时,我得到分段错误,当我输入10个以上时,输出显示raw为NULL.应该注意的是,raw的大小是1024 malloc'd字节,所以我应该有更多的工作空间.
解析函数是:
int parse(char *src, char **dst)
{
int num_valid = 0, len = strlen(src), j = 0;
// Count number of valid characters …Run Code Online (Sandbox Code Playgroud) pointers ×10
c ×8
arrays ×3
malloc ×3
c++ ×2
struct ×2
allocation ×1
new-operator ×1
r ×1
rcpp ×1