[英]Program receives signal - gdb
For the below question , 对于以下问题 ,
Exercise 12335 - Write a C program to read through an array of any type. Write a C program to scan through this array to find a particular value.
Here is my solution: 这是我的解决方案:
#include<stdio.h>
enum types
{
integer,
character,
floatValue
};
void f(void *, enum types);
void doSomething(void *, enum types);
int main(){
int a[] ={1,2,3};
enum types type = integer;
f(a, type);
}
void f(void *ptr, enum types type){
switch(type){
case(integer):
if(sizeof((int *)ptr)/sizeof(int) > 1){
doSomething(&((int *)ptr)[1], type); // Print second element
}
break;
case(character):
if(sizeof((char *)ptr)/sizeof(char) > 1){
doSomething(&((int *)ptr)[1], type); // Print second element
}
break;
case(floatValue):
if(sizeof((float *)ptr)/sizeof(float) > 1){
doSomething(&((int *)ptr)[1], type); // Print second element
}
break;
}
}
void doSomething(void *ptr, enum types type){
switch(type){
case(integer):
printf("%d", *(int *)ptr);
break;
case(character):
printf("%c", *(char *)ptr);
break;
case(floatValue):
printf("%f", *(float *)ptr);
break;
}
}
> gcc -g temp.c -o program.exe
> gdb program.exe
Below is the gdb output: 以下是gdb输出:
Question: 题:
Why does program.exe
not work? 为什么
program.exe
不起作用?
You can not find the size of an array based solely on the pointer (the pointer is only an address - it does not contain any information about what it points to). 您无法仅基于指针找到数组的大小(指针只是一个地址-它不包含有关其指向的任何信息)。
Below is a solution where the function is modified, and the array size is passed in as a parameter. 下面是一个修改函数的解决方案,并将数组大小作为参数传递。
#include<stdio.h>
enum types
{
integer,
character,
floatValue
};
void f(void *, enum types, int a_size);
void doSomething(void *, enum types);
int main(){
int a[] ={1,2,3};
enum types type = integer;
f(a, type, sizeof(a));
}
void f(void *ptr, enum types type, int a_size){
switch(type){
case(integer):
if(a_size/sizeof(int) > 1){
doSomething(&((int *)ptr)[1], type); // Print second element
}
break;
case(character):
if(a_size/sizeof(char) > 1){
doSomething(&((int *)ptr)[1], type); // Print second element
}
break;
case(floatValue):
if(a_size/sizeof(float) > 1){
doSomething(&((int *)ptr)[1], type); // Print second element
}
break;
}
}
void doSomething(void *ptr, enum types type){
switch(type){
case(integer):
printf("%d", *(int *)ptr);
break;
case(character):
printf("%c", *(char *)ptr);
break;
case(floatValue):
printf("%f", *(float *)ptr);
break;
}
}
I'm not sure if it will fix your gdb issue (I'm on Linux, not Windows, and not receiving any signals), but the program will run correctly. 我不确定它是否可以解决您的gdb问题(我使用的是Linux,而不是Windows,并且未收到任何信号),但是该程序可以正常运行。
[UPDATE] [更新]
This is a style thing, but I would update the f
function to look like this: 这是一个样式,但是我将更新
f
函数,使其看起来像这样:
void f(void *ptr, enum types type, int a_size){
int unit_size;
switch(type){
case(integer):
unit_size = sizeof(int);
break;
case(character):
unit_size = sizeof(char);
break;
case(floatValue):
unit_size = sizeof(float);
break;
default:
break;
}
if((a_size/unit_size) > 1){
doSomething(&((int *)ptr)[1], type); // Print second element
}
}
It makes it more readable, quicker to add different types, and if you change the call to doSomething
, you only have to modify it in one place. 它使可读性更高,添加不同类型的速度更快,并且如果将调用更改为
doSomething
,则只需在一个地方进行修改即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.