碰见C语言的几个小问题

最近碰见C语言的几个小问题。

1.关于结构体的大小(sizeof)。

  1. /*struct assign
  2.   {
  3.     char a;
  4.     int  b;
  5.     char c;
  6.   };
  7.   */
  8.   struct assign
  9.   {
  10.     char a;
  11.     char b;
  12.     int c;
  13.   };
  14.   int i = offsetof(struct assign,b);
  15.   printf("%d/n",sizeof(struct assign));
  16.   printf("%d/n",i);

分别声明以上两种结构体,分析结构体的长度。

第一种应该为12,第二种应该为8.这个主要是内存对齐问题。32位机,为了提高速度,是不会把一个int型的放在2个32位的空间里面的。 因为当读取的时候会读取两次,系统不允许这样做的。也可以用offsetof求其中的一个结构体中的变量相对与结构体首部的位置。

(offsetof是一个宏,上次写可变长的函数时候就说过,对内存的地址进行操作。定义如下:#define offsetof(s,m) (size_t)&(((s *)0)->m))。

 

2.关于结构体和字符串。

     在内存中结构体和字符串都一样,一段内存,而结构体不同的是对其进行划分为不同的类型,而字符串全都是字符类型的。所以可以把字符串强制转化为一个结构体。

  1.  struct test
  2.   {
  3.     char a;
  4.     char b;
  5.   };
  6.   char * tmp ="abcdef";
  7.   struct test *pig = malloc(sizeof(struct test));
  8.   *pig = ((struct test *)tmp)[1];
  9.   printf("%c/n",pig->a);
  10.   printf("%c/n",pig->b);

最近看文件系统源代码的时候,没大看懂这个问题。现在明白了。一个字符串里面有很多相似与结构的字符,对其进行强制类型转化后,可以像数组一样操作了。

PS:没事看看源代码,会很有收获的,无论是在语法还是在一定的设计上,都会有很大收获。不断体会UNIX的哲学思想(KISS)。

当Spring Security在请求headers中有token情况下出现跨域问题时,可采用以下解决办法: ### 方法一:在SpringSecurity配置中添加跨域支持 在`protected void configure(HttpSecurity http)`配置中添加`http.cors().configurationSource(CorsConfigurationSource())`代码,并配置跨域访问资源。示例代码如下: ```java //在 protected void configure(HttpSecurity http)配置中添加下面这行代码: http.cors().configurationSource(CorsConfigurationSource()); //配置跨域访问资源 private CorsConfigurationSource CorsConfigurationSource() { CorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); //同源配置,*表示任何请求都视为同源,若需指定ip和端口可以改为如“localhost:8080”,多个以“,”分隔; corsConfiguration.addAllowedHeader("*");//header,允许哪些header,本案中使用的是token,此处可将*替换为token; corsConfiguration.addAllowedMethod("*"); //允许的请求方法,PSOT、GET等 ((UrlBasedCorsConfigurationSource) source).registerCorsConfiguration("/**",corsConfiguration); //配置允许跨域访问的url return source; } ``` 此方法通过自定义`CorsConfigurationSource`,允许所有来源、所有请求头和所有请求方法的跨域访问,同时可将`*`替换为具体的`token`来指定允许的请求头 [^2]。 ### 方法二:在SpringSecurity拦截链条中进行跨域配置 在`configure(HttpSecurity http)`方法中进行如下配置: ```java @Override protected void configure(HttpSecurity http) throws Exception { http // 放行所有OPTIONS请求 .antMatchers(HttpMethod.OPTIONS).permitAll() // 其他的需要登陆后才能访问 .anyRequest().authenticated() .and() // 过滤OPTIONS请求 .addFilterBefore(new OptionsFilter(), WebAsyncManagerIntegrationFilter.class) .and() // 跨域 .cors() .and() // 禁用csrf .csrf().disable(); } ``` 该方法在SpringSecurity的拦截链条中,放行所有`OPTIONS`请求,过滤`OPTIONS`请求,并开启跨域支持,同时禁用`csrf` [^3]。 ### 方法三:使用全局跨域配置类 创建一个全局跨域配置类,实现`WebMvcConfigurer`接口,重写`addCorsMappings`方法。示例代码如下: ```java package com.dhnsoft.education.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import static org.springframework.http.HttpHeaders.SET_COOKIE; /** * @Description * @ClassName CrosConfig * @Author dhn * @date 2022.05.07 23:27 */ @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") //允许跨域的域名,可以用*表示允许任何域名使用 .allowedMethods("*") //允许任何方法(post、get等) .allowedHeaders("*") //允许任何请求头 .allowCredentials(true) //带上cookie信息 .exposedHeaders(SET_COOKIE) .maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果 } } ``` 此方法通过全局跨域配置类,允许所有路径的跨域访问,指定允许的域名、请求方法、请求头,允许携带`cookie`信息,并设置预检验请求的缓存时间 [^4]。 ### 方法四:在SpringSecurity配置中添加http.cors() 当应用使用了Spring Security之后,若上述配置方法失效,可在`WebSecurityConfigurerAdapter`的`configure(HttpSecurity http)`配置方法中加上`http.cors()`配置。示例代码如下: ```java public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and() ... } } ``` 该方法确保在Spring Security环境下,跨域配置能够生效 [^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值