[英]C - pointer to pointer to structure - segmentation fault
I learn C pointers, therefore I tried to create array of structs using pointer notation. 我学习C指针,因此我尝试使用指针表示法创建结构数组。 I allocate array of pointers to struct
User
, I set names and identifiers, then I try to print it and then it crashes. 我将指针数组分配给struct
User
,设置名称和标识符,然后尝试打印它,然后崩溃。
Dockerfile: Dockerfile:
FROM ubuntu:xenial
RUN apt-get update \
&& apt-get install -y \
software-properties-common \
wget \
&& add-apt-repository -y ppa:ubuntu-toolchain-r/test \
&& apt-get update \
&& apt-get install -y \
make \
git \
curl \
vim \
vim-gnome \
&& apt-get install -y cmake=3.5.1-1ubuntu3 \
&& apt-get install -y \
gcc-4.9 g++-4.9 gcc-4.9-base \
gcc-4.8 g++-4.8 gcc-4.8-base \
gcc-4.7 g++-4.7 gcc-4.7-base \
gcc-4.6 g++-4.6 gcc-4.6-base \
&& update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 100 \
&& update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 100
COPY ./*.c /tmp/
WORKDIR /tmp/
RUN gcc -Wall main.c -o main
ENTRYPOINT ./main
I run my program in docker container because I want to have unused memory. 我在Docker容器中运行程序,因为我想拥有未使用的内存。
My program: 我的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_USERS_NUMBER 10
void set_users();
void print_users();
typedef struct User
{
int id;
char* username;
} User;
User **users;
int main(int argc, const char* argv[])
{
set_users();
print_users();
return 0;
}
void set_users()
{
users = (User **) malloc(MAX_USERS_NUMBER * sizeof(User *));
int i;
for (i = 0; i < MAX_USERS_NUMBER; i++)
{
*(users + i) = (User *) malloc(sizeof(User));
(*users + i)->id = i;
(*users + i)->username = (char *) malloc(strlen("username") + 1);
strcpy((*users + i)->username, "username");
printf("%d. id: %d, username: %s\n", i, (*users + i)->id, (*users + i)->username);
}
}
void print_users()
{
printf("\nUsers\n");
int i;
for (i = 0; i < MAX_USERS_NUMBER; i++)
{
printf("%d. id: %d: ,username: %s\n", i, (*users + i)->id, (*users + i)->username);
}
}
I build image by command: 我通过命令构建图像:
docker build -t gcc-poc .
And I run it by command: 然后通过命令运行它:
docker run -it gcc-poc /bin/bash
I always get result: 我总是得到结果:
0. id: 0, username: username
1. id: 1, username: username
2. id: 2, username: username
3. id: 3, username: username
4. id: 4, username: username
5. id: 5, username: username
6. id: 6, username: username
7. id: 7, username: username
8. id: 8, username: username
9. id: 9, username: username
Users
0. id: 0: ,username:
1. id: 1: ,username: username
2. id: 2: ,username: username
3. id: 3: ,username: username
Segmentation fault
I have read book "Understanding and using C pointers" by O'Reilly but I cannot diagnose the cause. 我已经读过O'Reilly的书“理解和使用C指针”,但我无法诊断出原因。
Since users
is a pointer to pointer to User
, *(users + i)
is a pointer to User
and is proper pointer arithmetic. 由于
users
是指向User
指针,因此*(users + i)
是指向User
的指针,是正确的指针算法。 But (*users + i)
is not the pointer arithmetic that will produce the result you are looking for. 但是
(*users + i)
不会产生所需结果的指针算法。
When a pointer is incremented, the increment will be the size of the type it is pointing to. 当指针递增时,增量将是其指向的类型的大小。 For example here:
例如这里:
users + 1
will increment users
with sizeof(User *);
users + 1
将用sizeof(User *);
增加users
sizeof(User *);
This would be the size of a pointer on your system. 这将是系统上指针的大小。
But *users + 1
will increment *users
with sizeof(User)
; 但是
*users + 1
将增加*users
与sizeof(User)
; This is the size of the User
structure you have defined. 这是您定义的
User
结构的大小。
So you have to change (*users + i)
to (*(users + i))
in the code. 因此,您必须在代码中将
(*users + i)
更改为(*(users + i))
。 The extra pair of parenthesis is needed because the ->
operator has higher precedence than *
operator. 需要额外的一对括号,因为
->
运算符的优先级高于*
运算符。
Change your set_users
and print_users
functions like this and it will work as intended: 像这样更改
set_users
和print_users
函数,它将按预期工作:
void set_users()
{
users = (User **) malloc(MAX_USERS_NUMBER * sizeof(User *));
int i;
for (i = 0; i < MAX_USERS_NUMBER; i++)
{
*(users + i) = (User *) malloc(sizeof(User));
(*(users + i))->id = i;
(*(users + i))->username = (char *) malloc(strlen("username") + 1);
strcpy((*(users + i))->username, "username");
printf("%d. id: %d, username: %s\n", i, (*(users + i))->id, (*(users + i))->username);
}
}
void print_users()
{
printf("\nUsers\n");
int i;
for (i = 0; i < MAX_USERS_NUMBER; i++)
{
printf("%d. id: %d: ,username: %s\n", i, (*(users + i))->id, (*(users + i))->username);
}
}
Just try this: 尝试一下:
#define MAX_CHARS_USERNAME 40
void set_users()
{
static/*added*/ User* pUser;
pUser = (User *) malloc(MAX_USERS_NUMBER * sizeof(User));
users = &pUser;
int i;
for (i = 0; i < MAX_USERS_NUMBER; i++)
{
(*users + i)->id = i;
(*users + i)->username = (char *) malloc(MAX_CHARS_USERNAME);
snprintf((*users + i)->username, MAX_CHARS_USERNAME, "username%d",i);
printf("%d. id: %d, username: %s\n", i, (*users + i)->id, (*users + i)->username);
}
}
For a pointer to pointer (if needed at all) you might want to use something like ppUser
. 对于指针的指针(如果需要的话),您可能想要使用
ppUser
类的ppUser
。 And do not forget to free the memory (in the reverse order) after usage! 并且不要忘记在使用后释放内存(以相反的顺序)!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.