标签: pointers

检查指针是否为NULL并导致运行时错误

我正在做leetcode问题https://leetcode.com/problems/add-two-numbers/

ListNode的定义是:

 // Definition for singly-linked list.
 struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
 };
Run Code Online (Sandbox Code Playgroud)

当我检查列表是否结束时,我使用了:

int v1 = h1 == NULL? h1->val:0; // h1 is defined before: ListNode* h1 = l1;
Run Code Online (Sandbox Code Playgroud)

但它返回运行时错误,但如果我将其更改为

int v1 = h1? h1->val:0;
Run Code Online (Sandbox Code Playgroud)

它被接受了.

这是为什么?

c++ pointers

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

复制后为什么我的字符串会被截断?

我试图找到是否less使用安装C(没有使用系统调用).但是我复制变量有问题.字符串的内容被截断:

 int ret;
 char * pathValue;
 char * pathValue2;
 char *token3;
 char * new_str ;
 int pathlength;
 pathValue = getenv ("PATH");
 if (! pathValue) {
    printf ("'%s' is not set.\n", "PATH");
 }
 else {
    printf ("'%s' is set to %s.\n", "PATH", pathValue);
 }

 pathlength = sizeof(pathValue)/sizeof(char);
 pathValue2 = malloc(sizeof(pathValue));
 strncpy(pathValue2, pathValue, pathlength);
 printf ("pathValue2 is to %s.\n", pathValue2);
 token3 = strtok(pathValue2, ":");
 ret = 1;
 printf("Looking for less\n");
 while( token3 != NULL ) {
    printf("Looking for …
Run Code Online (Sandbox Code Playgroud)

c string malloc pointers sizeof

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

指针和引用如何保存在内存中

考虑这两个文件:

//main1.cpp

int main()
{
    int a = 0;
    int &b = a;
}

//main2.cpp

int main()
{
    int a = 0;
    int *b = &a;
}
Run Code Online (Sandbox Code Playgroud)

当我用gcc -S选项编译它并比较2个汇编器输出时,它们是完全相同的输出.那么为什么有些书说参考变量不使用额外的内存呢?

c++ memory pointers reference

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

malloc返回类型混乱

我正在经历这里,发现如果我们不包含stdlib.h,则转换malloc会导致不需要的行为,转换返回值以及系统上的指针和整数大小是否不同.

下面是SO问题中给出的代码片段.这是在64位机器上尝试的,其中指针和整数大小不同.

int main()
{
      int* p;
      p = (int*)malloc(sizeof(int));
      *p = 10;
      return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我们不包含stdlib.h,编译器将假定malloc返回类型为int,并且转换它并分配给不同大小的指针可能会导致不需要的行为.但我的问题是,为什么铸造intint*并将其分配给不同大小的指针可能会导致问题.

c malloc pointers

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

为什么不用5,3而不是3,5?

我已经编写了这段代码,但我不知道为什么它会因5 3输入而失败.当我给出3 5它工作正常.在每种情况下,如果第二个数字更大,它有效,但为什么呢?我已经用malloc尝试过了.我正在使用Windows 7,代码块10.05

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

int main()
{
    int **matr;
    int row, col, i, j;

    scanf("%d", &row);    //number of rows
    scanf("%d", &col);    //number of cols

    matr = calloc(col, sizeof(int*));      //creating cols
    for(i = 0; i < col; i++)
    {
        matr[i] = calloc(row, sizeof(int));  //in every col i create an array with the size of row
    }

    for(j = 0; j < row; j++)
    {
        for(i = 0; i < col; i++)
        {
            matr[j][i] = 10;        //fill the matrix with …
Run Code Online (Sandbox Code Playgroud)

c pointers matrix

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

去数组的方法

我可以将数组及其指针用于Go方法吗?

我有以下代码:

var array = [3]string{"A", "B", "C"}
type arrayTypePt *[3]string
func (m *arrayTypePt) change() { m[1] = "W" }
func main() {
    (arrayTypePt(&array)).changeArray4()
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码:http : //play.golang.org/p/mXDEhmA9wk

给我一个错误:

invalid receiver type *arrayTypePt (arrayTypePt is a pointer type)
invalid operation: m[1] (type *arrayTypePt does not support indexing)
arrayTypePt(&array).changeArray4 undefined (type arrayTypePt has no field or method changeArray4)
Run Code Online (Sandbox Code Playgroud)

当我尝试使用slice时,出现相同的错误。为什么我不能用方法做到这一点?

methods pointers go

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

将双指针转换为int指针

当我打印b并且d他们都持有相同的地址(地址a).那么为什么*b打印0*d打印5?

 void main() 
 {
    double a = 5.0;
    double *d = &a;
    int *b = (int*)d;
    int a1 = 10;
    cout << "Val of D : " << d << " Address of d :" << &d
         << " Value of *d :" << *d << endl;
    cout << "Val of B : " << b << " Address of B :" << &b
         << " Value of …
Run Code Online (Sandbox Code Playgroud)

c++ pointers type-punning

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

检查'C'中的NULL字符时出现奇怪的行为

为了澄清一点,我写了两个小测试程序,如下所示.

#include <stdio.h>                                                                                                                                                                       

int main(int argc, char *argv[])
{
    char *p = "ab";
    p++;
    p++;

    if (*p)
        printf("p is not NULL \n");
    else
        printf("ps is NULL \n");

    return 0;

 }
Run Code Online (Sandbox Code Playgroud)

上面的程序初始化p字符串文字的char指针ab.我将指针递增两次,然后if循环检查是否p指向非NULL字符.这样可以正常工作并提供以下输出.

ps is NULL 
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>


int main(int argc, char *argv[])
{
    char *p = '\0';                                                                                                                                                                      

    if (*p)
        printf("p is not NULL \n");
    else
        printf("ps is NULL \n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的程序将char指针初始化p为NULL字符\0.如果我编译并运行程序,我会得到分段错误.有人解释一下吗?

两种情况的唯一区别是NULL字符位于位置0和位置.2否则程序看起来与我相同.

c null pointers

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

取消引用运算符和endl没有输出

我试图让我的头围指针,并且无法理解为什么使用dereference运算符来打印一个值在"\n";添加到行时工作正常,但由于某种原因我在使用时没有得到任何输出endl;.终端显示没有输出endl;.这与输出缓冲区刷新有关吗?

#include <iostream>
using namespace std;

int main()
{
    int arrayA[] = {0, 1, 2, 3, 4, 5};
    int * ptr_P;

    ptr_P = arrayA;
    for (int i; i < 6; i++)
    {
        cout << *ptr_P << "\n"; // Works fine, but endl; does not
        ptr_P++;
    }
   return(0);
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

智能指针vs拥有原始指针

据我所知,最好使用智能指针,而不是通过原始指针管理动态分配对象的生命周期,例如: MyObject* obj = new Object();

但是在某些框架/库中,它们总是返回/使用原始指针而不是智能指针(也许它们有自己的GC对象?我不知道).

它也更容易使用

 MyObject* obj = GetAObject(); // return raw owning pointer
Run Code Online (Sandbox Code Playgroud)

SharedPointer<MyObject> obj = GetAObject(); // return smart pointer
Run Code Online (Sandbox Code Playgroud)

应该总是使用智能指针而不是手动new/ delete(如上例所示),还是应该使用原始资源拥有指针?

c++ pointers

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

标签 统计

pointers ×10

c++ ×5

c ×4

malloc ×2

go ×1

matrix ×1

memory ×1

methods ×1

null ×1

reference ×1

sizeof ×1

string ×1

type-punning ×1